001package net.minecraft.entity.ai; 002 003import net.minecraft.block.Block; 004import net.minecraft.entity.EntityLiving; 005import net.minecraft.util.MathHelper; 006import net.minecraft.world.World; 007 008public class EntityAIEatGrass extends EntityAIBase 009{ 010 private EntityLiving theEntity; 011 private World theWorld; 012 013 /** A decrementing tick used for the sheep's head offset and animation. */ 014 int eatGrassTick = 0; 015 016 public EntityAIEatGrass(EntityLiving par1EntityLiving) 017 { 018 this.theEntity = par1EntityLiving; 019 this.theWorld = par1EntityLiving.worldObj; 020 this.setMutexBits(7); 021 } 022 023 /** 024 * Returns whether the EntityAIBase should begin execution. 025 */ 026 public boolean shouldExecute() 027 { 028 if (this.theEntity.getRNG().nextInt(this.theEntity.isChild() ? 50 : 1000) != 0) 029 { 030 return false; 031 } 032 else 033 { 034 int i = MathHelper.floor_double(this.theEntity.posX); 035 int j = MathHelper.floor_double(this.theEntity.posY); 036 int k = MathHelper.floor_double(this.theEntity.posZ); 037 return this.theWorld.getBlockId(i, j, k) == Block.tallGrass.blockID && this.theWorld.getBlockMetadata(i, j, k) == 1 ? true : this.theWorld.getBlockId(i, j - 1, k) == Block.grass.blockID; 038 } 039 } 040 041 /** 042 * Execute a one shot task or start executing a continuous task 043 */ 044 public void startExecuting() 045 { 046 this.eatGrassTick = 40; 047 this.theWorld.setEntityState(this.theEntity, (byte)10); 048 this.theEntity.getNavigator().clearPathEntity(); 049 } 050 051 /** 052 * Resets the task 053 */ 054 public void resetTask() 055 { 056 this.eatGrassTick = 0; 057 } 058 059 /** 060 * Returns whether an in-progress EntityAIBase should continue executing 061 */ 062 public boolean continueExecuting() 063 { 064 return this.eatGrassTick > 0; 065 } 066 067 public int getEatGrassTick() 068 { 069 return this.eatGrassTick; 070 } 071 072 /** 073 * Updates the task 074 */ 075 public void updateTask() 076 { 077 this.eatGrassTick = Math.max(0, this.eatGrassTick - 1); 078 079 if (this.eatGrassTick == 4) 080 { 081 int i = MathHelper.floor_double(this.theEntity.posX); 082 int j = MathHelper.floor_double(this.theEntity.posY); 083 int k = MathHelper.floor_double(this.theEntity.posZ); 084 085 if (this.theWorld.getBlockId(i, j, k) == Block.tallGrass.blockID) 086 { 087 this.theWorld.destroyBlock(i, j, k, false); 088 this.theEntity.eatGrassBonus(); 089 } 090 else if (this.theWorld.getBlockId(i, j - 1, k) == Block.grass.blockID) 091 { 092 this.theWorld.playAuxSFX(2001, i, j - 1, k, Block.grass.blockID); 093 this.theWorld.setBlock(i, j - 1, k, Block.dirt.blockID, 0, 2); 094 this.theEntity.eatGrassBonus(); 095 } 096 } 097 } 098}