EntityAIEatGrass .java

net.minecraft.entity.ai.EntityAIEatGrass (class): members: getEatingGrassTimer, eatingGrassTimer, grassEaterEntity, field_179505_b, entityWorld, resetTask.

net.minecraft.entity.aiclass122 linescommonextends EntityAIBase3.8 KB

API Surface (6)

boolean shouldExecute()
void startExecuting()
void resetTask()
boolean continueExecuting()
int getEatingGrassTimer()
void updateTask()

Fields & Constants (4)

field_179505_b
grassEaterEntity
entityWorld
eatingGrassTimer

String literals (1)

"mobGriefing"

Full Source

EntityAIEatGrass.java122 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import com.google.common.base.Predicate;
4
import com.google.common.base.Predicates;
5
import net.minecraft.block.Block;
6
import net.minecraft.block.BlockTallGrass;
7
import net.minecraft.block.state.IBlockState;
8
import net.minecraft.block.state.pattern.BlockStateHelper;
9
import net.minecraft.entity.EntityLiving;
10
import net.minecraft.init.Blocks;
11
import net.minecraft.util.BlockPos;
12
import net.minecraft.world.World;
13
14
public class EntityAIEatGrass extends EntityAIBase
15
{
16
    private static final Predicate<IBlockState> field_179505_b = BlockStateHelper.forBlock(Blocks.tallgrass).where(BlockTallGrass.TYPE, Predicates.equalTo(BlockTallGrass.EnumType.GRASS));
17
18
    /** The entity owner of this AITask */
19
    private EntityLiving grassEaterEntity;
20
21
    /** The world the grass eater entity is eating from */
22
    private World entityWorld;
23
24
    /** Number of ticks since the entity started to eat grass */
25
    int eatingGrassTimer;
26
27
    public EntityAIEatGrass(EntityLiving grassEaterEntityIn)
28
    {
29
        this.grassEaterEntity = grassEaterEntityIn;
30
        this.entityWorld = grassEaterEntityIn.worldObj;
31
        this.setMutexBits(7);
32
    }
33
34
    /**
35
     * Returns whether the EntityAIBase should begin execution.
36
     */
37
    public boolean shouldExecute()
38
    {
39
        if (this.grassEaterEntity.getRNG().nextInt(this.grassEaterEntity.isChild() ? 50 : 1000) != 0)
40
        {
41
            return false;
42
        }
43
        else
44
        {
45
            BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);
46
            return field_179505_b.apply(this.entityWorld.getBlockState(blockpos)) ? true : this.entityWorld.getBlockState(blockpos.down()).getBlock() == Blocks.grass;
47
        }
48
    }
49
50
    /**
51
     * Execute a one shot task or start executing a continuous task
52
     */
53
    public void startExecuting()
54
    {
55
        this.eatingGrassTimer = 40;
56
        this.entityWorld.setEntityState(this.grassEaterEntity, (byte)10);
57
        this.grassEaterEntity.getNavigator().clearPathEntity();
58
    }
59
60
    /**
61
     * Resets the task
62
     */
63
    public void resetTask()
64
    {
65
        this.eatingGrassTimer = 0;
66
    }
67
68
    /**
69
     * Returns whether an in-progress EntityAIBase should continue executing
70
     */
71
    public boolean continueExecuting()
72
    {
73
        return this.eatingGrassTimer > 0;
74
    }
75
76
    /**
77
     * Number of ticks since the entity started to eat grass
78
     */
79
    public int getEatingGrassTimer()
80
    {
81
        return this.eatingGrassTimer;
82
    }
83
84
    /**
85
     * Updates the task
86
     */
87
    public void updateTask()
88
    {
89
        this.eatingGrassTimer = Math.max(0, this.eatingGrassTimer - 1);
90
91
        if (this.eatingGrassTimer == 4)
92
        {
93
            BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);
94
95
            if (field_179505_b.apply(this.entityWorld.getBlockState(blockpos)))
96
            {
97
                if (this.entityWorld.getGameRules().getBoolean("mobGriefing"))
98
                {
99
                    this.entityWorld.destroyBlock(blockpos, false);
100
                }
101
102
                this.grassEaterEntity.eatGrassBonus();
103
            }
104
            else
105
            {
106
                BlockPos blockpos1 = blockpos.down();
107
108
                if (this.entityWorld.getBlockState(blockpos1).getBlock() == Blocks.grass)
109
                {
110
                    if (this.entityWorld.getGameRules().getBoolean("mobGriefing"))
111
                    {
112
                        this.entityWorld.playAuxSFX(2001, blockpos1, Block.getIdFromBlock(Blocks.grass));
113
                        this.entityWorld.setBlockState(blockpos1, Blocks.dirt.getDefaultState(), 2);
114
                    }
115
116
                    this.grassEaterEntity.eatGrassBonus();
117
                }
118
            }
119
        }
120
    }
121
}
122

Deobfuscated MCP 1.8.9 source of EntityAIEatGrass from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/ai/EntityAIEatGrass.java), stored verbatim.