| 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.Entity;
|
| 7 | import net.minecraft.entity.EntityCreature;
|
| 8 | import net.minecraft.entity.EntityLiving;
|
| 9 | import net.minecraft.entity.EntityLivingBase;
|
| 10 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 11 | import net.minecraft.entity.ai.attributes.IAttributeInstance;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 14 | import net.minecraft.scoreboard.Team;
|
| 15 | import org.apache.logging.log4j.LogManager;
|
| 16 | import org.apache.logging.log4j.Logger;
|
| 17 |
|
| 18 | public class EntityAIFindEntityNearestPlayer extends EntityAIBase
|
| 19 | {
|
| 20 | private static final Logger LOGGER = LogManager.getLogger();
|
| 21 |
|
| 22 | /** The entity that use this AI */
|
| 23 | private EntityLiving entityLiving;
|
| 24 | private final Predicate<Entity> predicate;
|
| 25 |
|
| 26 | /** Used to compare two entities */
|
| 27 | private final EntityAINearestAttackableTarget.Sorter sorter;
|
| 28 |
|
| 29 | /** The current target */
|
| 30 | private EntityLivingBase entityTarget;
|
| 31 |
|
| 32 | public EntityAIFindEntityNearestPlayer(EntityLiving entityLivingIn)
|
| 33 | {
|
| 34 | this.entityLiving = entityLivingIn;
|
| 35 |
|
| 36 | if (entityLivingIn instanceof EntityCreature)
|
| 37 | {
|
| 38 | LOGGER.warn("Use NearestAttackableTargetGoal.class for PathfinerMob mobs!");
|
| 39 | }
|
| 40 |
|
| 41 | this.predicate = new Predicate<Entity>()
|
| 42 | {
|
| 43 | public boolean apply(Entity p_apply_1_)
|
| 44 | {
|
| 45 | if (!(p_apply_1_ instanceof EntityPlayer))
|
| 46 | {
|
| 47 | return false;
|
| 48 | }
|
| 49 | else if (((EntityPlayer)p_apply_1_).capabilities.disableDamage)
|
| 50 | {
|
| 51 | return false;
|
| 52 | }
|
| 53 | else
|
| 54 | {
|
| 55 | double d0 = EntityAIFindEntityNearestPlayer.this.maxTargetRange();
|
| 56 |
|
| 57 | if (p_apply_1_.isSneaking())
|
| 58 | {
|
| 59 | d0 *= 0.800000011920929D;
|
| 60 | }
|
| 61 |
|
| 62 | if (p_apply_1_.isInvisible())
|
| 63 | {
|
| 64 | float f = ((EntityPlayer)p_apply_1_).getArmorVisibility();
|
| 65 |
|
| 66 | if (f < 0.1F)
|
| 67 | {
|
| 68 | f = 0.1F;
|
| 69 | }
|
| 70 |
|
| 71 | d0 *= (double)(0.7F * f);
|
| 72 | }
|
| 73 |
|
| 74 | return (double)p_apply_1_.getDistanceToEntity(EntityAIFindEntityNearestPlayer.this.entityLiving) > d0 ? false : EntityAITarget.isSuitableTarget(EntityAIFindEntityNearestPlayer.this.entityLiving, (EntityLivingBase)p_apply_1_, false, true);
|
| 75 | }
|
| 76 | }
|
| 77 | };
|
| 78 | this.sorter = new EntityAINearestAttackableTarget.Sorter(entityLivingIn);
|
| 79 | }
|
| 80 |
|
| 81 | /**
|
| 82 | * Returns whether the EntityAIBase should begin execution.
|
| 83 | */
|
| 84 | public boolean shouldExecute()
|
| 85 | {
|
| 86 | double d0 = this.maxTargetRange();
|
| 87 | List<EntityPlayer> list = this.entityLiving.worldObj.<EntityPlayer>getEntitiesWithinAABB(EntityPlayer.class, this.entityLiving.getEntityBoundingBox().expand(d0, 4.0D, d0), this.predicate);
|
| 88 | Collections.sort(list, this.sorter);
|
| 89 |
|
| 90 | if (list.isEmpty())
|
| 91 | {
|
| 92 | return false;
|
| 93 | }
|
| 94 | else
|
| 95 | {
|
| 96 | this.entityTarget = (EntityLivingBase)list.get(0);
|
| 97 | return true;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 103 | */
|
| 104 | public boolean continueExecuting()
|
| 105 | {
|
| 106 | EntityLivingBase entitylivingbase = this.entityLiving.getAttackTarget();
|
| 107 |
|
| 108 | if (entitylivingbase == null)
|
| 109 | {
|
| 110 | return false;
|
| 111 | }
|
| 112 | else if (!entitylivingbase.isEntityAlive())
|
| 113 | {
|
| 114 | return false;
|
| 115 | }
|
| 116 | else if (entitylivingbase instanceof EntityPlayer && ((EntityPlayer)entitylivingbase).capabilities.disableDamage)
|
| 117 | {
|
| 118 | return false;
|
| 119 | }
|
| 120 | else
|
| 121 | {
|
| 122 | Team team = this.entityLiving.getTeam();
|
| 123 | Team team1 = entitylivingbase.getTeam();
|
| 124 |
|
| 125 | if (team != null && team1 == team)
|
| 126 | {
|
| 127 | return false;
|
| 128 | }
|
| 129 | else
|
| 130 | {
|
| 131 | double d0 = this.maxTargetRange();
|
| 132 | return this.entityLiving.getDistanceSqToEntity(entitylivingbase) > d0 * d0 ? false : !(entitylivingbase instanceof EntityPlayerMP) || !((EntityPlayerMP)entitylivingbase).theItemInWorldManager.isCreative();
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | /**
|
| 138 | * Execute a one shot task or start executing a continuous task
|
| 139 | */
|
| 140 | public void startExecuting()
|
| 141 | {
|
| 142 | this.entityLiving.setAttackTarget(this.entityTarget);
|
| 143 | super.startExecuting();
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * Resets the task
|
| 148 | */
|
| 149 | public void resetTask()
|
| 150 | {
|
| 151 | this.entityLiving.setAttackTarget((EntityLivingBase)null);
|
| 152 | super.startExecuting();
|
| 153 | }
|
| 154 |
|
| 155 | /**
|
| 156 | * Return the max target range of the entiity (16 by default)
|
| 157 | */
|
| 158 | protected double maxTargetRange()
|
| 159 | {
|
| 160 | IAttributeInstance iattributeinstance = this.entityLiving.getEntityAttribute(SharedMonsterAttributes.followRange);
|
| 161 | return iattributeinstance == null ? 16.0D : iattributeinstance.getAttributeValue();
|
| 162 | }
|
| 163 | }
|
| 164 |
|