    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 list = this.villagerObj.worldObj.getEntitiesWithinAABB(EntityVillager.class, this.villagerObj.boundingBox.expand(6.0D, 3.0D, 6.0D));
    039            double d0 = Double.MAX_VALUE;
    040            Iterator iterator = list.iterator();
    041
    042            while (iterator.hasNext())
    043            {
    044                EntityVillager entityvillager = (EntityVillager)iterator.next();
    045
    046                if (entityvillager != this.villagerObj && !entityvillager.isPlaying() && entityvillager.getGrowingAge() < 0)
    047                {
    048                    double d1 = entityvillager.getDistanceSqToEntity(this.villagerObj);
    049
    050                    if (d1 <= d0)
    051                    {
    052                        d0 = d1;
    053                        this.targetVillager = entityvillager;
    054                    }
    055                }
    056            }
    057
    058            if (this.targetVillager == null)
    059            {
    060                Vec3 vec3 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    061
    062                if (vec3 == 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 vec3 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    119
    120            if (vec3 == null)
    121            {
    122                return;
    123            }
    124
    125            this.villagerObj.getNavigator().tryMoveToXYZ(vec3.xCoord, vec3.yCoord, vec3.zCoord, this.field_75261_c);
    126        }
    127    }
    128}