package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.item.ItemStack;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class EnchantedItemTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return EnchantedItemTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final ItemPredicate bj4 = ItemPredicate.fromJson(jsonObject.get("item"));
        final MinMaxBounds.Ints d5 = MinMaxBounds.Ints.fromJson(jsonObject.get("levels"));
        return new TriggerInstance(bj4, d5);
    }
    
    public void trigger(final ServerPlayer xe, final ItemStack ben, final int integer) {
        this.trigger(xe.getAdvancements(), a -> a.matches(ben, integer));
    }
    
    static {
        ID = new ResourceLocation("enchanted_item");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ItemPredicate item;
        private final MinMaxBounds.Ints levels;
        
        public TriggerInstance(final ItemPredicate bj, final MinMaxBounds.Ints d) {
            super(EnchantedItemTrigger.ID);
            this.item = bj;
            this.levels = d;
        }
        
        public static TriggerInstance enchantedItem() {
            return new TriggerInstance(ItemPredicate.ANY, MinMaxBounds.Ints.ANY);
        }
        
        public boolean matches(final ItemStack ben, final int integer) {
            return this.item.matches(ben) && this.levels.matches(integer);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("item", this.item.serializeToJson());
            jsonObject2.add("levels", this.levels.serializeToJson());
            return jsonObject2;
        }
    }
}
