    001    package net.minecraft.src;
    002
    003    import java.util.Collections;
    004    import java.util.Iterator;
    005    import java.util.List;
    006
    007    public class EntityAINearestAttackableTarget extends EntityAITarget
    008    {
    009        EntityLiving targetEntity;
    010        Class targetClass;
    011        int targetChance;
    012
    013        /** Instance of EntityAINearestAttackableTargetSorter. */
    014        private EntityAINearestAttackableTargetSorter theNearestAttackableTargetSorter;
    015
    016        public EntityAINearestAttackableTarget(EntityLiving par1EntityLiving, Class par2Class, float par3, int par4, boolean par5)
    017        {
    018            this(par1EntityLiving, par2Class, par3, par4, par5, false);
    019        }
    020
    021        public EntityAINearestAttackableTarget(EntityLiving par1EntityLiving, Class par2Class, float par3, int par4, boolean par5, boolean par6)
    022        {
    023            super(par1EntityLiving, par3, par5, par6);
    024            this.targetClass = par2Class;
    025            this.targetDistance = par3;
    026            this.targetChance = par4;
    027            this.theNearestAttackableTargetSorter = new EntityAINearestAttackableTargetSorter(this, par1EntityLiving);
    028            this.setMutexBits(1);
    029        }
    030
    031        /**
    032         * Returns whether the EntityAIBase should begin execution.
    033         */
    034        public boolean shouldExecute()
    035        {
    036            if (this.targetChance > 0 && this.taskOwner.getRNG().nextInt(this.targetChance) != 0)
    037            {
    038                return false;
    039            }
    040            else
    041            {
    042                if (this.targetClass == EntityPlayer.class)
    043                {
    044                    EntityPlayer var1 = this.taskOwner.worldObj.getClosestVulnerablePlayerToEntity(this.taskOwner, (double)this.targetDistance);
    045
    046                    if (this.isSuitableTarget(var1, false))
    047                    {
    048                        this.targetEntity = var1;
    049                        return true;
    050                    }
    051                }
    052                else
    053                {
    054                    List var5 = this.taskOwner.worldObj.getEntitiesWithinAABB(this.targetClass, this.taskOwner.boundingBox.expand((double)this.targetDistance, 4.0D, (double)this.targetDistance));
    055                    Collections.sort(var5, this.theNearestAttackableTargetSorter);
    056                    Iterator var2 = var5.iterator();
    057
    058                    while (var2.hasNext())
    059                    {
    060                        Entity var3 = (Entity)var2.next();
    061                        EntityLiving var4 = (EntityLiving)var3;
    062
    063                        if (this.isSuitableTarget(var4, false))
    064                        {
    065                            this.targetEntity = var4;
    066                            return true;
    067                        }
    068                    }
    069                }
    070
    071                return false;
    072            }
    073        }
    074
    075        /**
    076         * Execute a one shot task or start executing a continuous task
    077         */
    078        public void startExecuting()
    079        {
    080            this.taskOwner.setAttackTarget(this.targetEntity);
    081            super.startExecuting();
    082        }
    083    }