package net.minecraft.advancements.critereon;

import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.world.level.storage.loot.LootContext;
import java.util.List;
import net.minecraft.world.entity.Entity;
import java.util.Collection;
import net.minecraft.server.level.ServerPlayer;
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;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final EntityPredicate.Composite[] arr5 = EntityPredicate.Composite.fromJsonArray(jsonObject, "victims", av);
        return new TriggerInstance(b, arr5);
    }
    
    public void trigger(final ServerPlayer ze, final Collection<? extends Entity> collection) {
        final List<LootContext> list4 = collection.stream().map(aom -> EntityPredicate.createContext(ze, aom)).collect(Collectors.toList());
        this.trigger(ze, a -> a.matches(list4));
    }
    
    static {
        ID = new ResourceLocation("channeled_lightning");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate.Composite[] victims;
        
        public TriggerInstance(final EntityPredicate.Composite b, final EntityPredicate.Composite[] arr) {
            super(ChanneledLightningTrigger.ID, b);
            this.victims = arr;
        }
        
        public static TriggerInstance channeledLightning(final EntityPredicate... arr) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, Stream.<EntityPredicate>of(arr).map(EntityPredicate.Composite::wrap).<EntityPredicate.Composite>toArray(EntityPredicate.Composite[]::new));
        }
        
        public boolean matches(final Collection<? extends LootContext> collection) {
            for (final EntityPredicate.Composite b6 : this.victims) {
                boolean boolean7 = false;
                for (final LootContext dat9 : collection) {
                    if (b6.matches(dat9)) {
                        boolean7 = true;
                        break;
                    }
                }
                if (!boolean7) {
                    return false;
                }
            }
            return true;
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("victims", EntityPredicate.Composite.toJson(this.victims, cg));
            return jsonObject3;
        }
    }
}
