EntityAIHarvestFarmland .java

net.minecraft.entity.ai.EntityAIHarvestFarmland (class): members: field_179501_f, field_179503_e, hasFarmItem, shouldMoveTo, theVillager, updateTask.

net.minecraft.entity.aiclass167 linescommonextends EntityAIMoveToBlock5.3 KB

API Surface (6)

boolean shouldExecute()
boolean continueExecuting()
void startExecuting()
void resetTask()
void updateTask()
boolean shouldMoveTo(World worldIn, BlockPos pos)

Fields & Constants (4)

theVillager
hasFarmItem
field_179503_e
field_179501_f

String literals (1)

"mobGriefing"

Full Source

EntityAIHarvestFarmland.java167 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import net.minecraft.block.Block;
4
import net.minecraft.block.BlockCrops;
5
import net.minecraft.block.state.IBlockState;
6
import net.minecraft.entity.passive.EntityVillager;
7
import net.minecraft.init.Blocks;
8
import net.minecraft.init.Items;
9
import net.minecraft.inventory.InventoryBasic;
10
import net.minecraft.item.ItemStack;
11
import net.minecraft.util.BlockPos;
12
import net.minecraft.world.World;
13
14
public class EntityAIHarvestFarmland extends EntityAIMoveToBlock
15
{
16
    /** Villager that is harvesting */
17
    private final EntityVillager theVillager;
18
    private boolean hasFarmItem;
19
    private boolean field_179503_e;
20
    private int field_179501_f;
21
22
    public EntityAIHarvestFarmland(EntityVillager theVillagerIn, double speedIn)
23
    {
24
        super(theVillagerIn, speedIn, 16);
25
        this.theVillager = theVillagerIn;
26
    }
27
28
    /**
29
     * Returns whether the EntityAIBase should begin execution.
30
     */
31
    public boolean shouldExecute()
32
    {
33
        if (this.runDelay <= 0)
34
        {
35
            if (!this.theVillager.worldObj.getGameRules().getBoolean("mobGriefing"))
36
            {
37
                return false;
38
            }
39
40
            this.field_179501_f = -1;
41
            this.hasFarmItem = this.theVillager.isFarmItemInInventory();
42
            this.field_179503_e = this.theVillager.func_175557_cr();
43
        }
44
45
        return super.shouldExecute();
46
    }
47
48
    /**
49
     * Returns whether an in-progress EntityAIBase should continue executing
50
     */
51
    public boolean continueExecuting()
52
    {
53
        return this.field_179501_f >= 0 && super.continueExecuting();
54
    }
55
56
    /**
57
     * Execute a one shot task or start executing a continuous task
58
     */
59
    public void startExecuting()
60
    {
61
        super.startExecuting();
62
    }
63
64
    /**
65
     * Resets the task
66
     */
67
    public void resetTask()
68
    {
69
        super.resetTask();
70
    }
71
72
    /**
73
     * Updates the task
74
     */
75
    public void updateTask()
76
    {
77
        super.updateTask();
78
        this.theVillager.getLookHelper().setLookPosition((double)this.destinationBlock.getX() + 0.5D, (double)(this.destinationBlock.getY() + 1), (double)this.destinationBlock.getZ() + 0.5D, 10.0F, (float)this.theVillager.getVerticalFaceSpeed());
79
80
        if (this.getIsAboveDestination())
81
        {
82
            World world = this.theVillager.worldObj;
83
            BlockPos blockpos = this.destinationBlock.up();
84
            IBlockState iblockstate = world.getBlockState(blockpos);
85
            Block block = iblockstate.getBlock();
86
87
            if (this.field_179501_f == 0 && block instanceof BlockCrops && ((Integer)iblockstate.getValue(BlockCrops.AGE)).intValue() == 7)
88
            {
89
                world.destroyBlock(blockpos, true);
90
            }
91
            else if (this.field_179501_f == 1 && block == Blocks.air)
92
            {
93
                InventoryBasic inventorybasic = this.theVillager.getVillagerInventory();
94
95
                for (int i = 0; i < inventorybasic.getSizeInventory(); ++i)
96
                {
97
                    ItemStack itemstack = inventorybasic.getStackInSlot(i);
98
                    boolean flag = false;
99
100
                    if (itemstack != null)
101
                    {
102
                        if (itemstack.getItem() == Items.wheat_seeds)
103
                        {
104
                            world.setBlockState(blockpos, Blocks.wheat.getDefaultState(), 3);
105
                            flag = true;
106
                        }
107
                        else if (itemstack.getItem() == Items.potato)
108
                        {
109
                            world.setBlockState(blockpos, Blocks.potatoes.getDefaultState(), 3);
110
                            flag = true;
111
                        }
112
                        else if (itemstack.getItem() == Items.carrot)
113
                        {
114
                            world.setBlockState(blockpos, Blocks.carrots.getDefaultState(), 3);
115
                            flag = true;
116
                        }
117
                    }
118
119
                    if (flag)
120
                    {
121
                        --itemstack.stackSize;
122
123
                        if (itemstack.stackSize <= 0)
124
                        {
125
                            inventorybasic.setInventorySlotContents(i, (ItemStack)null);
126
                        }
127
128
                        break;
129
                    }
130
                }
131
            }
132
133
            this.field_179501_f = -1;
134
            this.runDelay = 10;
135
        }
136
    }
137
138
    /**
139
     * Return true to set given position as destination
140
     */
141
    protected boolean shouldMoveTo(World worldIn, BlockPos pos)
142
    {
143
        Block block = worldIn.getBlockState(pos).getBlock();
144
145
        if (block == Blocks.farmland)
146
        {
147
            pos = pos.up();
148
            IBlockState iblockstate = worldIn.getBlockState(pos);
149
            block = iblockstate.getBlock();
150
151
            if (block instanceof BlockCrops && ((Integer)iblockstate.getValue(BlockCrops.AGE)).intValue() == 7 && this.field_179503_e && (this.field_179501_f == 0 || this.field_179501_f < 0))
152
            {
153
                this.field_179501_f = 0;
154
                return true;
155
            }
156
157
            if (block == Blocks.air && this.hasFarmItem && (this.field_179501_f == 1 || this.field_179501_f < 0))
158
            {
159
                this.field_179501_f = 1;
160
                return true;
161
            }
162
        }
163
164
        return false;
165
    }
166
}
167

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