EntityAIControlledByPlayer .java

net.minecraft.entity.ai.EntityAIControlledByPlayer (class): members: isControlledByPlayer, maxSpeedBoostTime, isSpeedBoosted, speedBoostTime, currentSpeed.

net.minecraft.entity.aiclass228 linescommonextends EntityAIBase7.3 KB

API Surface (7)

void startExecuting()
void resetTask()
boolean shouldExecute()
void updateTask()
boolean isSpeedBoosted()
void boostSpeed()
boolean isControlledByPlayer()

Fields & Constants (6)

thisEntity
maxSpeed
currentSpeed
speedBoosted
speedBoostTime
maxSpeedBoostTime

Full Source

EntityAIControlledByPlayer.java228 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import net.minecraft.block.Block;
4
import net.minecraft.block.BlockSlab;
5
import net.minecraft.block.BlockStairs;
6
import net.minecraft.block.material.Material;
7
import net.minecraft.entity.EntityCreature;
8
import net.minecraft.entity.EntityLiving;
9
import net.minecraft.entity.player.EntityPlayer;
10
import net.minecraft.init.Items;
11
import net.minecraft.item.ItemStack;
12
import net.minecraft.util.BlockPos;
13
import net.minecraft.util.MathHelper;
14
import net.minecraft.world.pathfinder.WalkNodeProcessor;
15
16
public class EntityAIControlledByPlayer extends EntityAIBase
17
{
18
    private final EntityLiving thisEntity;
19
    private final float maxSpeed;
20
    private float currentSpeed;
21
22
    /** Whether the entity's speed is boosted. */
23
    private boolean speedBoosted;
24
25
    /**
26
     * Counter for speed boosting, upon reaching maxSpeedBoostTime the speed boost will be disabled
27
     */
28
    private int speedBoostTime;
29
30
    /** Maximum time the entity's speed should be boosted for. */
31
    private int maxSpeedBoostTime;
32
33
    public EntityAIControlledByPlayer(EntityLiving entitylivingIn, float maxspeed)
34
    {
35
        this.thisEntity = entitylivingIn;
36
        this.maxSpeed = maxspeed;
37
        this.setMutexBits(7);
38
    }
39
40
    /**
41
     * Execute a one shot task or start executing a continuous task
42
     */
43
    public void startExecuting()
44
    {
45
        this.currentSpeed = 0.0F;
46
    }
47
48
    /**
49
     * Resets the task
50
     */
51
    public void resetTask()
52
    {
53
        this.speedBoosted = false;
54
        this.currentSpeed = 0.0F;
55
    }
56
57
    /**
58
     * Returns whether the EntityAIBase should begin execution.
59
     */
60
    public boolean shouldExecute()
61
    {
62
        return this.thisEntity.isEntityAlive() && this.thisEntity.riddenByEntity != null && this.thisEntity.riddenByEntity instanceof EntityPlayer && (this.speedBoosted || this.thisEntity.canBeSteered());
63
    }
64
65
    /**
66
     * Updates the task
67
     */
68
    public void updateTask()
69
    {
70
        EntityPlayer entityplayer = (EntityPlayer)this.thisEntity.riddenByEntity;
71
        EntityCreature entitycreature = (EntityCreature)this.thisEntity;
72
        float f = MathHelper.wrapAngleTo180_float(entityplayer.rotationYaw - this.thisEntity.rotationYaw) * 0.5F;
73
74
        if (f > 5.0F)
75
        {
76
            f = 5.0F;
77
        }
78
79
        if (f < -5.0F)
80
        {
81
            f = -5.0F;
82
        }
83
84
        this.thisEntity.rotationYaw = MathHelper.wrapAngleTo180_float(this.thisEntity.rotationYaw + f);
85
86
        if (this.currentSpeed < this.maxSpeed)
87
        {
88
            this.currentSpeed += (this.maxSpeed - this.currentSpeed) * 0.01F;
89
        }
90
91
        if (this.currentSpeed > this.maxSpeed)
92
        {
93
            this.currentSpeed = this.maxSpeed;
94
        }
95
96
        int i = MathHelper.floor_double(this.thisEntity.posX);
97
        int j = MathHelper.floor_double(this.thisEntity.posY);
98
        int k = MathHelper.floor_double(this.thisEntity.posZ);
99
        float f1 = this.currentSpeed;
100
101
        if (this.speedBoosted)
102
        {
103
            if (this.speedBoostTime++ > this.maxSpeedBoostTime)
104
            {
105
                this.speedBoosted = false;
106
            }
107
108
            f1 += f1 * 1.15F * MathHelper.sin((float)this.speedBoostTime / (float)this.maxSpeedBoostTime * (float)Math.PI);
109
        }
110
111
        float f2 = 0.91F;
112
113
        if (this.thisEntity.onGround)
114
        {
115
            f2 = this.thisEntity.worldObj.getBlockState(new BlockPos(MathHelper.floor_float((float)i), MathHelper.floor_float((float)j) - 1, MathHelper.floor_float((float)k))).getBlock().slipperiness * 0.91F;
116
        }
117
118
        float f3 = 0.16277136F / (f2 * f2 * f2);
119
        float f4 = MathHelper.sin(entitycreature.rotationYaw * (float)Math.PI / 180.0F);
120
        float f5 = MathHelper.cos(entitycreature.rotationYaw * (float)Math.PI / 180.0F);
121
        float f6 = entitycreature.getAIMoveSpeed() * f3;
122
        float f7 = Math.max(f1, 1.0F);
123
        f7 = f6 / f7;
124
        float f8 = f1 * f7;
125
        float f9 = -(f8 * f4);
126
        float f10 = f8 * f5;
127
128
        if (MathHelper.abs(f9) > MathHelper.abs(f10))
129
        {
130
            if (f9 < 0.0F)
131
            {
132
                f9 -= this.thisEntity.width / 2.0F;
133
            }
134
135
            if (f9 > 0.0F)
136
            {
137
                f9 += this.thisEntity.width / 2.0F;
138
            }
139
140
            f10 = 0.0F;
141
        }
142
        else
143
        {
144
            f9 = 0.0F;
145
146
            if (f10 < 0.0F)
147
            {
148
                f10 -= this.thisEntity.width / 2.0F;
149
            }
150
151
            if (f10 > 0.0F)
152
            {
153
                f10 += this.thisEntity.width / 2.0F;
154
            }
155
        }
156
157
        int l = MathHelper.floor_double(this.thisEntity.posX + (double)f9);
158
        int i1 = MathHelper.floor_double(this.thisEntity.posZ + (double)f10);
159
        int j1 = MathHelper.floor_float(this.thisEntity.width + 1.0F);
160
        int k1 = MathHelper.floor_float(this.thisEntity.height + entityplayer.height + 1.0F);
161
        int l1 = MathHelper.floor_float(this.thisEntity.width + 1.0F);
162
163
        if (i != l || k != i1)
164
        {
165
            Block block = this.thisEntity.worldObj.getBlockState(new BlockPos(i, j, k)).getBlock();
166
            boolean flag = !this.isStairOrSlab(block) && (block.getMaterial() != Material.air || !this.isStairOrSlab(this.thisEntity.worldObj.getBlockState(new BlockPos(i, j - 1, k)).getBlock()));
167
168
            if (flag && 0 == WalkNodeProcessor.func_176170_a(this.thisEntity.worldObj, this.thisEntity, l, j, i1, j1, k1, l1, false, false, true) && 1 == WalkNodeProcessor.func_176170_a(this.thisEntity.worldObj, this.thisEntity, i, j + 1, k, j1, k1, l1, false, false, true) && 1 == WalkNodeProcessor.func_176170_a(this.thisEntity.worldObj, this.thisEntity, l, j + 1, i1, j1, k1, l1, false, false, true))
169
            {
170
                entitycreature.getJumpHelper().setJumping();
171
            }
172
        }
173
174
        if (!entityplayer.capabilities.isCreativeMode && this.currentSpeed >= this.maxSpeed * 0.5F && this.thisEntity.getRNG().nextFloat() < 0.006F && !this.speedBoosted)
175
        {
176
            ItemStack itemstack = entityplayer.getHeldItem();
177
178
            if (itemstack != null && itemstack.getItem() == Items.carrot_on_a_stick)
179
            {
180
                itemstack.damageItem(1, entityplayer);
181
182
                if (itemstack.stackSize == 0)
183
                {
184
                    ItemStack itemstack1 = new ItemStack(Items.fishing_rod);
185
                    itemstack1.setTagCompound(itemstack.getTagCompound());
186
                    entityplayer.inventory.mainInventory[entityplayer.inventory.currentItem] = itemstack1;
187
                }
188
            }
189
        }
190
191
        this.thisEntity.moveEntityWithHeading(0.0F, f1);
192
    }
193
194
    /**
195
     * True if the block is a stair block or a slab block
196
     */
197
    private boolean isStairOrSlab(Block blockIn)
198
    {
199
        return blockIn instanceof BlockStairs || blockIn instanceof BlockSlab;
200
    }
201
202
    /**
203
     * Return whether the entity's speed is boosted.
204
     */
205
    public boolean isSpeedBoosted()
206
    {
207
        return this.speedBoosted;
208
    }
209
210
    /**
211
     * Boost the entity's movement speed.
212
     */
213
    public void boostSpeed()
214
    {
215
        this.speedBoosted = true;
216
        this.speedBoostTime = 0;
217
        this.maxSpeedBoostTime = this.thisEntity.getRNG().nextInt(841) + 140;
218
    }
219
220
    /**
221
     * Return whether the entity is being controlled by a player.
222
     */
223
    public boolean isControlledByPlayer()
224
    {
225
        return !this.isSpeedBoosted() && this.currentSpeed > this.maxSpeed * 0.3F;
226
    }
227
}
228

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