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.entity.animal.Animal;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class TameAnimalTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return TameAnimalTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final EntityPredicate bb4 = EntityPredicate.fromJson(jsonObject.get("entity"));
        return new TriggerInstance(bb4);
    }
    
    public void trigger(final ServerPlayer xe, final Animal atc) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, atc));
    }
    
    static {
        ID = new ResourceLocation("tame_animal");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate entity;
        
        public TriggerInstance(final EntityPredicate bb) {
            super(TameAnimalTrigger.ID);
            this.entity = bb;
        }
        
        public static TriggerInstance tamedAnimal() {
            return new TriggerInstance(EntityPredicate.ANY);
        }
        
        public static TriggerInstance tamedAnimal(final EntityPredicate bb) {
            return new TriggerInstance(bb);
        }
        
        public boolean matches(final ServerPlayer xe, final Animal atc) {
            return this.entity.matches(xe, atc);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("entity", this.entity.serializeToJson());
            return jsonObject2;
        }
    }
}
