package net.minecraft.advancements.critereon;

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

public class EffectsChangedTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return EffectsChangedTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final MobEffectsPredicate bs4 = MobEffectsPredicate.fromJson(jsonObject.get("effects"));
        return new TriggerInstance(bs4);
    }
    
    public void trigger(final ServerPlayer xe) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe));
    }
    
    static {
        ID = new ResourceLocation("effects_changed");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final MobEffectsPredicate effects;
        
        public TriggerInstance(final MobEffectsPredicate bs) {
            super(EffectsChangedTrigger.ID);
            this.effects = bs;
        }
        
        public static TriggerInstance hasEffects(final MobEffectsPredicate bs) {
            return new TriggerInstance(bs);
        }
        
        public boolean matches(final ServerPlayer xe) {
            return this.effects.matches(xe);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("effects", this.effects.serializeToJson());
            return jsonObject2;
        }
    }
}
