Merge pull request #2956 from gemakfy/fix/creative-f-offhand-crash

Fix creative inventory off-hand swap crash on Key F
This commit is contained in:
rdbt
2026-10-28 00:32:16 +00:00
committed by GitHub
3 changed files with 35 additions and 14 deletions
+12 -3
View File
@@ -575,6 +575,8 @@ pub enum DisplayClick {
Nothing,
/// SWAP: the hotbar slot gets a full stack of the item.
Hotbar(usize, ItemStack),
/// SWAP with button 40 (Key F): the off-hand gets a full stack of the item.
Offhand(ItemStack),
/// THROW: a copy of the item is dropped.
Drop(ItemStack),
}
@@ -596,8 +598,11 @@ pub fn click_display(
match input {
Input::Swap => {
return match shown {
Some(s) => DisplayClick::Hotbar(button as usize, copy(s, max(s))),
None => DisplayClick::Nothing,
Some(s) if button == 40 => DisplayClick::Offhand(copy(s, max(s))),
Some(s) if (0..9).contains(&button) => {
DisplayClick::Hotbar(button as usize, copy(s, max(s)))
}
Some(_) | None => DisplayClick::Nothing,
};
}
Input::Clone => {
@@ -720,11 +725,15 @@ mod tests {
display(Some(stack("ender_pearl", 9)), 1, Pickup).0,
Some(stack("ender_pearl", 8))
);
// Number keys fill a hotbar slot, Q drops copies, pick block clones.
// Number keys fill a hotbar slot, F fills off-hand, Q drops copies, pick block clones.
assert_eq!(
display(None, 3, Swap).1,
DisplayClick::Hotbar(3, stack("stone", 64))
);
assert_eq!(
display(None, 40, Swap).1,
DisplayClick::Offhand(stack("stone", 64))
);
assert_eq!(
display(None, 0, Throw).1,
DisplayClick::Drop(stack("stone", 1))
+5 -2
View File
@@ -6611,9 +6611,12 @@ impl Game<'_> {
let catalog = self.res.items();
let max = |s: &ItemStack| catalog.get(&s.item).map_or(64, |i| i.max_stack);
match click_display(&mut self.carried, shown, max, button, input) {
DisplayClick::Hotbar(i, stack) => self.inventory[i] = Some(stack),
DisplayClick::Hotbar(i, stack) if i < self.inventory.len() => {
self.inventory[i] = Some(stack);
}
DisplayClick::Offhand(stack) => self.offhand = Some(stack),
DisplayClick::Drop(stack) => self.player_drop(stack),
DisplayClick::Nothing => {}
_ => {}
}
}
/// Several clicks on one menu instance (a quick craft drag is three).
+18 -9
View File
@@ -213,7 +213,8 @@ pub(super) struct Remote {
/// `LocalPlayer.showDeathScreen`: off with `immediate_respawn`.
show_death_screen: bool,
player_type: Option<Arc<MobType>>,
/// Inventory as last synchronised, for creative-mode slot updates.
/// Inventory (then the off hand) as last synchronised, for creative-mode
/// slot updates.
synced: Vec<Option<ItemStack>>,
decode_errors: u32,
clock: Option<i32>,
@@ -510,7 +511,7 @@ impl Game<'_> {
death_message: None,
show_death_screen: true,
player_type,
synced: vec![None; 36],
synced: vec![None; 37],
decode_errors: 0,
clock: None,
clock_rate: 1.0,
@@ -4307,12 +4308,14 @@ impl Game<'_> {
{
out.push(packet);
}
// Creative inventory edits are sent slot by slot (SetCreativeModeSlot).
// Creative inventory edits are sent slot by slot (SetCreativeModeSlot);
// `synced[36]` is the off hand.
let mut creative_slots = Vec::new();
if self.creative {
for i in 0..36 {
if self.inventory[i] != r.synced[i] {
r.synced[i] = self.inventory[i].clone();
for i in 0..37 {
let stack = self.inventory.get(i).unwrap_or(&self.offhand);
if *stack != r.synced[i] {
r.synced[i] = stack.clone();
creative_slots.push(i);
}
}
@@ -4321,8 +4324,13 @@ impl Game<'_> {
self.send(packet);
}
for i in creative_slots {
let slot = if i < 9 { 36 + i } else { i } as i16;
let item = self.to_network(&self.inventory[i]);
// InventoryMenu's slots: the hotbar is 36..=44, the off hand 45.
let slot = match i {
0..9 => 36 + i,
36 => 45,
_ => i,
} as i16;
let item = self.to_network(self.inventory.get(i).unwrap_or(&self.offhand));
self.send(S::SetCreativeModeSlot(play::SetCreativeModeSlot {
slot,
item,
@@ -4331,7 +4339,8 @@ impl Game<'_> {
}
/// Inventory as the server last described it.
fn remote_synced(&mut self) {
let inventory = self.inventory.clone();
let mut inventory = self.inventory.clone();
inventory.push(self.offhand.clone());
if let Some(r) = self.remote.as_mut() {
r.synced = inventory;
}