The Bug:
Players in spectator mode continue to consume foods and liquids shortly after switching game modes.
Steps to Reproduce:
Obtain some food, for example, some bread.
Begin eating the bread and as you're doing this, quickly switch into spectator mode by pressing "F3 + N".
Take note as to whether or not players in spectator mode continue to consume foods and liquids shortly after switching game modes.
Observed Behavior:
Players in spectator mode continue to consume foods and liquids shortly after switching game modes.
Expected Behavior:
Players in spectator mode would not continue to consume foods and liquids shortly after switching game modes.
Here's a code analysis along with a fix regarding this issue.
Code Analysis:
Code analysis by @unknown can be found below.
The following is based on a decompiled version of Minecraft 1.19.2 using MCP-Reborn.
net.minecraft.server.level.ServerPlayer.java
public class ServerPlayer extends Player {
...
public boolean setGameMode(GameType gameType) {
if (!this.gameMode.changeGameModeForPlayer(gameType)) {
return false;
} else {
this.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, (float)gameType.getId()));
if (gameType == GameType.SPECTATOR) {
this.removeEntitiesOnShoulder();
this.stopRiding();
} else {
this.setCamera(this);
}
this.onUpdateAbilities();
this.updateEffectVisibility();
return true;
}
}
...
If we look at the above class, we can see that only two methods are called when a player switches into spectator mode. These methods are removeEntitiesOnShoulder()
and stopRiding()
. This basically means that when a player changes into spectator mode, any entities that are riding on their shoulders will be dismounted, along with the player themselves being dismounted if they are riding any entity. As a result of only these two methods being called, the player can continue to use items even after switching into spectator mode.
Fix:
Simply calling the stopUsingItem()
method where appropriate within this piece of code will stop players from using items upon switching into spectator mode, thus resolving this problem. The following line code can be used to fix this issue.
this.stopUsingItem();
Linked issues
is duplicated by 2
Attachments
Comments 12
I'd like to request ownership of this ticket since the current reporter has been inactive since October of 2020.
Following on from my code analysis, I've double-checked my proposed fix and I can confidently confirm that it's fully functioning and works as expected, so I've attached two screenshots to this report, one of which shows the current code and the other that shows the fixed code. I feel this information may be quite insightful hence my reasoning for providing it.
[media][media]
⚠️ Please do not mark issues as private, unless your bug report is an exploit or contains information about your username or server.