package net.minecraft.advancements.critereon;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class KilledTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private final ResourceLocation id;
    
    public KilledTrigger(final ResourceLocation uh) {
        this.id = uh;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        return new TriggerInstance(this.id, b, EntityPredicate.Composite.fromJson(jsonObject, "entity", av), DamageSourcePredicate.fromJson(jsonObject.get("killing_blow")));
    }
    
    public void trigger(final ServerPlayer ze, final Entity aom, final DamageSource anw) {
        final LootContext dat5 = EntityPredicate.createContext(ze, aom);
        this.trigger(ze, a -> a.matches(ze, dat5, anw));
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate.Composite entityPredicate;
        private final DamageSourcePredicate killingBlow;
        
        public TriggerInstance(final ResourceLocation uh, final EntityPredicate.Composite b2, final EntityPredicate.Composite b3, final DamageSourcePredicate au) {
            super(uh, b2);
            this.entityPredicate = b3;
            this.killingBlow = au;
        }
        
        public static TriggerInstance playerKilledEntity(final EntityPredicate.Builder a) {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, EntityPredicate.Composite.ANY, EntityPredicate.Composite.wrap(a.build()), DamageSourcePredicate.ANY);
        }
        
        public static TriggerInstance playerKilledEntity() {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, DamageSourcePredicate.ANY);
        }
        
        public static TriggerInstance playerKilledEntity(final EntityPredicate.Builder a, final DamageSourcePredicate.Builder a) {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, EntityPredicate.Composite.ANY, EntityPredicate.Composite.wrap(a.build()), a.build());
        }
        
        public static TriggerInstance entityKilledPlayer() {
            return new TriggerInstance(CriteriaTriggers.ENTITY_KILLED_PLAYER.id, EntityPredicate.Composite.ANY, EntityPredicate.Composite.ANY, DamageSourcePredicate.ANY);
        }
        
        public boolean matches(final ServerPlayer ze, final LootContext dat, final DamageSource anw) {
            return this.killingBlow.matches(ze, anw) && this.entityPredicate.matches(dat);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("entity", this.entityPredicate.toJson(cg));
            jsonObject3.add("killing_blow", this.killingBlow.serializeToJson());
            return jsonObject3;
        }
    }
}
