EntityThrowable .java

net.minecraft.entity.projectile.EntityThrowable (class, 381 lines): members: throwableShake, throwerName, getThrower, thrower, setThrowableHeading, inTile.

net.minecraft.entity.projectileclass381 linescommonextends Entity13.4 KB

Implements

API Surface (11)

void entityInit()
boolean isInRangeToRenderDist(double distance)
float getVelocity()
float getInaccuracy()
void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy)
void setVelocity(double x, double y, double z)
void onUpdate()
float getGravityVelocity()
void writeEntityToNBT(NBTTagCompound tagCompound)
void readEntityFromNBT(NBTTagCompound tagCompund)
EntityLivingBase getThrower()

Fields & Constants (10)

xTile
yTile
zTile
inTile
inGround
throwableShake
thrower
throwerName
ticksInGround
ticksInAir

String literals (7)

"xTile"
"yTile"
"zTile"
"inTile"
"shake"
"inGround"
"ownerName"

Full Source

EntityThrowable.java381 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.projectile;
2
3
import java.util.List;
4
import java.util.UUID;
5
import net.minecraft.block.Block;
6
import net.minecraft.entity.Entity;
7
import net.minecraft.entity.EntityLivingBase;
8
import net.minecraft.entity.IProjectile;
9
import net.minecraft.entity.player.EntityPlayer;
10
import net.minecraft.init.Blocks;
11
import net.minecraft.nbt.NBTTagCompound;
12
import net.minecraft.util.AxisAlignedBB;
13
import net.minecraft.util.BlockPos;
14
import net.minecraft.util.EnumParticleTypes;
15
import net.minecraft.util.MathHelper;
16
import net.minecraft.util.MovingObjectPosition;
17
import net.minecraft.util.ResourceLocation;
18
import net.minecraft.util.Vec3;
19
import net.minecraft.world.World;
20
import net.minecraft.world.WorldServer;
21
22
public abstract class EntityThrowable extends Entity implements IProjectile
23
{
24
    private int xTile = -1;
25
    private int yTile = -1;
26
    private int zTile = -1;
27
    private Block inTile;
28
    protected boolean inGround;
29
    public int throwableShake;
30
31
    /** The entity that threw this throwable item. */
32
    private EntityLivingBase thrower;
33
    private String throwerName;
34
    private int ticksInGround;
35
    private int ticksInAir;
36
37
    public EntityThrowable(World worldIn)
38
    {
39
        super(worldIn);
40
        this.setSize(0.25F, 0.25F);
41
    }
42
43
    protected void entityInit()
44
    {
45
    }
46
47
    /**
48
     * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
49
     * length * 64 * renderDistanceWeight Args: distance
50
     */
51
    public boolean isInRangeToRenderDist(double distance)
52
    {
53
        double d0 = this.getEntityBoundingBox().getAverageEdgeLength() * 4.0D;
54
55
        if (Double.isNaN(d0))
56
        {
57
            d0 = 4.0D;
58
        }
59
60
        d0 = d0 * 64.0D;
61
        return distance < d0 * d0;
62
    }
63
64
    public EntityThrowable(World worldIn, EntityLivingBase throwerIn)
65
    {
66
        super(worldIn);
67
        this.thrower = throwerIn;
68
        this.setSize(0.25F, 0.25F);
69
        this.setLocationAndAngles(throwerIn.posX, throwerIn.posY + (double)throwerIn.getEyeHeight(), throwerIn.posZ, throwerIn.rotationYaw, throwerIn.rotationPitch);
70
        this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
71
        this.posY -= 0.10000000149011612D;
72
        this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
73
        this.setPosition(this.posX, this.posY, this.posZ);
74
        float f = 0.4F;
75
        this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
76
        this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
77
        this.motionY = (double)(-MathHelper.sin((this.rotationPitch + this.getInaccuracy()) / 180.0F * (float)Math.PI) * f);
78
        this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, this.getVelocity(), 1.0F);
79
    }
80
81
    public EntityThrowable(World worldIn, double x, double y, double z)
82
    {
83
        super(worldIn);
84
        this.ticksInGround = 0;
85
        this.setSize(0.25F, 0.25F);
86
        this.setPosition(x, y, z);
87
    }
88
89
    protected float getVelocity()
90
    {
91
        return 1.5F;
92
    }
93
94
    protected float getInaccuracy()
95
    {
96
        return 0.0F;
97
    }
98
99
    /**
100
     * Similar to setArrowHeading, it's point the throwable entity to a x, y, z direction.
101
     */
102
    public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy)
103
    {
104
        float f = MathHelper.sqrt_double(x * x + y * y + z * z);
105
        x = x / (double)f;
106
        y = y / (double)f;
107
        z = z / (double)f;
108
        x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
109
        y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
110
        z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
111
        x = x * (double)velocity;
112
        y = y * (double)velocity;
113
        z = z * (double)velocity;
114
        this.motionX = x;
115
        this.motionY = y;
116
        this.motionZ = z;
117
        float f1 = MathHelper.sqrt_double(x * x + z * z);
118
        this.prevRotationYaw = this.rotationYaw = (float)(MathHelper.atan2(x, z) * 180.0D / Math.PI);
119
        this.prevRotationPitch = this.rotationPitch = (float)(MathHelper.atan2(y, (double)f1) * 180.0D / Math.PI);
120
        this.ticksInGround = 0;
121
    }
122
123
    /**
124
     * Sets the velocity to the args. Args: x, y, z
125
     */
126
    public void setVelocity(double x, double y, double z)
127
    {
128
        this.motionX = x;
129
        this.motionY = y;
130
        this.motionZ = z;
131
132
        if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
133
        {
134
            float f = MathHelper.sqrt_double(x * x + z * z);
135
            this.prevRotationYaw = this.rotationYaw = (float)(MathHelper.atan2(x, z) * 180.0D / Math.PI);
136
            this.prevRotationPitch = this.rotationPitch = (float)(MathHelper.atan2(y, (double)f) * 180.0D / Math.PI);
137
        }
138
    }
139
140
    /**
141
     * Called to update the entity's position/logic.
142
     */
143
    public void onUpdate()
144
    {
145
        this.lastTickPosX = this.posX;
146
        this.lastTickPosY = this.posY;
147
        this.lastTickPosZ = this.posZ;
148
        super.onUpdate();
149
150
        if (this.throwableShake > 0)
151
        {
152
            --this.throwableShake;
153
        }
154
155
        if (this.inGround)
156
        {
157
            if (this.worldObj.getBlockState(new BlockPos(this.xTile, this.yTile, this.zTile)).getBlock() == this.inTile)
158
            {
159
                ++this.ticksInGround;
160
161
                if (this.ticksInGround == 1200)
162
                {
163
                    this.setDead();
164
                }
165
166
                return;
167
            }
168
169
            this.inGround = false;
170
            this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
171
            this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
172
            this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
173
            this.ticksInGround = 0;
174
            this.ticksInAir = 0;
175
        }
176
        else
177
        {
178
            ++this.ticksInAir;
179
        }
180
181
        Vec3 vec3 = new Vec3(this.posX, this.posY, this.posZ);
182
        Vec3 vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
183
        MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec3, vec31);
184
        vec3 = new Vec3(this.posX, this.posY, this.posZ);
185
        vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
186
187
        if (movingobjectposition != null)
188
        {
189
            vec31 = new Vec3(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
190
        }
191
192
        if (!this.worldObj.isRemote)
193
        {
194
            Entity entity = null;
195
            List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
196
            double d0 = 0.0D;
197
            EntityLivingBase entitylivingbase = this.getThrower();
198
199
            for (int j = 0; j < list.size(); ++j)
200
            {
201
                Entity entity1 = (Entity)list.get(j);
202
203
                if (entity1.canBeCollidedWith() && (entity1 != entitylivingbase || this.ticksInAir >= 5))
204
                {
205
                    float f = 0.3F;
206
                    AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expand((double)f, (double)f, (double)f);
207
                    MovingObjectPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec3, vec31);
208
209
                    if (movingobjectposition1 != null)
210
                    {
211
                        double d1 = vec3.squareDistanceTo(movingobjectposition1.hitVec);
212
213
                        if (d1 < d0 || d0 == 0.0D)
214
                        {
215
                            entity = entity1;
216
                            d0 = d1;
217
                        }
218
                    }
219
                }
220
            }
221
222
            if (entity != null)
223
            {
224
                movingobjectposition = new MovingObjectPosition(entity);
225
            }
226
        }
227
228
        if (movingobjectposition != null)
229
        {
230
            if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && this.worldObj.getBlockState(movingobjectposition.getBlockPos()).getBlock() == Blocks.portal)
231
            {
232
                this.setPortal(movingobjectposition.getBlockPos());
233
            }
234
            else
235
            {
236
                this.onImpact(movingobjectposition);
237
            }
238
        }
239
240
        this.posX += this.motionX;
241
        this.posY += this.motionY;
242
        this.posZ += this.motionZ;
243
        float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
244
        this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
245
246
        for (this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f1) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
247
        {
248
            ;
249
        }
250
251
        while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
252
        {
253
            this.prevRotationPitch += 360.0F;
254
        }
255
256
        while (this.rotationYaw - this.prevRotationYaw < -180.0F)
257
        {
258
            this.prevRotationYaw -= 360.0F;
259
        }
260
261
        while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
262
        {
263
            this.prevRotationYaw += 360.0F;
264
        }
265
266
        this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
267
        this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
268
        float f2 = 0.99F;
269
        float f3 = this.getGravityVelocity();
270
271
        if (this.isInWater())
272
        {
273
            for (int i = 0; i < 4; ++i)
274
            {
275
                float f4 = 0.25F;
276
                this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * (double)f4, this.posY - this.motionY * (double)f4, this.posZ - this.motionZ * (double)f4, this.motionX, this.motionY, this.motionZ, new int[0]);
277
            }
278
279
            f2 = 0.8F;
280
        }
281
282
        this.motionX *= (double)f2;
283
        this.motionY *= (double)f2;
284
        this.motionZ *= (double)f2;
285
        this.motionY -= (double)f3;
286
        this.setPosition(this.posX, this.posY, this.posZ);
287
    }
288
289
    /**
290
     * Gets the amount of gravity to apply to the thrown entity with each tick.
291
     */
292
    protected float getGravityVelocity()
293
    {
294
        return 0.03F;
295
    }
296
297
    /**
298
     * Called when this EntityThrowable hits a block or entity.
299
     */
300
    protected abstract void onImpact(MovingObjectPosition p_70184_1_);
301
302
    /**
303
     * (abstract) Protected helper method to write subclass entity data to NBT.
304
     */
305
    public void writeEntityToNBT(NBTTagCompound tagCompound)
306
    {
307
        tagCompound.setShort("xTile", (short)this.xTile);
308
        tagCompound.setShort("yTile", (short)this.yTile);
309
        tagCompound.setShort("zTile", (short)this.zTile);
310
        ResourceLocation resourcelocation = (ResourceLocation)Block.blockRegistry.getNameForObject(this.inTile);
311
        tagCompound.setString("inTile", resourcelocation == null ? "" : resourcelocation.toString());
312
        tagCompound.setByte("shake", (byte)this.throwableShake);
313
        tagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0));
314
315
        if ((this.throwerName == null || this.throwerName.length() == 0) && this.thrower instanceof EntityPlayer)
316
        {
317
            this.throwerName = this.thrower.getName();
318
        }
319
320
        tagCompound.setString("ownerName", this.throwerName == null ? "" : this.throwerName);
321
    }
322
323
    /**
324
     * (abstract) Protected helper method to read subclass entity data from NBT.
325
     */
326
    public void readEntityFromNBT(NBTTagCompound tagCompund)
327
    {
328
        this.xTile = tagCompund.getShort("xTile");
329
        this.yTile = tagCompund.getShort("yTile");
330
        this.zTile = tagCompund.getShort("zTile");
331
332
        if (tagCompund.hasKey("inTile", 8))
333
        {
334
            this.inTile = Block.getBlockFromName(tagCompund.getString("inTile"));
335
        }
336
        else
337
        {
338
            this.inTile = Block.getBlockById(tagCompund.getByte("inTile") & 255);
339
        }
340
341
        this.throwableShake = tagCompund.getByte("shake") & 255;
342
        this.inGround = tagCompund.getByte("inGround") == 1;
343
        this.thrower = null;
344
        this.throwerName = tagCompund.getString("ownerName");
345
346
        if (this.throwerName != null && this.throwerName.length() == 0)
347
        {
348
            this.throwerName = null;
349
        }
350
351
        this.thrower = this.getThrower();
352
    }
353
354
    public EntityLivingBase getThrower()
355
    {
356
        if (this.thrower == null && this.throwerName != null && this.throwerName.length() > 0)
357
        {
358
            this.thrower = this.worldObj.getPlayerEntityByName(this.throwerName);
359
360
            if (this.thrower == null && this.worldObj instanceof WorldServer)
361
            {
362
                try
363
                {
364
                    Entity entity = ((WorldServer)this.worldObj).getEntityFromUuid(UUID.fromString(this.throwerName));
365
366
                    if (entity instanceof EntityLivingBase)
367
                    {
368
                        this.thrower = (EntityLivingBase)entity;
369
                    }
370
                }
371
                catch (Throwable var2)
372
                {
373
                    this.thrower = null;
374
                }
375
            }
376
        }
377
378
        return this.thrower;
379
    }
380
}
381

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