package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.world.entity.Entity;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
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;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final EntityPredicate bb4 = EntityPredicate.fromJson(jsonObject.get("villager"));
        final ItemPredicate bj5 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(bb4, bj5);
    }
    
    public void trigger(final ServerPlayer xe, final AbstractVillager axs, final ItemStack ben) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, axs, ben));
    }
    
    static {
        ID = new ResourceLocation("villager_trade");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate villager;
        private final ItemPredicate item;
        
        public TriggerInstance(final EntityPredicate bb, final ItemPredicate bj) {
            super(TradeTrigger.ID);
            this.villager = bb;
            this.item = bj;
        }
        
        public static TriggerInstance tradedWithVillager() {
            return new TriggerInstance(EntityPredicate.ANY, ItemPredicate.ANY);
        }
        
        public boolean matches(final ServerPlayer xe, final AbstractVillager axs, final ItemStack ben) {
            return this.villager.matches(xe, axs) && this.item.matches(ben);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("item", this.item.serializeToJson());
            jsonObject2.add("villager", this.villager.serializeToJson());
            return jsonObject2;
        }
    }
}
