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

import java.util.Arrays;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Lists;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntry;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.nbt.Tag;
import net.minecraft.world.ContainerHelper;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.core.NonNullList;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import java.util.Collection;
import com.google.common.collect.ImmutableList;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntryContainer;
import java.util.List;

public class SetContainerContents extends LootItemConditionalFunction {
    private final List<LootPoolEntryContainer> entries;
    
    private SetContainerContents(final LootItemCondition[] arr, final List<LootPoolEntryContainer> list) {
        super(arr);
        this.entries = ImmutableList.copyOf(list);
    }
    
    public ItemStack run(final ItemStack ben, final LootContext crx) {
        if (ben.isEmpty()) {
            return ben;
        }
        final NonNullList<ItemStack> fy4 = NonNullList.<ItemStack>create();
        this.entries.forEach(csq -> csq.expand(crx, csp -> csp.createItemStack(LootTable.createStackSplitter(fy4::add), crx)));
        final CompoundTag jt5 = new CompoundTag();
        ContainerHelper.saveAllItems(jt5, fy4);
        final CompoundTag jt6 = ben.getOrCreateTag();
        jt6.put("BlockEntityTag", jt5.merge(jt6.getCompound("BlockEntityTag")));
        return ben;
    }
    
    @Override
    public void validate(final ValidationContext csg) {
        super.validate(csg);
        for (int integer3 = 0; integer3 < this.entries.size(); ++integer3) {
            this.entries.get(integer3).validate(csg.forChild(".entry[" + integer3 + "]"));
        }
    }
    
    public static Builder setContents() {
        return new Builder();
    }
    
    public static class Builder extends LootItemConditionalFunction.Builder<Builder> {
        private final List<LootPoolEntryContainer> entries;
        
        public Builder() {
            this.entries = Lists.newArrayList();
        }
        
        @Override
        protected Builder getThis() {
            return this;
        }
        
        public Builder withEntry(final LootPoolEntryContainer.Builder<?> a) {
            this.entries.add(a.build());
            return this;
        }
        
        @Override
        public LootItemFunction build() {
            return new SetContainerContents(this.getConditions(), this.entries, null);
        }
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetContainerContents> {
        protected Serializer() {
            super(new ResourceLocation("set_contents"), SetContainerContents.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final SetContainerContents ctm, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, ctm, jsonSerializationContext);
            jsonObject.add("entries", jsonSerializationContext.serialize(ctm.entries));
        }
        
        @Override
        public SetContainerContents deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final LootPoolEntryContainer[] arr2 = GsonHelper.<LootPoolEntryContainer[]>getAsObject(jsonObject, "entries", jsonDeserializationContext, LootPoolEntryContainer[].class);
            return new SetContainerContents(arr, Arrays.<LootPoolEntryContainer>asList(arr2), null);
        }
    }
}
