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 uh, final int integer2, final int integer3, final LootItemCondition[] arr, final LootItemFunction[] arr) {
        super(integer2, integer3, arr, arr);
        this.name = uh;
    }
    
    @Override
    public LootPoolEntryType getType() {
        return LootPoolEntries.REFERENCE;
    }
    
    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext dat) {
        final LootTable daw4 = dat.getLootTable(this.name);
        daw4.getRandomItemsRaw(dat, consumer);
    }
    
    @Override
    public void validate(final ValidationContext dbe) {
        if (dbe.hasVisitedTable(this.name)) {
            dbe.reportProblem("Table " + this.name + " is recursively called");
            return;
        }
        super.validate(dbe);
        final LootTable daw3 = dbe.resolveLootTable(this.name);
        if (daw3 == null) {
            dbe.reportProblem("Unknown loot table called " + this.name);
        }
        else {
            daw3.validate(dbe.enterTable("->{" + this.name + "}", this.name));
        }
    }
    
    public static Builder<?> lootTableReference(final ResourceLocation uh) {
        return LootPoolSingletonContainer.simpleBuilder((integer2, integer3, arr, arr) -> new LootTableReference(uh, integer2, integer3, arr, arr));
    }
    
    public static class Serializer extends LootPoolSingletonContainer.Serializer<LootTableReference> {
        @Override
        public void serialize(final JsonObject jsonObject, final LootTableReference dbr, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dbr, jsonSerializationContext);
            jsonObject.addProperty("name", dbr.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 uh8 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            return new LootTableReference(uh8, integer3, integer4, arr, arr, null);
        }
    }
}
