package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import java.util.Iterator;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.entity.Entity;
import java.util.Collection;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class ChanneledLightningTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return ChanneledLightningTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final EntityPredicate[] arr4 = EntityPredicate.fromJsonArray(jsonObject.get("victims"));
        return new TriggerInstance(arr4);
    }
    
    public void trigger(final ServerPlayer xe, final Collection<? extends Entity> collection) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, collection));
    }
    
    static {
        ID = new ResourceLocation("channeled_lightning");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate[] victims;
        
        public TriggerInstance(final EntityPredicate[] arr) {
            super(ChanneledLightningTrigger.ID);
            this.victims = arr;
        }
        
        public static TriggerInstance channeledLightning(final EntityPredicate... arr) {
            return new TriggerInstance(arr);
        }
        
        public boolean matches(final ServerPlayer xe, final Collection<? extends Entity> collection) {
            for (final EntityPredicate bb7 : this.victims) {
                boolean boolean8 = false;
                for (final Entity akq10 : collection) {
                    if (bb7.matches(xe, akq10)) {
                        boolean8 = true;
                        break;
                    }
                }
                if (!boolean8) {
                    return false;
                }
            }
            return true;
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("victims", EntityPredicate.serializeArrayToJson(this.victims));
            return jsonObject2;
        }
    }
}
