mojira.dev
MC-143474

Respawning causes your hotbar to reset to the first space

The Bug:

Hotbar selection resets to slot 0 upon death.

Steps to Reproduce:

  • Give yourself two different items held in different hotbar slots.

/item replace entity @s hotbar.0 with minecraft:dirt
/item replace entity @s hotbar.1 with minecraft:deepslate
  • Hold the deepslate in your hand and set the "keepInventory" gamerule to "true".

/gamerule keepInventory true
  • Run the "/kill" command.

  • Take note as to whether or not hotbar selection resets to slot 0 upon death.

Observed Behavior:

Hotbar selection resets to slot 0 upon death.

Expected Behavior:

Hotbar selection would not reset to slot 0 upon death.

Linked issues

Attachments

Comments 13

Can confirm in 20w51a.

Can confirm in 21w03a.

Can confirm in 21w05b.

Can confirm in 21w06a.

3 more comments

Can confirm in 1.18.2 and 22w18a.

Code analysis

Yarn 1.19 mappings - net.minecraft.client.network.ClientPlayNetworkHandler

 

@Override
public void onPlayerRespawn(PlayerRespawnS2CPacket packet) {
    // ...

    ClientPlayerEntity clientPlayerEntity2 = this.client.interactionManager.createPlayer(this.world, clientPlayerEntity.getStatHandler(), clientPlayerEntity.getRecipeBook(), clientPlayerEntity.isSneaking(), clientPlayerEntity.isSprinting());
    clientPlayerEntity2.setId(i);
    this.client.player = clientPlayerEntity2;

    // ...
}

As you can see this is where the player is re-created. In this method there is no call to set the selected slot to the previous. I suggest the following.

 

 

@Override
public void onPlayerRespawn(PlayerRespawnS2CPacket packet) {
// ...

   ClientPlayerEntity clientPlayerEntity2 = this.client.interactionManager.createPlayer(this.world, clientPlayerEntity.getStatHandler(), clientPlayerEntity.getRecipeBook(), clientPlayerEntity.isSneaking(), clientPlayerEntity.isSprinting()); 
    clientPlayerEntity2.setId(i); 

    // add below code
    clientPlayerEntity2.getInventory().selectedSlot = this.client.player.getInventory().selectedSlot;
      // finish
    
    this.client.player = clientPlayerEntity2;

// ...
}

 

Can confirm in 1.19.2.

Why should it be the case that the hotbar slot is preserved? Other things, like orientation, aren't. If keepInventory is disabled then it's extra silly to preserve the slot.

Map makers may trigger things based on selected slot/item, causing you to unwillingly trigger said system.

GreenPlanet669

(Unassigned)

Confirmed

UI

Minecraft 1.13.2, Minecraft 19w05a, Minecraft 19w12b, Minecraft 19w13b, Minecraft 19w14a, ..., 1.19.2, 1.20.1, 1.20.4, 23w51b, 1.21.4

Retrieved