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.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 das) {
        super(arr);
        this.limiter = das;
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.LIMIT_COUNT;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        final int integer4 = this.limiter.applyAsInt(bki.getCount());
        bki.setCount(integer4);
        return bki;
    }
    
    public static Builder<?> limitCount(final IntLimiter das) {
        return LootItemConditionalFunction.simpleBuilder(arr -> new LimitCount(arr, das));
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<LimitCount> {
        @Override
        public void serialize(final JsonObject jsonObject, final LimitCount dcf, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dcf, jsonSerializationContext);
            jsonObject.add("limit", jsonSerializationContext.serialize(dcf.limiter));
        }
        
        @Override
        public LimitCount deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final IntLimiter das5 = GsonHelper.<IntLimiter>getAsObject(jsonObject, "limit", jsonDeserializationContext, IntLimiter.class);
            return new LimitCount(arr, das5, null);
        }
    }
}
