package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
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 PlayerHurtEntityTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return PlayerHurtEntityTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final DamagePredicate ar4 = DamagePredicate.fromJson(jsonObject.get("damage"));
        final EntityPredicate bb5 = EntityPredicate.fromJson(jsonObject.get("entity"));
        return new TriggerInstance(ar4, bb5);
    }
    
    public void trigger(final ServerPlayer xe, final Entity akq, final DamageSource ajz, final float float4, final float float5, final boolean boolean6) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, akq, ajz, float4, float5, boolean6));
    }
    
    static {
        ID = new ResourceLocation("player_hurt_entity");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final DamagePredicate damage;
        private final EntityPredicate entity;
        
        public TriggerInstance(final DamagePredicate ar, final EntityPredicate bb) {
            super(PlayerHurtEntityTrigger.ID);
            this.damage = ar;
            this.entity = bb;
        }
        
        public static TriggerInstance playerHurtEntity(final DamagePredicate.Builder a) {
            return new TriggerInstance(a.build(), EntityPredicate.ANY);
        }
        
        public boolean matches(final ServerPlayer xe, final Entity akq, final DamageSource ajz, final float float4, final float float5, final boolean boolean6) {
            return this.damage.matches(xe, ajz, float4, float5, boolean6) && this.entity.matches(xe, akq);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("damage", this.damage.serializeToJson());
            jsonObject2.add("entity", this.entity.serializeToJson());
            return jsonObject2;
        }
    }
}
