EntityItem .java

net.minecraft.entity.item.EntityItem (class): members: setAgeToCreativeDespawnTime, setInfinitePickupDelay, setDefaultPickupDelay, setEntityItemStack, age.

net.minecraft.entity.itemclass544 linescommonextends Entity16.0 KB

API Surface (27)

boolean canTriggerWalking()
void entityInit()
void onUpdate()
void setAgeToCreativeDespawnTime()
boolean handleWaterMovement()
void dealFireDamage(int amount)
boolean attackEntityFrom(DamageSource source, float amount)
void writeEntityToNBT(NBTTagCompound tagCompound)
void readEntityFromNBT(NBTTagCompound tagCompund)
void onCollideWithPlayer(EntityPlayer entityIn)
String getName()
boolean canAttackWithItem()
void travelToDimension(int dimensionId)
ItemStack getEntityItem()
void setEntityItemStack(ItemStack stack)
String getOwner()
void setOwner(String owner)
String getThrower()
void setThrower(String thrower)
int getAge()
void setDefaultPickupDelay()
void setNoPickupDelay()
void setInfinitePickupDelay()
void setPickupDelay(int ticks)
boolean cannotPickup()
void setNoDespawn()
void func_174870_v()

Fields & Constants (7)

logger
age
delayBeforeCanPickup
health
thrower
owner
hoverStart

String literals (10)

"random.fizz"
"Health"
"PickupDelay"
"Thrower"
"Owner"
"Item"
"random.pop"
"item."
"Item entity "
" has no item?!"

Full Source

EntityItem.java544 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.item;
2
3
import net.minecraft.block.material.Material;
4
import net.minecraft.entity.Entity;
5
import net.minecraft.entity.player.EntityPlayer;
6
import net.minecraft.init.Blocks;
7
import net.minecraft.init.Items;
8
import net.minecraft.item.Item;
9
import net.minecraft.item.ItemStack;
10
import net.minecraft.nbt.NBTTagCompound;
11
import net.minecraft.stats.AchievementList;
12
import net.minecraft.util.BlockPos;
13
import net.minecraft.util.DamageSource;
14
import net.minecraft.util.MathHelper;
15
import net.minecraft.util.StatCollector;
16
import net.minecraft.world.World;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
20
public class EntityItem extends Entity
21
{
22
    private static final Logger logger = LogManager.getLogger();
23
24
    /**
25
     * The age of this EntityItem (used to animate it up and down as well as expire it)
26
     */
27
    private int age;
28
    private int delayBeforeCanPickup;
29
30
    /** The health of this EntityItem. (For example, damage for tools) */
31
    private int health;
32
    private String thrower;
33
    private String owner;
34
35
    /** The EntityItem's random initial float height. */
36
    public float hoverStart;
37
38
    public EntityItem(World worldIn, double x, double y, double z)
39
    {
40
        super(worldIn);
41
        this.health = 5;
42
        this.hoverStart = (float)(Math.random() * Math.PI * 2.0D);
43
        this.setSize(0.25F, 0.25F);
44
        this.setPosition(x, y, z);
45
        this.rotationYaw = (float)(Math.random() * 360.0D);
46
        this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
47
        this.motionY = 0.20000000298023224D;
48
        this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
49
    }
50
51
    public EntityItem(World worldIn, double x, double y, double z, ItemStack stack)
52
    {
53
        this(worldIn, x, y, z);
54
        this.setEntityItemStack(stack);
55
    }
56
57
    /**
58
     * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
59
     * prevent them from trampling crops
60
     */
61
    protected boolean canTriggerWalking()
62
    {
63
        return false;
64
    }
65
66
    public EntityItem(World worldIn)
67
    {
68
        super(worldIn);
69
        this.health = 5;
70
        this.hoverStart = (float)(Math.random() * Math.PI * 2.0D);
71
        this.setSize(0.25F, 0.25F);
72
        this.setEntityItemStack(new ItemStack(Blocks.air, 0));
73
    }
74
75
    protected void entityInit()
76
    {
77
        this.getDataWatcher().addObjectByDataType(10, 5);
78
    }
79
80
    /**
81
     * Called to update the entity's position/logic.
82
     */
83
    public void onUpdate()
84
    {
85
        if (this.getEntityItem() == null)
86
        {
87
            this.setDead();
88
        }
89
        else
90
        {
91
            super.onUpdate();
92
93
            if (this.delayBeforeCanPickup > 0 && this.delayBeforeCanPickup != 32767)
94
            {
95
                --this.delayBeforeCanPickup;
96
            }
97
98
            this.prevPosX = this.posX;
99
            this.prevPosY = this.posY;
100
            this.prevPosZ = this.posZ;
101
            this.motionY -= 0.03999999910593033D;
102
            this.noClip = this.pushOutOfBlocks(this.posX, (this.getEntityBoundingBox().minY + this.getEntityBoundingBox().maxY) / 2.0D, this.posZ);
103
            this.moveEntity(this.motionX, this.motionY, this.motionZ);
104
            boolean flag = (int)this.prevPosX != (int)this.posX || (int)this.prevPosY != (int)this.posY || (int)this.prevPosZ != (int)this.posZ;
105
106
            if (flag || this.ticksExisted % 25 == 0)
107
            {
108
                if (this.worldObj.getBlockState(new BlockPos(this)).getBlock().getMaterial() == Material.lava)
109
                {
110
                    this.motionY = 0.20000000298023224D;
111
                    this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
112
                    this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
113
                    this.playSound("random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
114
                }
115
116
                if (!this.worldObj.isRemote)
117
                {
118
                    this.searchForOtherItemsNearby();
119
                }
120
            }
121
122
            float f = 0.98F;
123
124
            if (this.onGround)
125
            {
126
                f = this.worldObj.getBlockState(new BlockPos(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.getEntityBoundingBox().minY) - 1, MathHelper.floor_double(this.posZ))).getBlock().slipperiness * 0.98F;
127
            }
128
129
            this.motionX *= (double)f;
130
            this.motionY *= 0.9800000190734863D;
131
            this.motionZ *= (double)f;
132
133
            if (this.onGround)
134
            {
135
                this.motionY *= -0.5D;
136
            }
137
138
            if (this.age != -32768)
139
            {
140
                ++this.age;
141
            }
142
143
            this.handleWaterMovement();
144
145
            if (!this.worldObj.isRemote && this.age >= 6000)
146
            {
147
                this.setDead();
148
            }
149
        }
150
    }
151
152
    /**
153
     * Looks for other itemstacks nearby and tries to stack them together
154
     */
155
    private void searchForOtherItemsNearby()
156
    {
157
        for (EntityItem entityitem : this.worldObj.getEntitiesWithinAABB(EntityItem.class, this.getEntityBoundingBox().expand(0.5D, 0.0D, 0.5D)))
158
        {
159
            this.combineItems(entityitem);
160
        }
161
    }
162
163
    /**
164
     * Tries to merge this item with the item passed as the parameter. Returns true if successful. Either this item or
165
     * the other item will  be removed from the world.
166
     */
167
    private boolean combineItems(EntityItem other)
168
    {
169
        if (other == this)
170
        {
171
            return false;
172
        }
173
        else if (other.isEntityAlive() && this.isEntityAlive())
174
        {
175
            ItemStack itemstack = this.getEntityItem();
176
            ItemStack itemstack1 = other.getEntityItem();
177
178
            if (this.delayBeforeCanPickup != 32767 && other.delayBeforeCanPickup != 32767)
179
            {
180
                if (this.age != -32768 && other.age != -32768)
181
                {
182
                    if (itemstack1.getItem() != itemstack.getItem())
183
                    {
184
                        return false;
185
                    }
186
                    else if (itemstack1.hasTagCompound() ^ itemstack.hasTagCompound())
187
                    {
188
                        return false;
189
                    }
190
                    else if (itemstack1.hasTagCompound() && !itemstack1.getTagCompound().equals(itemstack.getTagCompound()))
191
                    {
192
                        return false;
193
                    }
194
                    else if (itemstack1.getItem() == null)
195
                    {
196
                        return false;
197
                    }
198
                    else if (itemstack1.getItem().getHasSubtypes() && itemstack1.getMetadata() != itemstack.getMetadata())
199
                    {
200
                        return false;
201
                    }
202
                    else if (itemstack1.stackSize < itemstack.stackSize)
203
                    {
204
                        return other.combineItems(this);
205
                    }
206
                    else if (itemstack1.stackSize + itemstack.stackSize > itemstack1.getMaxStackSize())
207
                    {
208
                        return false;
209
                    }
210
                    else
211
                    {
212
                        itemstack1.stackSize += itemstack.stackSize;
213
                        other.delayBeforeCanPickup = Math.max(other.delayBeforeCanPickup, this.delayBeforeCanPickup);
214
                        other.age = Math.min(other.age, this.age);
215
                        other.setEntityItemStack(itemstack1);
216
                        this.setDead();
217
                        return true;
218
                    }
219
                }
220
                else
221
                {
222
                    return false;
223
                }
224
            }
225
            else
226
            {
227
                return false;
228
            }
229
        }
230
        else
231
        {
232
            return false;
233
        }
234
    }
235
236
    /**
237
     * sets the age of the item so that it'll despawn one minute after it has been dropped (instead of five). Used when
238
     * items are dropped from players in creative mode
239
     */
240
    public void setAgeToCreativeDespawnTime()
241
    {
242
        this.age = 4800;
243
    }
244
245
    /**
246
     * Returns if this entity is in water and will end up adding the waters velocity to the entity
247
     */
248
    public boolean handleWaterMovement()
249
    {
250
        if (this.worldObj.handleMaterialAcceleration(this.getEntityBoundingBox(), Material.water, this))
251
        {
252
            if (!this.inWater && !this.firstUpdate)
253
            {
254
                this.resetHeight();
255
            }
256
257
            this.inWater = true;
258
        }
259
        else
260
        {
261
            this.inWater = false;
262
        }
263
264
        return this.inWater;
265
    }
266
267
    /**
268
     * Will deal the specified amount of damage to the entity if the entity isn't immune to fire damage. Args:
269
     * amountDamage
270
     */
271
    protected void dealFireDamage(int amount)
272
    {
273
        this.attackEntityFrom(DamageSource.inFire, (float)amount);
274
    }
275
276
    /**
277
     * Called when the entity is attacked.
278
     */
279
    public boolean attackEntityFrom(DamageSource source, float amount)
280
    {
281
        if (this.isEntityInvulnerable(source))
282
        {
283
            return false;
284
        }
285
        else if (this.getEntityItem() != null && this.getEntityItem().getItem() == Items.nether_star && source.isExplosion())
286
        {
287
            return false;
288
        }
289
        else
290
        {
291
            this.setBeenAttacked();
292
            this.health = (int)((float)this.health - amount);
293
294
            if (this.health <= 0)
295
            {
296
                this.setDead();
297
            }
298
299
            return false;
300
        }
301
    }
302
303
    /**
304
     * (abstract) Protected helper method to write subclass entity data to NBT.
305
     */
306
    public void writeEntityToNBT(NBTTagCompound tagCompound)
307
    {
308
        tagCompound.setShort("Health", (short)((byte)this.health));
309
        tagCompound.setShort("Age", (short)this.age);
310
        tagCompound.setShort("PickupDelay", (short)this.delayBeforeCanPickup);
311
312
        if (this.getThrower() != null)
313
        {
314
            tagCompound.setString("Thrower", this.thrower);
315
        }
316
317
        if (this.getOwner() != null)
318
        {
319
            tagCompound.setString("Owner", this.owner);
320
        }
321
322
        if (this.getEntityItem() != null)
323
        {
324
            tagCompound.setTag("Item", this.getEntityItem().writeToNBT(new NBTTagCompound()));
325
        }
326
    }
327
328
    /**
329
     * (abstract) Protected helper method to read subclass entity data from NBT.
330
     */
331
    public void readEntityFromNBT(NBTTagCompound tagCompund)
332
    {
333
        this.health = tagCompund.getShort("Health") & 255;
334
        this.age = tagCompund.getShort("Age");
335
336
        if (tagCompund.hasKey("PickupDelay"))
337
        {
338
            this.delayBeforeCanPickup = tagCompund.getShort("PickupDelay");
339
        }
340
341
        if (tagCompund.hasKey("Owner"))
342
        {
343
            this.owner = tagCompund.getString("Owner");
344
        }
345
346
        if (tagCompund.hasKey("Thrower"))
347
        {
348
            this.thrower = tagCompund.getString("Thrower");
349
        }
350
351
        NBTTagCompound nbttagcompound = tagCompund.getCompoundTag("Item");
352
        this.setEntityItemStack(ItemStack.loadItemStackFromNBT(nbttagcompound));
353
354
        if (this.getEntityItem() == null)
355
        {
356
            this.setDead();
357
        }
358
    }
359
360
    /**
361
     * Called by a player entity when they collide with an entity
362
     */
363
    public void onCollideWithPlayer(EntityPlayer entityIn)
364
    {
365
        if (!this.worldObj.isRemote)
366
        {
367
            ItemStack itemstack = this.getEntityItem();
368
            int i = itemstack.stackSize;
369
370
            if (this.delayBeforeCanPickup == 0 && (this.owner == null || 6000 - this.age <= 200 || this.owner.equals(entityIn.getName())) && entityIn.inventory.addItemStackToInventory(itemstack))
371
            {
372
                if (itemstack.getItem() == Item.getItemFromBlock(Blocks.log))
373
                {
374
                    entityIn.triggerAchievement(AchievementList.mineWood);
375
                }
376
377
                if (itemstack.getItem() == Item.getItemFromBlock(Blocks.log2))
378
                {
379
                    entityIn.triggerAchievement(AchievementList.mineWood);
380
                }
381
382
                if (itemstack.getItem() == Items.leather)
383
                {
384
                    entityIn.triggerAchievement(AchievementList.killCow);
385
                }
386
387
                if (itemstack.getItem() == Items.diamond)
388
                {
389
                    entityIn.triggerAchievement(AchievementList.diamonds);
390
                }
391
392
                if (itemstack.getItem() == Items.blaze_rod)
393
                {
394
                    entityIn.triggerAchievement(AchievementList.blazeRod);
395
                }
396
397
                if (itemstack.getItem() == Items.diamond && this.getThrower() != null)
398
                {
399
                    EntityPlayer entityplayer = this.worldObj.getPlayerEntityByName(this.getThrower());
400
401
                    if (entityplayer != null && entityplayer != entityIn)
402
                    {
403
                        entityplayer.triggerAchievement(AchievementList.diamondsToYou);
404
                    }
405
                }
406
407
                if (!this.isSilent())
408
                {
409
                    this.worldObj.playSoundAtEntity(entityIn, "random.pop", 0.2F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
410
                }
411
412
                entityIn.onItemPickup(this, i);
413
414
                if (itemstack.stackSize <= 0)
415
                {
416
                    this.setDead();
417
                }
418
            }
419
        }
420
    }
421
422
    /**
423
     * Get the name of this object. For players this returns their username
424
     */
425
    public String getName()
426
    {
427
        return this.hasCustomName() ? this.getCustomNameTag() : StatCollector.translateToLocal("item." + this.getEntityItem().getUnlocalizedName());
428
    }
429
430
    /**
431
     * If returns false, the item will not inflict any damage against entities.
432
     */
433
    public boolean canAttackWithItem()
434
    {
435
        return false;
436
    }
437
438
    /**
439
     * Teleports the entity to another dimension. Params: Dimension number to teleport to
440
     */
441
    public void travelToDimension(int dimensionId)
442
    {
443
        super.travelToDimension(dimensionId);
444
445
        if (!this.worldObj.isRemote)
446
        {
447
            this.searchForOtherItemsNearby();
448
        }
449
    }
450
451
    /**
452
     * Returns the ItemStack corresponding to the Entity (Note: if no item exists, will log an error but still return an
453
     * ItemStack containing Block.stone)
454
     */
455
    public ItemStack getEntityItem()
456
    {
457
        ItemStack itemstack = this.getDataWatcher().getWatchableObjectItemStack(10);
458
459
        if (itemstack == null)
460
        {
461
            if (this.worldObj != null)
462
            {
463
                logger.error("Item entity " + this.getEntityId() + " has no item?!");
464
            }
465
466
            return new ItemStack(Blocks.stone);
467
        }
468
        else
469
        {
470
            return itemstack;
471
        }
472
    }
473
474
    /**
475
     * Sets the ItemStack for this entity
476
     */
477
    public void setEntityItemStack(ItemStack stack)
478
    {
479
        this.getDataWatcher().updateObject(10, stack);
480
        this.getDataWatcher().setObjectWatched(10);
481
    }
482
483
    public String getOwner()
484
    {
485
        return this.owner;
486
    }
487
488
    public void setOwner(String owner)
489
    {
490
        this.owner = owner;
491
    }
492
493
    public String getThrower()
494
    {
495
        return this.thrower;
496
    }
497
498
    public void setThrower(String thrower)
499
    {
500
        this.thrower = thrower;
501
    }
502
503
    public int getAge()
504
    {
505
        return this.age;
506
    }
507
508
    public void setDefaultPickupDelay()
509
    {
510
        this.delayBeforeCanPickup = 10;
511
    }
512
513
    public void setNoPickupDelay()
514
    {
515
        this.delayBeforeCanPickup = 0;
516
    }
517
518
    public void setInfinitePickupDelay()
519
    {
520
        this.delayBeforeCanPickup = 32767;
521
    }
522
523
    public void setPickupDelay(int ticks)
524
    {
525
        this.delayBeforeCanPickup = ticks;
526
    }
527
528
    public boolean cannotPickup()
529
    {
530
        return this.delayBeforeCanPickup > 0;
531
    }
532
533
    public void setNoDespawn()
534
    {
535
        this.age = -6000;
536
    }
537
538
    public void func_174870_v()
539
    {
540
        this.setInfinitePickupDelay();
541
        this.age = 5999;
542
    }
543
}
544

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