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

import com.google.gson.JsonDeserializationContext;
import net.minecraft.world.level.storage.loot.RandomIntGenerators;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
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.world.level.storage.loot.RandomIntGenerator;

public class SetItemCountFunction extends LootItemConditionalFunction {
    private final RandomIntGenerator value;
    
    private SetItemCountFunction(final LootItemCondition[] arr, final RandomIntGenerator daz) {
        super(arr);
        this.value = daz;
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.SET_COUNT;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        bki.setCount(this.value.getInt(dat.getRandom()));
        return bki;
    }
    
    public static Builder<?> setCount(final RandomIntGenerator daz) {
        return LootItemConditionalFunction.simpleBuilder(arr -> new SetItemCountFunction(arr, daz));
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SetItemCountFunction> {
        @Override
        public void serialize(final JsonObject jsonObject, final SetItemCountFunction dco, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dco, jsonSerializationContext);
            jsonObject.add("count", RandomIntGenerators.serialize(dco.value, jsonSerializationContext));
        }
        
        @Override
        public SetItemCountFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final RandomIntGenerator daz5 = RandomIntGenerators.deserialize(jsonObject.get("count"), jsonDeserializationContext);
            return new SetItemCountFunction(arr, daz5, null);
        }
    }
}
