mojira.dev
MC-120643

Picking up an incomplete stack of items is not correctly detected

The bug

Picking up only a part of an item stack of an item entity because your inventory is nearly full is not correctly detected by the server causing:

  • Server-side:

    • Missing statistics change

  • Client-side:

    • Missing sound and animation

    • Item stack of item entity not updated

How to reproduce

It's easiest to reproduce this by starting in creative mode and then switching to survival partway through.

  1. Fill all of your inventory except for the first hotbar slot with one item (e.g. wooden swords), fill the hotbar slot with a nearly full stack of for example stone

    /give @s wooden_sword 36
    /item replace entity @s hotbar.0 with minecraft:stone 63
  2. Switch to survival mode (This is needed because in creative you can pick items up even when the inventory is full.)

    /gamemode survival
  3. Summon an item entity containing two stone items

    /summon item ~ ~ ~ {Item:{id:"stone",Count:2b}}

    → ❌ The item entity still shows two items, there was no sound, no animation, etc.

Code analysis

Based on 1.12.2 decompiled using MCP 9.40

The method net.minecraft.entity.item.EntityItem.onCollideWithPlayer(EntityPlayer) tests if net.minecraft.entity.player.InventoryPlayer.addItemStackToInventory(ItemStack) returns true. Since this is only the case if the complete stack was picked up, it should instead test if the stack size decreased.

The following shows the problems with the current code:

ItemStack itemstack = this.getItem();
Item item = itemstack.getItem();
int i = itemstack.getCount();

// Don't test for entityIn.inventory.addItemStackToInventory(itemstack) == true
if (this.pickupDelay == 0 && (this.owner == null || 6000 - this.age <= 200 || this.owner.equals(entityIn.getName())) && entityIn.inventory.addItemStackToInventory(itemstack))
{
    // Change this, picked up count can be < i
    entityIn.onItemPickup(this, i);

    if (itemstack.isEmpty())
    {
        this.setDead();
        itemstack.setCount(i);
    }

    // Change this, picked up count can be < i
    entityIn.addStat(StatList.getObjectsPickedUpStats(item), i);
}

Linked issues

Attachments

Comments 10

Can confirm in 20w48a. For Step 2, since the /replaceitem command was replaced with /item, here's the updated use.

/item entity @s hotbar.0 replace stone 63

Can confirm in 21w05b.

Can confirm for 1.16.5

Here's the updated command for the first reproduction step:

/item replace entity @s hotbar.0 with minecraft:stone 63

Can confirm in 1.17.1.

Can confirm in 1.18.1.

Can confirm in 1.19.

Can confirm in 1.19.1.

Can confirm in 1.21.7.

pokechu22

(Unassigned)

Confirmed

Platform

Normal

Networking

animation, item, item-entity, item-pickup, sound, statistics

Minecraft 1.12.1, Minecraft 1.12.2 Pre-Release 1, Minecraft 1.12.2 Pre-Release 2, Minecraft 1.12.2, Minecraft 17w43a, ..., 1.19.4, 1.20.1, 1.20.6, 24w19b, 1.21

Retrieved