    001package net.minecraft.entity.ai;
    002
    003import java.util.Iterator;
    004import java.util.List;
    005import net.minecraft.entity.EntityLiving;
    006import net.minecraft.entity.passive.EntityVillager;
    007import net.minecraft.util.Vec3;
    008
    009public class EntityAIPlay extends EntityAIBase
    010{
    011    private EntityVillager villagerObj;
    012    private EntityLiving targetVillager;
    013    private float field_75261_c;
    014    private int playTime;
    015
    016    public EntityAIPlay(EntityVillager par1EntityVillager, float par2)
    017    {
    018        this.villagerObj = par1EntityVillager;
    019        this.field_75261_c = par2;
    020        this.setMutexBits(1);
    021    }
    022
    023    /**
    024     * Returns whether the EntityAIBase should begin execution.
    025     */
    026    public boolean shouldExecute()
    027    {
    028        if (this.villagerObj.getGrowingAge() >= 0)
    029        {
    030            return false;
    031        }
    032        else if (this.villagerObj.getRNG().nextInt(400) != 0)
    033        {
    034            return false;
    035        }
    036        else
    037        {
    038            List var1 = this.villagerObj.worldObj.getEntitiesWithinAABB(EntityVillager.class, this.villagerObj.boundingBox.expand(6.0D, 3.0D, 6.0D));
    039            double var2 = Double.MAX_VALUE;
    040            Iterator var4 = var1.iterator();
    041
    042            while (var4.hasNext())
    043            {
    044                EntityVillager var5 = (EntityVillager)var4.next();
    045
    046                if (var5 != this.villagerObj && !var5.isPlaying() && var5.getGrowingAge() < 0)
    047                {
    048                    double var6 = var5.getDistanceSqToEntity(this.villagerObj);
    049
    050                    if (var6 <= var2)
    051                    {
    052                        var2 = var6;
    053                        this.targetVillager = var5;
    054                    }
    055                }
    056            }
    057
    058            if (this.targetVillager == null)
    059            {
    060                Vec3 var8 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    061
    062                if (var8 == null)
    063                {
    064                    return false;
    065                }
    066            }
    067
    068            return true;
    069        }
    070    }
    071
    072    /**
    073     * Returns whether an in-progress EntityAIBase should continue executing
    074     */
    075    public boolean continueExecuting()
    076    {
    077        return this.playTime > 0;
    078    }
    079
    080    /**
    081     * Execute a one shot task or start executing a continuous task
    082     */
    083    public void startExecuting()
    084    {
    085        if (this.targetVillager != null)
    086        {
    087            this.villagerObj.setPlaying(true);
    088        }
    089
    090        this.playTime = 1000;
    091    }
    092
    093    /**
    094     * Resets the task
    095     */
    096    public void resetTask()
    097    {
    098        this.villagerObj.setPlaying(false);
    099        this.targetVillager = null;
    100    }
    101
    102    /**
    103     * Updates the task
    104     */
    105    public void updateTask()
    106    {
    107        --this.playTime;
    108
    109        if (this.targetVillager != null)
    110        {
    111            if (this.villagerObj.getDistanceSqToEntity(this.targetVillager) > 4.0D)
    112            {
    113                this.villagerObj.getNavigator().tryMoveToEntityLiving(this.targetVillager, this.field_75261_c);
    114            }
    115        }
    116        else if (this.villagerObj.getNavigator().noPath())
    117        {
    118            Vec3 var1 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    119
    120            if (var1 == null)
    121            {
    122                return;
    123            }
    124
    125            this.villagerObj.getNavigator().tryMoveToXYZ(var1.xCoord, var1.yCoord, var1.zCoord, this.field_75261_c);
    126        }
    127    }
    128}