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.world.entity.npc.AbstractVillager;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class TradeTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return TradeTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final EntityPredicate.Composite b2 = EntityPredicate.Composite.fromJson(jsonObject, "villager", av);
        final ItemPredicate bo6 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(b, b2, bo6);
    }
    
    public void trigger(final ServerPlayer ze, final AbstractVillager bdk, final ItemStack bki) {
        final LootContext dat5 = EntityPredicate.createContext(ze, bdk);
        this.trigger(ze, a -> a.matches(dat5, bki));
    }
    
    static {
        ID = new ResourceLocation("villager_trade");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate.Composite villager;
        private final ItemPredicate item;
        
        public TriggerInstance(final EntityPredicate.Composite b1, final EntityPredicate.Composite b2, final ItemPredicate bo) {
            super(TradeTrigger.ID, b1);
            this.villager = b2;
            this.item = bo;
        }
        
        public static TriggerInstance tradedWithVillager() {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, ItemPredicate.ANY);
        }
        
        public boolean matches(final LootContext dat, final ItemStack bki) {
            return this.villager.matches(dat) && this.item.matches(bki);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("item", this.item.serializeToJson());
            jsonObject3.add("villager", this.villager.toJson(cg));
            return jsonObject3;
        }
    }
}
