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
is duplicated by 1
Attachments
Comments 13
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;
// ...
}
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.
Affects 20w30a