package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class KilledTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private final ResourceLocation id;
    
    public KilledTrigger(final ResourceLocation sm) {
        this.id = sm;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        return new TriggerInstance(this.id, EntityPredicate.fromJson(jsonObject.get("entity")), DamageSourcePredicate.fromJson(jsonObject.get("killing_blow")));
    }
    
    public void trigger(final ServerPlayer xe, final Entity akq, final DamageSource ajz) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, akq, ajz));
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate entityPredicate;
        private final DamageSourcePredicate killingBlow;
        
        public TriggerInstance(final ResourceLocation sm, final EntityPredicate bb, final DamageSourcePredicate as) {
            super(sm);
            this.entityPredicate = bb;
            this.killingBlow = as;
        }
        
        public static TriggerInstance playerKilledEntity(final EntityPredicate.Builder a) {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, a.build(), DamageSourcePredicate.ANY);
        }
        
        public static TriggerInstance playerKilledEntity() {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, EntityPredicate.ANY, DamageSourcePredicate.ANY);
        }
        
        public static TriggerInstance playerKilledEntity(final EntityPredicate.Builder a, final DamageSourcePredicate.Builder a) {
            return new TriggerInstance(CriteriaTriggers.PLAYER_KILLED_ENTITY.id, a.build(), a.build());
        }
        
        public static TriggerInstance entityKilledPlayer() {
            return new TriggerInstance(CriteriaTriggers.ENTITY_KILLED_PLAYER.id, EntityPredicate.ANY, DamageSourcePredicate.ANY);
        }
        
        public boolean matches(final ServerPlayer xe, final Entity akq, final DamageSource ajz) {
            return this.killingBlow.matches(xe, ajz) && this.entityPredicate.matches(xe, akq);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("entity", this.entityPredicate.serializeToJson());
            jsonObject2.add("killing_blow", this.killingBlow.serializeToJson());
            return jsonObject2;
        }
    }
}
