package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.world.entity.EntityType;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Sets;
import com.google.common.collect.Lists;
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 KilledByCrossbowTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return KilledByCrossbowTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final EntityPredicate[] arr4 = EntityPredicate.fromJsonArray(jsonObject.get("victims"));
        final MinMaxBounds.Ints d5 = MinMaxBounds.Ints.fromJson(jsonObject.get("unique_entity_types"));
        return new TriggerInstance(arr4, d5);
    }
    
    public void trigger(final ServerPlayer xe, final Collection<Entity> collection, final int integer) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, collection, integer));
    }
    
    static {
        ID = new ResourceLocation("killed_by_crossbow");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final EntityPredicate[] victims;
        private final MinMaxBounds.Ints uniqueEntityTypes;
        
        public TriggerInstance(final EntityPredicate[] arr, final MinMaxBounds.Ints d) {
            super(KilledByCrossbowTrigger.ID);
            this.victims = arr;
            this.uniqueEntityTypes = d;
        }
        
        public static TriggerInstance crossbowKilled(final EntityPredicate.Builder... arr) {
            final EntityPredicate[] arr2 = new EntityPredicate[arr.length];
            for (int integer3 = 0; integer3 < arr.length; ++integer3) {
                final EntityPredicate.Builder a4 = arr[integer3];
                arr2[integer3] = a4.build();
            }
            return new TriggerInstance(arr2, MinMaxBounds.Ints.ANY);
        }
        
        public static TriggerInstance crossbowKilled(final MinMaxBounds.Ints d) {
            final EntityPredicate[] arr2 = new EntityPredicate[0];
            return new TriggerInstance(arr2, d);
        }
        
        public boolean matches(final ServerPlayer xe, final Collection<Entity> collection, final int integer) {
            if (this.victims.length > 0) {
                final List<Entity> list5 = Lists.newArrayList(collection);
                for (final EntityPredicate bb9 : this.victims) {
                    boolean boolean10 = false;
                    final Iterator<Entity> iterator11 = list5.iterator();
                    while (iterator11.hasNext()) {
                        final Entity akq12 = iterator11.next();
                        if (bb9.matches(xe, akq12)) {
                            iterator11.remove();
                            boolean10 = true;
                            break;
                        }
                    }
                    if (!boolean10) {
                        return false;
                    }
                }
            }
            if (this.uniqueEntityTypes != MinMaxBounds.Ints.ANY) {
                final Set<EntityType<?>> set5 = Sets.newHashSet();
                for (final Entity akq13 : collection) {
                    set5.add(akq13.getType());
                }
                return this.uniqueEntityTypes.matches(set5.size()) && this.uniqueEntityTypes.matches(integer);
            }
            return true;
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("victims", EntityPredicate.serializeArrayToJson(this.victims));
            jsonObject2.add("unique_entity_types", this.uniqueEntityTypes.serializeToJson());
            return jsonObject2;
        }
    }
}
