| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import net.minecraft.entity.EntityCreature;
|
| 4 | import net.minecraft.entity.EntityLivingBase;
|
| 5 | import net.minecraft.util.Vec3;
|
| 6 |
|
| 7 | public class EntityAIMoveTowardsTarget extends EntityAIBase
|
| 8 | {
|
| 9 | private EntityCreature theEntity;
|
| 10 | private EntityLivingBase targetEntity;
|
| 11 | private double movePosX;
|
| 12 | private double movePosY;
|
| 13 | private double movePosZ;
|
| 14 | private double speed;
|
| 15 |
|
| 16 | /**
|
| 17 | * If the distance to the target entity is further than this, this AI task will not run.
|
| 18 | */
|
| 19 | private float maxTargetDistance;
|
| 20 |
|
| 21 | public EntityAIMoveTowardsTarget(EntityCreature creature, double speedIn, float targetMaxDistance)
|
| 22 | {
|
| 23 | this.theEntity = creature;
|
| 24 | this.speed = speedIn;
|
| 25 | this.maxTargetDistance = targetMaxDistance;
|
| 26 | this.setMutexBits(1);
|
| 27 | }
|
| 28 |
|
| 29 | /**
|
| 30 | * Returns whether the EntityAIBase should begin execution.
|
| 31 | */
|
| 32 | public boolean shouldExecute()
|
| 33 | {
|
| 34 | this.targetEntity = this.theEntity.getAttackTarget();
|
| 35 |
|
| 36 | if (this.targetEntity == null)
|
| 37 | {
|
| 38 | return false;
|
| 39 | }
|
| 40 | else if (this.targetEntity.getDistanceSqToEntity(this.theEntity) > (double)(this.maxTargetDistance * this.maxTargetDistance))
|
| 41 | {
|
| 42 | return false;
|
| 43 | }
|
| 44 | else
|
| 45 | {
|
| 46 | Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockTowards(this.theEntity, 16, 7, new Vec3(this.targetEntity.posX, this.targetEntity.posY, this.targetEntity.posZ));
|
| 47 |
|
| 48 | if (vec3 == null)
|
| 49 | {
|
| 50 | return false;
|
| 51 | }
|
| 52 | else
|
| 53 | {
|
| 54 | this.movePosX = vec3.xCoord;
|
| 55 | this.movePosY = vec3.yCoord;
|
| 56 | this.movePosZ = vec3.zCoord;
|
| 57 | return true;
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 64 | */
|
| 65 | public boolean continueExecuting()
|
| 66 | {
|
| 67 | return !this.theEntity.getNavigator().noPath() && this.targetEntity.isEntityAlive() && this.targetEntity.getDistanceSqToEntity(this.theEntity) < (double)(this.maxTargetDistance * this.maxTargetDistance);
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Resets the task
|
| 72 | */
|
| 73 | public void resetTask()
|
| 74 | {
|
| 75 | this.targetEntity = null;
|
| 76 | }
|
| 77 |
|
| 78 | /**
|
| 79 | * Execute a one shot task or start executing a continuous task
|
| 80 | */
|
| 81 | public void startExecuting()
|
| 82 | {
|
| 83 | this.theEntity.getNavigator().tryMoveToXYZ(this.movePosX, this.movePosY, this.movePosZ, this.speed);
|
| 84 | }
|
| 85 | }
|
| 86 |
|