package net.minecraft.advancements.critereon;

import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.server.level.ServerPlayer;
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;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final EntityPredicate.Composite b2 = EntityPredicate.Composite.fromJson(jsonObject, "entity", av);
        return new TriggerInstance(b, b2);
    }
    
    public void trigger(final ServerPlayer ze, final Animal ayk) {
        final LootContext dat4 = EntityPredicate.createContext(ze, ayk);
        this.trigger(ze, a -> a.matches(dat4));
    }
    
    static {
        ID = new ResourceLocation("tame_animal");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate.Composite entity;
        
        public TriggerInstance(final EntityPredicate.Composite b1, final EntityPredicate.Composite b2) {
            super(TameAnimalTrigger.ID, b1);
            this.entity = b2;
        }
        
        public static TriggerInstance tamedAnimal() {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY);
        }
        
        public static TriggerInstance tamedAnimal(final EntityPredicate be) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, EntityPredicate.Composite.wrap(be));
        }
        
        public boolean matches(final LootContext dat) {
            return this.entity.matches(dat);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("entity", this.entity.toJson(cg));
            return jsonObject3;
        }
    }
}
