| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.entity.EntityCreature;
|
| 7 | import net.minecraft.entity.EntityLiving;
|
| 8 | import net.minecraft.entity.EntityLivingBase;
|
| 9 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 10 | import net.minecraft.entity.ai.attributes.IAttributeInstance;
|
| 11 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 12 | import org.apache.logging.log4j.LogManager;
|
| 13 | import org.apache.logging.log4j.Logger;
|
| 14 |
|
| 15 | public class EntityAIFindEntityNearest extends EntityAIBase
|
| 16 | {
|
| 17 | private static final Logger LOGGER = LogManager.getLogger();
|
| 18 | private EntityLiving mob;
|
| 19 | private final Predicate<EntityLivingBase> field_179443_c;
|
| 20 | private final EntityAINearestAttackableTarget.Sorter field_179440_d;
|
| 21 | private EntityLivingBase target;
|
| 22 | private Class <? extends EntityLivingBase > field_179439_f;
|
| 23 |
|
| 24 | public EntityAIFindEntityNearest(EntityLiving mobIn, Class <? extends EntityLivingBase > p_i45884_2_)
|
| 25 | {
|
| 26 | this.mob = mobIn;
|
| 27 | this.field_179439_f = p_i45884_2_;
|
| 28 |
|
| 29 | if (mobIn instanceof EntityCreature)
|
| 30 | {
|
| 31 | LOGGER.warn("Use NearestAttackableTargetGoal.class for PathfinerMob mobs!");
|
| 32 | }
|
| 33 |
|
| 34 | this.field_179443_c = new Predicate<EntityLivingBase>()
|
| 35 | {
|
| 36 | public boolean apply(EntityLivingBase p_apply_1_)
|
| 37 | {
|
| 38 | double d0 = EntityAIFindEntityNearest.this.getFollowRange();
|
| 39 |
|
| 40 | if (p_apply_1_.isSneaking())
|
| 41 | {
|
| 42 | d0 *= 0.800000011920929D;
|
| 43 | }
|
| 44 |
|
| 45 | return p_apply_1_.isInvisible() ? false : ((double)p_apply_1_.getDistanceToEntity(EntityAIFindEntityNearest.this.mob) > d0 ? false : EntityAITarget.isSuitableTarget(EntityAIFindEntityNearest.this.mob, p_apply_1_, false, true));
|
| 46 | }
|
| 47 | };
|
| 48 | this.field_179440_d = new EntityAINearestAttackableTarget.Sorter(mobIn);
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Returns whether the EntityAIBase should begin execution.
|
| 53 | */
|
| 54 | public boolean shouldExecute()
|
| 55 | {
|
| 56 | double d0 = this.getFollowRange();
|
| 57 | List<EntityLivingBase> list = this.mob.worldObj.<EntityLivingBase>getEntitiesWithinAABB(this.field_179439_f, this.mob.getEntityBoundingBox().expand(d0, 4.0D, d0), this.field_179443_c);
|
| 58 | Collections.sort(list, this.field_179440_d);
|
| 59 |
|
| 60 | if (list.isEmpty())
|
| 61 | {
|
| 62 | return false;
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | this.target = (EntityLivingBase)list.get(0);
|
| 67 | return true;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | /**
|
| 72 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 73 | */
|
| 74 | public boolean continueExecuting()
|
| 75 | {
|
| 76 | EntityLivingBase entitylivingbase = this.mob.getAttackTarget();
|
| 77 |
|
| 78 | if (entitylivingbase == null)
|
| 79 | {
|
| 80 | return false;
|
| 81 | }
|
| 82 | else if (!entitylivingbase.isEntityAlive())
|
| 83 | {
|
| 84 | return false;
|
| 85 | }
|
| 86 | else
|
| 87 | {
|
| 88 | double d0 = this.getFollowRange();
|
| 89 | return this.mob.getDistanceSqToEntity(entitylivingbase) > d0 * d0 ? false : !(entitylivingbase instanceof EntityPlayerMP) || !((EntityPlayerMP)entitylivingbase).theItemInWorldManager.isCreative();
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Execute a one shot task or start executing a continuous task
|
| 95 | */
|
| 96 | public void startExecuting()
|
| 97 | {
|
| 98 | this.mob.setAttackTarget(this.target);
|
| 99 | super.startExecuting();
|
| 100 | }
|
| 101 |
|
| 102 | /**
|
| 103 | * Resets the task
|
| 104 | */
|
| 105 | public void resetTask()
|
| 106 | {
|
| 107 | this.mob.setAttackTarget((EntityLivingBase)null);
|
| 108 | super.startExecuting();
|
| 109 | }
|
| 110 |
|
| 111 | protected double getFollowRange()
|
| 112 | {
|
| 113 | IAttributeInstance iattributeinstance = this.mob.getEntityAttribute(SharedMonsterAttributes.followRange);
|
| 114 | return iattributeinstance == null ? 16.0D : iattributeinstance.getAttributeValue();
|
| 115 | }
|
| 116 | }
|
| 117 |
|