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.item.enchantment.EnchantmentHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.Entity;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.RandomValueBounds;

public class LootingEnchantFunction extends LootItemConditionalFunction {
    private final RandomValueBounds value;
    private final int limit;
    
    private LootingEnchantFunction(final LootItemCondition[] arr, final RandomValueBounds dbb, final int integer) {
        super(arr);
        this.value = dbb;
        this.limit = integer;
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.LOOTING_ENCHANT;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.KILLER_ENTITY);
    }
    
    private boolean hasLimit() {
        return this.limit > 0;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        final Entity aom4 = dat.<Entity>getParamOrNull(LootContextParams.KILLER_ENTITY);
        if (aom4 instanceof LivingEntity) {
            final int integer5 = EnchantmentHelper.getMobLooting((LivingEntity)aom4);
            if (integer5 == 0) {
                return bki;
            }
            final float float6 = integer5 * this.value.getFloat(dat.getRandom());
            bki.grow(Math.round(float6));
            if (this.hasLimit() && bki.getCount() > this.limit) {
                bki.setCount(this.limit);
            }
        }
        return bki;
    }
    
    public static Builder lootingMultiplier(final RandomValueBounds dbb) {
        return new Builder(dbb);
    }
    
    public static class Builder extends LootItemConditionalFunction.Builder<Builder> {
        private final RandomValueBounds count;
        private int limit;
        
        public Builder(final RandomValueBounds dbb) {
            this.limit = 0;
            this.count = dbb;
        }
        
        @Override
        protected Builder getThis() {
            return this;
        }
        
        public Builder setLimit(final int integer) {
            this.limit = integer;
            return this;
        }
        
        @Override
        public LootItemFunction build() {
            return new LootingEnchantFunction(this.getConditions(), this.count, this.limit, null);
        }
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<LootingEnchantFunction> {
        @Override
        public void serialize(final JsonObject jsonObject, final LootingEnchantFunction dck, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, dck, jsonSerializationContext);
            jsonObject.add("count", jsonSerializationContext.serialize(dck.value));
            if (dck.hasLimit()) {
                jsonObject.add("limit", jsonSerializationContext.serialize(dck.limit));
            }
        }
        
        @Override
        public LootingEnchantFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final int integer5 = GsonHelper.getAsInt(jsonObject, "limit", 0);
            return new LootingEnchantFunction(arr, GsonHelper.<RandomValueBounds>getAsObject(jsonObject, "count", jsonDeserializationContext, RandomValueBounds.class), integer5, null);
        }
    }
}
