001    package net.minecraft.entity.ai;
002    
003    import net.minecraft.block.Block;
004    import net.minecraft.entity.EntityLiving;
005    import net.minecraft.util.MathHelper;
006    import net.minecraft.world.World;
007    
008    public 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 var1 = MathHelper.floor_double(this.theEntity.posX);
035                int var2 = MathHelper.floor_double(this.theEntity.posY);
036                int var3 = MathHelper.floor_double(this.theEntity.posZ);
037                return this.theWorld.getBlockId(var1, var2, var3) == Block.tallGrass.blockID && this.theWorld.getBlockMetadata(var1, var2, var3) == 1 ? true : this.theWorld.getBlockId(var1, var2 - 1, var3) == 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 var1 = MathHelper.floor_double(this.theEntity.posX);
082                int var2 = MathHelper.floor_double(this.theEntity.posY);
083                int var3 = MathHelper.floor_double(this.theEntity.posZ);
084    
085                if (this.theWorld.getBlockId(var1, var2, var3) == Block.tallGrass.blockID)
086                {
087                    this.theWorld.playAuxSFX(2001, var1, var2, var3, Block.tallGrass.blockID + 4096);
088                    this.theWorld.setBlockWithNotify(var1, var2, var3, 0);
089                    this.theEntity.eatGrassBonus();
090                }
091                else if (this.theWorld.getBlockId(var1, var2 - 1, var3) == Block.grass.blockID)
092                {
093                    this.theWorld.playAuxSFX(2001, var1, var2 - 1, var3, Block.grass.blockID);
094                    this.theWorld.setBlockWithNotify(var1, var2 - 1, var3, Block.dirt.blockID);
095                    this.theEntity.eatGrassBonus();
096                }
097            }
098        }
099    }