package net.minecraft.advancements.criterion;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.advancements.ICriterionTrigger;
import net.minecraft.advancements.PlayerAdvancements;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;

public class UsedEnderEyeTrigger implements ICriterionTrigger<UsedEnderEyeTrigger.Instance> {
   private static final ResourceLocation ID = new ResourceLocation("used_ender_eye");
   private final Map<PlayerAdvancements, UsedEnderEyeTrigger.Listeners> listeners = Maps.newHashMap();

   public ResourceLocation getId() {
      return ID;
   }

   public void addListener(PlayerAdvancements playerAdvancementsIn, ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener) {
      UsedEnderEyeTrigger.Listeners usedendereyetrigger$listeners = this.listeners.get(playerAdvancementsIn);
      if (usedendereyetrigger$listeners == null) {
         usedendereyetrigger$listeners = new UsedEnderEyeTrigger.Listeners(playerAdvancementsIn);
         this.listeners.put(playerAdvancementsIn, usedendereyetrigger$listeners);
      }

      usedendereyetrigger$listeners.add(listener);
   }

   public void removeListener(PlayerAdvancements playerAdvancementsIn, ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener) {
      UsedEnderEyeTrigger.Listeners usedendereyetrigger$listeners = this.listeners.get(playerAdvancementsIn);
      if (usedendereyetrigger$listeners != null) {
         usedendereyetrigger$listeners.remove(listener);
         if (usedendereyetrigger$listeners.isEmpty()) {
            this.listeners.remove(playerAdvancementsIn);
         }
      }

   }

   public void removeAllListeners(PlayerAdvancements playerAdvancementsIn) {
      this.listeners.remove(playerAdvancementsIn);
   }

   public UsedEnderEyeTrigger.Instance deserializeInstance(JsonObject json, JsonDeserializationContext context) {
      MinMaxBounds.FloatBound minmaxbounds$floatbound = MinMaxBounds.FloatBound.fromJson(json.get("distance"));
      return new UsedEnderEyeTrigger.Instance(minmaxbounds$floatbound);
   }

   public void trigger(ServerPlayerEntity player, BlockPos pos) {
      UsedEnderEyeTrigger.Listeners usedendereyetrigger$listeners = this.listeners.get(player.getAdvancements());
      if (usedendereyetrigger$listeners != null) {
         double d0 = player.posX - (double)pos.getX();
         double d1 = player.posZ - (double)pos.getZ();
         usedendereyetrigger$listeners.trigger(d0 * d0 + d1 * d1);
      }

   }

   public static class Instance extends CriterionInstance {
      private final MinMaxBounds.FloatBound distance;

      public Instance(MinMaxBounds.FloatBound p_i49730_1_) {
         super(UsedEnderEyeTrigger.ID);
         this.distance = p_i49730_1_;
      }

      public boolean test(double distanceSq) {
         return this.distance.testSquared(distanceSq);
      }
   }

   static class Listeners {
      private final PlayerAdvancements playerAdvancements;
      private final Set<ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance>> listeners = Sets.newHashSet();

      public Listeners(PlayerAdvancements playerAdvancementsIn) {
         this.playerAdvancements = playerAdvancementsIn;
      }

      public boolean isEmpty() {
         return this.listeners.isEmpty();
      }

      public void add(ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener) {
         this.listeners.add(listener);
      }

      public void remove(ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener) {
         this.listeners.remove(listener);
      }

      public void trigger(double distanceSq) {
         List<ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance>> list = null;

         for(ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener : this.listeners) {
            if (listener.getCriterionInstance().test(distanceSq)) {
               if (list == null) {
                  list = Lists.newArrayList();
               }

               list.add(listener);
            }
         }

         if (list != null) {
            for(ICriterionTrigger.Listener<UsedEnderEyeTrigger.Instance> listener1 : list) {
               listener1.grantCriterion(this.playerAdvancements);
            }
         }

      }
   }
}