From 86085a4c9c2665186de99cd2a9a849da6263e4c2 Mon Sep 17 00:00:00 2001 From: AviiNL Date: Fri, 8 Sep 2023 16:22:11 +0200 Subject: [PATCH 1/4] update player selected slot --- crates/valence_inventory/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/valence_inventory/src/lib.rs b/crates/valence_inventory/src/lib.rs index 4498b9aac..f30d4dc0d 100644 --- a/crates/valence_inventory/src/lib.rs +++ b/crates/valence_inventory/src/lib.rs @@ -54,6 +54,7 @@ impl Plugin for InventoryPlugin { PostUpdate, ( update_client_on_close_inventory.before(update_open_inventories), + update_player_selected_slot, update_open_inventories, update_player_inventories, ) @@ -397,6 +398,8 @@ impl ClientInventoryState { #[derive(Debug, Clone, Copy, PartialEq, Eq, Component, Deref)] pub struct HeldItem { held_item_slot: u16, + #[deref(ignore)] + changed: bool, } impl HeldItem { @@ -405,6 +408,17 @@ impl HeldItem { pub fn slot(&self) -> u16 { self.held_item_slot } + + pub fn set_slot(&mut self, slot: u16) { + // temp + assert!( + (36..=44).contains(&slot), + "slot index of {slot} out of bounds" + ); + + self.held_item_slot = slot; + self.changed = true; + } } /// The item stack that the client thinks it's holding under the mouse @@ -578,6 +592,7 @@ fn init_new_client_inventories(clients: Query>, mut comman HeldItem { // First slot of the hotbar. held_item_slot: 36, + changed: false, }, )); } @@ -659,6 +674,20 @@ fn update_player_inventories( } } +fn update_player_selected_slot(mut clients: Query<(&mut Client, &mut HeldItem)>) { + for (mut client, mut held_item) in &mut clients { + if held_item.changed { + client.write_packet( + &valence_server::protocol::packets::play::UpdateSelectedSlotS2c { + slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8, + }, + ); + + held_item.changed = false; + } + } +} + /// Handles the `OpenInventory` component being added to a client, which /// indicates that the client is now viewing an inventory, and sends inventory /// updates to the client when the inventory is modified. From 10ce1bd496283f4f72f1a333bd32fbf8ef4748ac Mon Sep 17 00:00:00 2001 From: AviiNL Date: Fri, 8 Sep 2023 16:25:55 +0200 Subject: [PATCH 2/4] Minor cleanup --- crates/valence_inventory/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/valence_inventory/src/lib.rs b/crates/valence_inventory/src/lib.rs index f30d4dc0d..e99f4c58c 100644 --- a/crates/valence_inventory/src/lib.rs +++ b/crates/valence_inventory/src/lib.rs @@ -35,6 +35,7 @@ pub use valence_server::protocol::packets::play::player_action_c2s::PlayerAction use valence_server::protocol::packets::play::{ ClickSlotC2s, CloseHandledScreenC2s, CloseScreenS2c, CreativeInventoryActionC2s, InventoryS2c, OpenScreenS2c, PlayerActionC2s, ScreenHandlerSlotUpdateS2c, UpdateSelectedSlotC2s, + UpdateSelectedSlotS2c, }; use valence_server::protocol::{VarInt, WritePacket}; use valence_server::text::IntoText; @@ -674,14 +675,14 @@ fn update_player_inventories( } } +/// Handles the `HeldItem` component being changed on a client, which +/// indicates that the server has changed the selected hotbar slot. fn update_player_selected_slot(mut clients: Query<(&mut Client, &mut HeldItem)>) { for (mut client, mut held_item) in &mut clients { if held_item.changed { - client.write_packet( - &valence_server::protocol::packets::play::UpdateSelectedSlotS2c { - slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8, - }, - ); + client.write_packet(&UpdateSelectedSlotS2c { + slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8, + }); held_item.changed = false; } From d241e09aed97b9060e63acde9925ce3ca2642d9c Mon Sep 17 00:00:00 2001 From: AviiNL Date: Fri, 8 Sep 2023 18:17:37 +0200 Subject: [PATCH 3/4] better change detection. --- crates/valence_inventory/src/lib.rs | 33 ++++++++++++----------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/crates/valence_inventory/src/lib.rs b/crates/valence_inventory/src/lib.rs index e99f4c58c..32d6f2ade 100644 --- a/crates/valence_inventory/src/lib.rs +++ b/crates/valence_inventory/src/lib.rs @@ -399,8 +399,6 @@ impl ClientInventoryState { #[derive(Debug, Clone, Copy, PartialEq, Eq, Component, Deref)] pub struct HeldItem { held_item_slot: u16, - #[deref(ignore)] - changed: bool, } impl HeldItem { @@ -418,7 +416,6 @@ impl HeldItem { ); self.held_item_slot = slot; - self.changed = true; } } @@ -593,7 +590,6 @@ fn init_new_client_inventories(clients: Query>, mut comman HeldItem { // First slot of the hotbar. held_item_slot: 36, - changed: false, }, )); } @@ -675,20 +671,6 @@ fn update_player_inventories( } } -/// Handles the `HeldItem` component being changed on a client, which -/// indicates that the server has changed the selected hotbar slot. -fn update_player_selected_slot(mut clients: Query<(&mut Client, &mut HeldItem)>) { - for (mut client, mut held_item) in &mut clients { - if held_item.changed { - client.write_packet(&UpdateSelectedSlotS2c { - slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8, - }); - - held_item.changed = false; - } - } -} - /// Handles the `OpenInventory` component being added to a client, which /// indicates that the client is now viewing an inventory, and sends inventory /// updates to the client when the inventory is modified. @@ -1246,6 +1228,17 @@ pub struct UpdateSelectedSlotEvent { pub slot: u8, } +/// Handles the `HeldItem` component being changed on a client, which +/// indicates that the server has changed the selected hotbar slot. +fn update_player_selected_slot(mut clients: Query<(&mut Client, &HeldItem), Changed>) { + for (mut client, held_item) in &mut clients { + client.write_packet(&UpdateSelectedSlotS2c { + slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8, + }); + } +} + +/// Client to Server HeldItem Slot fn handle_update_selected_slot( mut packets: EventReader, mut clients: Query<&mut HeldItem>, @@ -1253,11 +1246,13 @@ fn handle_update_selected_slot( ) { for packet in packets.iter() { if let Some(pkt) = packet.decode::() { - if let Ok(mut held) = clients.get_mut(packet.client) { + if let Ok(mut mut_held) = clients.get_mut(packet.client) { + let held = mut_held.bypass_change_detection(); if pkt.slot > 8 { // The client is trying to interact with a slot that does not exist, ignore. continue; } + held.held_item_slot = convert_hotbar_slot_id(pkt.slot); events.send(UpdateSelectedSlotEvent { From b728362440df4bf0f116fa8c3c196599c658da5c Mon Sep 17 00:00:00 2001 From: AviiNL Date: Fri, 8 Sep 2023 20:26:37 +0200 Subject: [PATCH 4/4] Update crates/valence_inventory/src/lib.rs Co-authored-by: Carson McManus --- crates/valence_inventory/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/valence_inventory/src/lib.rs b/crates/valence_inventory/src/lib.rs index 32d6f2ade..0c7a122af 100644 --- a/crates/valence_inventory/src/lib.rs +++ b/crates/valence_inventory/src/lib.rs @@ -1228,7 +1228,7 @@ pub struct UpdateSelectedSlotEvent { pub slot: u8, } -/// Handles the `HeldItem` component being changed on a client, which +/// Handles the `HeldItem` component being changed on a client entity, which /// indicates that the server has changed the selected hotbar slot. fn update_player_selected_slot(mut clients: Query<(&mut Client, &HeldItem), Changed>) { for (mut client, held_item) in &mut clients {