Skip to content

Commit

Permalink
handle clicks outside inventory and non modifying clicks (#661)
Browse files Browse the repository at this point in the history
# Objective

Fixes some re-syncs because the validation did not accept the now
specific cases (all of the affected modifications where cases where the
user did not modify the inventory)

Cases like:
- user clicks without an item on the cursor outside the inventory
- user clicks on the margin area without an item held
- user clicks on an empty slot when not holding anything 
- user uses hotbar keybinds on an empty slot to an empty hotbar slot

This causes unneccery re-syncs to the client and also unneccery logs
indicating some illegal client packets, while these packets are totaly
fine

# Solution

adjusted packet validation logic to allow these cases, but also check
for "conservation of mass" when these special magic slot ids shows up
modified the handling code to basically ignore these changes, (I guess
there is room for improvement here, as some event would probably be nice
to publish in these cases?)
  • Loading branch information
lukashermansson authored Oct 12, 2024
1 parent 70a266a commit b1f18a7
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 129 deletions.
11 changes: 9 additions & 2 deletions crates/valence_inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ fn handle_click_slot(
continue;
}

if pkt.slot_idx < 0 && pkt.mode == ClickMode::Click {
if pkt.slot_idx == -999 && pkt.mode == ClickMode::Click {
// The client is dropping the cursor item by clicking outside the window.

let stack = std::mem::take(&mut cursor_item.0);
Expand Down Expand Up @@ -926,6 +926,10 @@ fn handle_click_slot(

continue;
}
if pkt.slot_idx == -999 {
// The player was just clicking outside the inventories without holding an item
continue;
}

if (0_i16..target_inventory.slot_count() as i16).contains(&pkt.slot_idx) {
// The player is dropping an item from another inventory.
Expand Down Expand Up @@ -1014,7 +1018,10 @@ fn handle_click_slot(
});
continue;
}

if pkt.slot_idx == -999 {
// The player was just clicking outside the inventories without holding an item
continue;
}
let stack = client_inv.slot(pkt.slot_idx as u16);

if !stack.is_empty() {
Expand Down
Loading

0 comments on commit b1f18a7

Please sign in to comment.