001    package net.minecraft.src;
002    
003    public class EntityAIWatchClosest extends EntityAIBase
004    {
005        private EntityLiving theWatcher;
006    
007        /** The closest entity which is being watched by this one. */
008        protected Entity closestEntity;
009        private float field_75333_c;
010        private int lookTime;
011        private float field_75331_e;
012        private Class watchedClass;
013    
014        public EntityAIWatchClosest(EntityLiving par1EntityLiving, Class par2Class, float par3)
015        {
016            this.theWatcher = par1EntityLiving;
017            this.watchedClass = par2Class;
018            this.field_75333_c = par3;
019            this.field_75331_e = 0.02F;
020            this.setMutexBits(2);
021        }
022    
023        public EntityAIWatchClosest(EntityLiving par1EntityLiving, Class par2Class, float par3, float par4)
024        {
025            this.theWatcher = par1EntityLiving;
026            this.watchedClass = par2Class;
027            this.field_75333_c = par3;
028            this.field_75331_e = par4;
029            this.setMutexBits(2);
030        }
031    
032        /**
033         * Returns whether the EntityAIBase should begin execution.
034         */
035        public boolean shouldExecute()
036        {
037            if (this.theWatcher.getRNG().nextFloat() >= this.field_75331_e)
038            {
039                return false;
040            }
041            else
042            {
043                if (this.watchedClass == EntityPlayer.class)
044                {
045                    this.closestEntity = this.theWatcher.worldObj.getClosestPlayerToEntity(this.theWatcher, (double)this.field_75333_c);
046                }
047                else
048                {
049                    this.closestEntity = this.theWatcher.worldObj.findNearestEntityWithinAABB(this.watchedClass, this.theWatcher.boundingBox.expand((double)this.field_75333_c, 3.0D, (double)this.field_75333_c), this.theWatcher);
050                }
051    
052                return this.closestEntity != null;
053            }
054        }
055    
056        /**
057         * Returns whether an in-progress EntityAIBase should continue executing
058         */
059        public boolean continueExecuting()
060        {
061            return !this.closestEntity.isEntityAlive() ? false : (this.theWatcher.getDistanceSqToEntity(this.closestEntity) > (double)(this.field_75333_c * this.field_75333_c) ? false : this.lookTime > 0);
062        }
063    
064        /**
065         * Execute a one shot task or start executing a continuous task
066         */
067        public void startExecuting()
068        {
069            this.lookTime = 40 + this.theWatcher.getRNG().nextInt(40);
070        }
071    
072        /**
073         * Resets the task
074         */
075        public void resetTask()
076        {
077            this.closestEntity = null;
078        }
079    
080        /**
081         * Updates the task
082         */
083        public void updateTask()
084        {
085            this.theWatcher.getLookHelper().setLookPosition(this.closestEntity.posX, this.closestEntity.posY + (double)this.closestEntity.getEyeHeight(), this.closestEntity.posZ, 10.0F, (float)this.theWatcher.getVerticalFaceSpeed());
086            --this.lookTime;
087        }
088    }