content: package com.github.alexthe666.iceandfire.entity.ai;
  
  import com.github.alexthe666.iceandfire.entity.EntityMyrmexBase;
  import com.github.alexthe666.iceandfire.entity.EntityMyrmexSoldier;
  import net.minecraft.world.entity.Entity;
  import net.minecraft.world.entity.ai.goal.target.TargetGoal;
  import net.minecraft.world.phys.AABB;
  
  import java.util.Comparator;
  import java.util.EnumSet;
  import java.util.List;
  import java.util.function.Predicate;
  
  public class MyrmexAIFindGaurdingEntity<T extends EntityMyrmexBase> extends TargetGoal {
      protected final DragonAITargetItems.Sorter theNearestAttackableTargetSorter;
      protected final Predicate<? super EntityMyrmexBase> targetEntitySelector;
      public EntityMyrmexSoldier myrmex;
      protected EntityMyrmexBase targetEntity;
  
      public MyrmexAIFindGaurdingEntity(EntityMyrmexSoldier myrmex) {
          super(myrmex, false, false);
          this.theNearestAttackableTargetSorter = new DragonAITargetItems.Sorter(myrmex);
          this.targetEntitySelector = new Predicate<EntityMyrmexBase>() {
              @Override
              public boolean test(EntityMyrmexBase myrmex) {
                  return !(myrmex instanceof EntityMyrmexSoldier) && myrmex.getGrowthStage() > 1
                      && EntityMyrmexBase.haveSameHive(MyrmexAIFindGaurdingEntity.this.myrmex, myrmex)
                      && !myrmex.isBeingGuarded && myrmex.needsGaurding();
              }
          };
          this.myrmex = myrmex;
          this.setFlags(EnumSet.of(Flag.MOVE));
      }
  
      @Override
      public boolean canUse() {
          if (!this.myrmex.canMove() || this.myrmex.getTarget() != null || this.myrmex.guardingEntity != null) {
              return false;
          }
          List<EntityMyrmexBase> list = this.mob.level.getEntitiesOfClass(EntityMyrmexBase.class, this.getTargetableArea(this.getFollowDistance()), this.targetEntitySelector);
          if (list.isEmpty()) {
              return false;
          } else {
              list.sort(this.theNearestAttackableTargetSorter);
              this.myrmex.guardingEntity = list.get(0);
              return true;
          }
      }
  
      protected AABB getTargetableArea(double targetDistance) {
          return this.mob.getBoundingBox().inflate(targetDistance, 4.0D, targetDistance);
      }
  
      @Override
      public boolean canContinueToUse() {
          return false;
      }
  
      public static class Sorter implements Comparator<Entity> {
          private final Entity theEntity;
  
          public Sorter(EntityMyrmexBase theEntityIn) {
              this.theEntity = theEntityIn;
          }
  
          @Override
          public int compare(Entity p_compare_1_, Entity p_compare_2_) {
              final double d0 = this.theEntity.distanceToSqr(p_compare_1_);
              final double d1 = this.theEntity.distanceToSqr(p_compare_2_);
              return Double.compare(d0, d1);
          }
      }
  }