    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        private final IEntitySelector field_82643_g;
    013
    014        /** Instance of EntityAINearestAttackableTargetSorter. */
    015        private EntityAINearestAttackableTargetSorter theNearestAttackableTargetSorter;
    016
    017        public EntityAINearestAttackableTarget(EntityLiving par1EntityLiving, Class par2Class, float par3, int par4, boolean par5)
    018        {
    019            this(par1EntityLiving, par2Class, par3, par4, par5, false);
    020        }
    021
    022        public EntityAINearestAttackableTarget(EntityLiving par1EntityLiving, Class par2Class, float par3, int par4, boolean par5, boolean par6)
    023        {
    024            this(par1EntityLiving, par2Class, par3, par4, par5, par6, (IEntitySelector)null);
    025        }
    026
    027        public EntityAINearestAttackableTarget(EntityLiving par1, Class par2, float par3, int par4, boolean par5, boolean par6, IEntitySelector par7IEntitySelector)
    028        {
    029            super(par1, par3, par5, par6);
    030            this.targetClass = par2;
    031            this.targetDistance = par3;
    032            this.targetChance = par4;
    033            this.theNearestAttackableTargetSorter = new EntityAINearestAttackableTargetSorter(this, par1);
    034            this.field_82643_g = par7IEntitySelector;
    035            this.setMutexBits(1);
    036        }
    037
    038        /**
    039         * Returns whether the EntityAIBase should begin execution.
    040         */
    041        public boolean shouldExecute()
    042        {
    043            if (this.targetChance > 0 && this.taskOwner.getRNG().nextInt(this.targetChance) != 0)
    044            {
    045                return false;
    046            }
    047            else
    048            {
    049                if (this.targetClass == EntityPlayer.class)
    050                {
    051                    EntityPlayer var1 = this.taskOwner.worldObj.getClosestVulnerablePlayerToEntity(this.taskOwner, (double)this.targetDistance);
    052
    053                    if (this.isSuitableTarget(var1, false))
    054                    {
    055                        this.targetEntity = var1;
    056                        return true;
    057                    }
    058                }
    059                else
    060                {
    061                    List var5 = this.taskOwner.worldObj.func_82733_a(this.targetClass, this.taskOwner.boundingBox.expand((double)this.targetDistance, 4.0D, (double)this.targetDistance), this.field_82643_g);
    062                    Collections.sort(var5, this.theNearestAttackableTargetSorter);
    063                    Iterator var2 = var5.iterator();
    064
    065                    while (var2.hasNext())
    066                    {
    067                        Entity var3 = (Entity)var2.next();
    068                        EntityLiving var4 = (EntityLiving)var3;
    069
    070                        if (this.isSuitableTarget(var4, false))
    071                        {
    072                            this.targetEntity = var4;
    073                            return true;
    074                        }
    075                    }
    076                }
    077
    078                return false;
    079            }
    080        }
    081
    082        /**
    083         * Execute a one shot task or start executing a continuous task
    084         */
    085        public void startExecuting()
    086        {
    087            this.taskOwner.setAttackTarget(this.targetEntity);
    088            super.startExecuting();
    089        }
    090    }