From 24ac4e149bac81e29676bc9175c264f4012b9b42 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:13:21 +0200 Subject: [PATCH] Swap and sync the off hand on a server On a server the F key swapped the hands only on the client, and the inventory menu's off-hand slot (InventoryMenu slot 45) was missing from the client's slot table, so clicks on it went nowhere and the server's updates of it were dropped (#543). - F sends ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND and predicts nothing, as vanilla's Minecraft.handleKeybinds does (not for spectators). - Inventory menu slot 45 is the off hand; SetPlayerInventory slot 40 too. - The server swaps only for non-spectators and stops the item in use (ServerGamePacketListenerImpl.handlePlayerAction). Fixes #543 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 8 ++++- crates/horizon-client/src/game/lan_actions.rs | 7 +++- crates/horizon-client/src/game/remote.rs | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5ffcd9124..ffec6b6f8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8860,7 +8860,13 @@ impl Game<'_> { } _ => {} }, - // key.swapOffhand: ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND. + // key.swapOffhand: on a server, ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND + // (the server swaps the hands and sends both slots back; spectators can't). + KeyCode::KeyF if self.screen == Screen::Playing && self.remote.is_some() => { + if !self.spectator() { + self.remote_swap_hands(); + } + } KeyCode::KeyF if self.screen == Screen::Playing => { std::mem::swap(&mut self.offhand, &mut self.inventory[self.selected]); self.using = None; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 6bf76f160..aab414156 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -395,10 +395,15 @@ impl Game<'_> { } }); } - 7 => { + // SWAP_ITEM_WITH_OFFHAND: the hands swap and the item in use stops + // (ServerGamePacketListenerImpl.handlePlayerAction), not for spectators. + 7 if !spectator => { let g = &mut host.hub.guests[gi]; let slot = g.selected; std::mem::swap(&mut g.inventory[slot], &mut g.offhand); + if let Some(play) = host.play.get_mut(&conn) { + play.using = None; + } } _ => {} }, diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 525d6d2bb..438854e87 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -746,6 +746,8 @@ impl Game<'_> { 5..=8 => Some(500 + s - 5), 9..=35 => Some(s), 36..=44 => Some(s - 36), + // InventoryMenu.SHIELD_SLOT: the off hand. + 45 => Some(504), _ => None, }, "minecraft:crafting" => match s { @@ -1245,6 +1247,8 @@ impl Game<'_> { match p.slot { 0..=35 => self.inventory[p.slot as usize] = stack, 36..=39 => self.armor[3 - (p.slot - 36) as usize] = stack, + // Inventory.SLOT_OFFHAND. + 40 => self.offhand = stack, _ => {} } self.remote_synced(); @@ -2521,6 +2525,17 @@ impl Game<'_> { })); } } + /// Minecraft.handleKeybinds, key.swapOffhand: the server swaps the hands + /// (ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND at BlockPos.ZERO, facing down) and + /// sends both slots back; nothing is predicted. + pub(super) fn remote_swap_hands(&mut self) { + self.send(S::PlayerAction(play::PlayerAction { + action: 7, + pos: [0; 3], + direction: 0, + sequence: 0, + })); + } /// A container click: the server answers with the authoritative slots. pub(super) fn remote_click(&mut self, right: bool) { let Some(size) = self @@ -2941,3 +2956,20 @@ impl Game<'_> { })) } } + +#[cfg(test)] +mod tests { + use super::Game; + + #[test] + fn every_inventory_menu_slot_is_a_game_slot_and_45_is_the_off_hand() { + // InventoryMenu: result, 2x2 grid, armour, 27 + 9 inventory slots, then the off hand. + assert_eq!(Game::menu_slot("inventory", 45), Some(504)); + let mut seen = std::collections::HashSet::new(); + for slot in 0..46 { + let internal = Game::menu_slot("inventory", slot); + assert!(internal.is_some_and(|i| seen.insert(i)), "slot {slot}"); + } + assert_eq!(Game::menu_slot("inventory", 46), None); + } +}