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.resources.ResourceLocation;
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.IntLimiter;

public class LimitCount extends LootItemConditionalFunction {
    private final IntLimiter limiter;
    
    private LimitCount(final LootItemCondition[] arr, final IntLimiter crw) {
        super(arr);
        this.limiter = crw;
    }
    
    public ItemStack run(final ItemStack ben, final LootContext crx) {
        final int integer4 = this.limiter.applyAsInt(ben.getCount());
        ben.setCount(integer4);
        return ben;
    }
    
    public static Builder<?> limitCount(final IntLimiter crw) {
        return LootItemConditionalFunction.simpleBuilder(arr -> new LimitCount(arr, crw));
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<LimitCount> {
        protected Serializer() {
            super(new ResourceLocation("limit_count"), LimitCount.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final LimitCount ctg, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, ctg, jsonSerializationContext);
            jsonObject.add("limit", jsonSerializationContext.serialize(ctg.limiter));
        }
        
        @Override
        public LimitCount deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final IntLimiter crw5 = GsonHelper.<IntLimiter>getAsObject(jsonObject, "limit", jsonDeserializationContext, IntLimiter.class);
            return new LimitCount(arr, crw5, null);
        }
    }
}
