001package net.minecraft.entity.ai; 002 003import java.util.Iterator; 004import java.util.List; 005import net.minecraft.entity.passive.EntityAnimal; 006 007public class EntityAIFollowParent extends EntityAIBase 008{ 009 /** The child that is following its parent. */ 010 EntityAnimal childAnimal; 011 EntityAnimal parentAnimal; 012 float field_75347_c; 013 private int field_75345_d; 014 015 public EntityAIFollowParent(EntityAnimal par1EntityAnimal, float par2) 016 { 017 this.childAnimal = par1EntityAnimal; 018 this.field_75347_c = par2; 019 } 020 021 /** 022 * Returns whether the EntityAIBase should begin execution. 023 */ 024 public boolean shouldExecute() 025 { 026 if (this.childAnimal.getGrowingAge() >= 0) 027 { 028 return false; 029 } 030 else 031 { 032 List list = this.childAnimal.worldObj.getEntitiesWithinAABB(this.childAnimal.getClass(), this.childAnimal.boundingBox.expand(8.0D, 4.0D, 8.0D)); 033 EntityAnimal entityanimal = null; 034 double d0 = Double.MAX_VALUE; 035 Iterator iterator = list.iterator(); 036 037 while (iterator.hasNext()) 038 { 039 EntityAnimal entityanimal1 = (EntityAnimal)iterator.next(); 040 041 if (entityanimal1.getGrowingAge() >= 0) 042 { 043 double d1 = this.childAnimal.getDistanceSqToEntity(entityanimal1); 044 045 if (d1 <= d0) 046 { 047 d0 = d1; 048 entityanimal = entityanimal1; 049 } 050 } 051 } 052 053 if (entityanimal == null) 054 { 055 return false; 056 } 057 else if (d0 < 9.0D) 058 { 059 return false; 060 } 061 else 062 { 063 this.parentAnimal = entityanimal; 064 return true; 065 } 066 } 067 } 068 069 /** 070 * Returns whether an in-progress EntityAIBase should continue executing 071 */ 072 public boolean continueExecuting() 073 { 074 if (!this.parentAnimal.isEntityAlive()) 075 { 076 return false; 077 } 078 else 079 { 080 double d0 = this.childAnimal.getDistanceSqToEntity(this.parentAnimal); 081 return d0 >= 9.0D && d0 <= 256.0D; 082 } 083 } 084 085 /** 086 * Execute a one shot task or start executing a continuous task 087 */ 088 public void startExecuting() 089 { 090 this.field_75345_d = 0; 091 } 092 093 /** 094 * Resets the task 095 */ 096 public void resetTask() 097 { 098 this.parentAnimal = null; 099 } 100 101 /** 102 * Updates the task 103 */ 104 public void updateTask() 105 { 106 if (--this.field_75345_d <= 0) 107 { 108 this.field_75345_d = 10; 109 this.childAnimal.getNavigator().tryMoveToEntityLiving(this.parentAnimal, this.field_75347_c); 110 } 111 } 112}