package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import net.minecraft.world.level.ItemLike;
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 ConsumeItemTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return ConsumeItemTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        return new TriggerInstance(ItemPredicate.fromJson(jsonObject.get("item")));
    }
    
    public void trigger(final ServerPlayer xe, final ItemStack ben) {
        this.trigger(xe.getAdvancements(), a -> a.matches(ben));
    }
    
    static {
        ID = new ResourceLocation("consume_item");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ItemPredicate item;
        
        public TriggerInstance(final ItemPredicate bj) {
            super(ConsumeItemTrigger.ID);
            this.item = bj;
        }
        
        public static TriggerInstance usedItem() {
            return new TriggerInstance(ItemPredicate.ANY);
        }
        
        public static TriggerInstance usedItem(final ItemLike bjv) {
            return new TriggerInstance(new ItemPredicate(null, bjv.asItem(), MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, EnchantmentPredicate.NONE, EnchantmentPredicate.NONE, null, NbtPredicate.ANY));
        }
        
        public boolean matches(final ItemStack ben) {
            return this.item.matches(ben);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("item", this.item.serializeToJson());
            return jsonObject2;
        }
    }
}
