[SPIGOT-1341] Cancelling Right Click Cake (PlayerInteractEvent) Client Hunger Goes Up Server Hunger Does Not Created: 15/Dec/15  Updated: 01/Jul/16  Resolved: 01/Jul/16

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: BillyGalbreath Assignee: Unassigned
Resolution: Fixed Votes: 2
Labels: None
Environment:

git-Spigot-5f38d38-18fbb24



 Description   

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();
                }
            }
        }
    }

Generated at Fri Mar 28 13:32:49 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.