package net.minecraft.advancements.critereon;

import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class PlayerInteractTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return PlayerInteractTrigger.ID;
    }
    
    @Override
    protected TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final ItemPredicate bo5 = ItemPredicate.fromJson(jsonObject.get("item"));
        final EntityPredicate.Composite b2 = EntityPredicate.Composite.fromJson(jsonObject, "entity", av);
        return new TriggerInstance(b, bo5, b2);
    }
    
    public void trigger(final ServerPlayer ze, final ItemStack bki, final Entity aom) {
        final LootContext dat5 = EntityPredicate.createContext(ze, aom);
        this.trigger(ze, a -> a.matches(bki, dat5));
    }
    
    static {
        ID = new ResourceLocation("player_interacted_with_entity");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ItemPredicate item;
        private final EntityPredicate.Composite entity;
        
        public TriggerInstance(final EntityPredicate.Composite b1, final ItemPredicate bo, final EntityPredicate.Composite b3) {
            super(PlayerInteractTrigger.ID, b1);
            this.item = bo;
            this.entity = b3;
        }
        
        public static TriggerInstance itemUsedOnEntity(final EntityPredicate.Composite b1, final ItemPredicate.Builder a, final EntityPredicate.Composite b3) {
            return new TriggerInstance(b1, a.build(), b3);
        }
        
        public boolean matches(final ItemStack bki, final LootContext dat) {
            return this.item.matches(bki) && this.entity.matches(dat);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("item", this.item.serializeToJson());
            jsonObject3.add("entity", this.entity.toJson(cg));
            return jsonObject3;
        }
    }
}
