Cancelling Right Click Cake (PlayerInteractEvent) Client Hunger Goes Up Server Hunger Does Not

    • Type: Bug
    • Resolution: Fixed
    • Priority: Minor
    • None
    • Affects Version/s: None
    • None
    • Environment:

      git-Spigot-5f38d38-18fbb24

      Cancelling the PlayerInteractEvent when a player right clicks a CAKE_BLOCK messes up the client side hunger bar.

      On the server the hunger does not go up, but on the client the hunger bar shows it goes up. After the next food level change the client hunger goes back to matching the server's copy.

      Test Code:

          @EventHandler
          public void onEatCake(PlayerInteractEvent event) {
              if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                  if (event.getClickedBlock().getType().equals(Material.CAKE_BLOCK)) {
                      event.setCancelled(true);
                      event.getPlayer().sendMessage("Cancelled Event.");
                  }
              }
          }
      

      More information: Sending an update_health packet with the current food level seems to force fix the issue, somewhat. The client still shows the hunger go up for a split second until the packet is received then it goes back to normal.

      Example code (using ProtocolLib)

          @EventHandler
          public void onEatCake(PlayerInteractEvent event) {
              if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                  if (event.getClickedBlock().getType().equals(Material.CAKE_BLOCK)) {
                      event.setCancelled(true);
                      event.getPlayer().sendMessage("Cancelled Event.");
      
                      PacketContainer packet = new PacketContainer(PacketType.Play.Server.UPDATE_HEALTH);
                      packet.getFloat().write(0, (float) player.getHealth());
                      packet.getIntegers().write(0, player.getFoodLevel());
                      packet.getFloat().write(1, player.getSaturation());
      
                      try {
                          ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet);
                      } catch (InvocationTargetException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
      

            Assignee:
            Unassigned
            Reporter:
            BillyGalbreath
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: