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 sm, final long long3) {
        super(arr);
        this.name = sm;
        this.seed = long3;
    }
    
    public ItemStack run(final ItemStack ben, final LootContext crx) {
        if (ben.isEmpty()) {
            return ben;
        }
        final CompoundTag jt4 = new CompoundTag();
        jt4.putString("LootTable", this.name.toString());
        if (this.seed != 0L) {
            jt4.putLong("LootTableSeed", this.seed);
        }
        ben.getOrCreateTag().put("BlockEntityTag", jt4);
        return ben;
    }
    
    @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 class Serializer extends LootItemConditionalFunction.Serializer<SetContainerLootTable> {
        protected Serializer() {
            super(new ResourceLocation("set_loot_table"), SetContainerLootTable.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final SetContainerLootTable ctn, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, ctn, jsonSerializationContext);
            jsonObject.addProperty("name", ctn.name.toString());
            if (ctn.seed != 0L) {
                jsonObject.addProperty("seed", ctn.seed);
            }
        }
        
        @Override
        public SetContainerLootTable deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final ResourceLocation sm5 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            final long long6 = GsonHelper.getAsLong(jsonObject, "seed", 0L);
            return new SetContainerLootTable(arr, sm5, long6, null);
        }
    }
}
