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