Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-129909

Players in spectator mode continue to consume foods and liquids shortly after switching game modes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • Minecraft 1.12.2, Minecraft 18w20c, Minecraft 1.13.1, Minecraft 1.13.2, Minecraft 18w43c, Minecraft 19w12b, Minecraft 19w13b, Minecraft 19w14a, Minecraft 19w14b, Minecraft 1.14.3, 1.15 Pre-release 4, 1.16.2, 20w46a, 21w03a, 1.16.5, 21w05b, 21w06a, 21w08b, 21w11a, 21w14a, 1.17.1, 1.18 Pre-release 4, 1.18, 1.18.1, 22w03a, 22w05a, 22w06a, 22w07a, 1.18.2, 22w17a, 22w18a, 1.19 Pre-release 1, 1.19, 1.19.2, 22w43a, 1.19.3, 1.19.4, 1.20, 1.20.1, 23w32a, 1.20.4, 23w51b, 24w03b
    • Confirmed
    • Spectator
    • Player
    • Low
    • Platform

      The Bug:

      Players in spectator mode continue to consume foods and liquids shortly after switching game modes.

      Steps to Reproduce:

      1. Obtain some food, for example, some bread.
      2. Begin eating the bread and as you're doing this, quickly switch into spectator mode by pressing "F3 + N".
      3. 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 Avoma 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();

        1. 2018-05-19 12-28-52.mp4
          5.68 MB
        2. MC-129909.mp4
          4.62 MB
        3. MC-129909.png
          MC-129909.png
          420 kB
        4. MC-129909 - Current Code.png
          MC-129909 - Current Code.png
          51 kB
        5. MC-129909 - Fixed Code.png
          MC-129909 - Fixed Code.png
          54 kB

            Unassigned Unassigned
            Avoma [Mod] Avoma
            Votes:
            16 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              CHK: