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