package net.minecraft.world.level.storage.loot.entries;

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import java.util.function.Consumer;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.resources.ResourceLocation;

public class LootTableReference extends LootPoolSingletonContainer {
    private final ResourceLocation name;
    
    private LootTableReference(final ResourceLocation sm, final int integer2, final int integer3, final LootItemCondition[] arr, final LootItemFunction[] arr) {
        super(integer2, integer3, arr, arr);
        this.name = sm;
    }
    
    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext crx) {
        final LootTable csa4 = crx.getLootTable(this.name);
        csa4.getRandomItemsRaw(crx, consumer);
    }
    
    @Override
    public void validate(final ValidationContext csg) {
        if (csg.hasVisitedTable(this.name)) {
            csg.reportProblem("Table " + this.name + " is recursively called");
            return;
        }
        super.validate(csg);
        final LootTable csa3 = csg.resolveLootTable(this.name);
        if (csa3 == null) {
            csg.reportProblem("Unknown loot table called " + this.name);
        }
        else {
            csa3.validate(csg.enterTable("->{" + this.name + "}", this.name));
        }
    }
    
    public static Builder<?> lootTableReference(final ResourceLocation sm) {
        return LootPoolSingletonContainer.simpleBuilder((integer2, integer3, arr, arr) -> new LootTableReference(sm, integer2, integer3, arr, arr));
    }
    
    public static class Serializer extends LootPoolSingletonContainer.Serializer<LootTableReference> {
        public Serializer() {
            super(new ResourceLocation("loot_table"), LootTableReference.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final LootTableReference css, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, css, jsonSerializationContext);
            jsonObject.addProperty("name", css.name.toString());
        }
        
        @Override
        protected LootTableReference deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final int integer3, final int integer4, final LootItemCondition[] arr, final LootItemFunction[] arr) {
            final ResourceLocation sm8 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            return new LootTableReference(sm8, integer3, integer4, arr, arr, null);
        }
    }
}
