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

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.LootTable;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.resources.ResourceLocation;

public class SetContainerLootTable extends LootItemConditionalFunction {
    private final ResourceLocation name;
    private final long seed;
    
    private SetContainerLootTable(final LootItemCondition[] arr, final ResourceLocation uh, final long long3) {
        super(arr);
        this.name = uh;
        this.seed = long3;
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.SET_LOOT_TABLE;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        if (bki.isEmpty()) {
            return bki;
        }
        final CompoundTag le4 = new CompoundTag();
        le4.putString("LootTable", this.name.toString());
        if (this.seed != 0L) {
            le4.putLong("LootTableSeed", this.seed);
        }
        bki.getOrCreateTag().put("BlockEntityTag", le4);
        return bki;
    }
    
    @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 class Serializer extends LootItemConditionalFunction.Serializer<SetContainerLootTable> {
        @Override
        public void serialize(final JsonObject jsonObject, final SetContainerLootTable dcn, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dcn, jsonSerializationContext);
            jsonObject.addProperty("name", dcn.name.toString());
            if (dcn.seed != 0L) {
                jsonObject.addProperty("seed", dcn.seed);
            }
        }
        
        @Override
        public SetContainerLootTable deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final ResourceLocation uh5 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            final long long6 = GsonHelper.getAsLong(jsonObject, "seed", 0L);
            return new SetContainerLootTable(arr, uh5, long6, null);
        }
    }
}
