From e1a7d4e7eed37bba4606d974767f8b0ee91d05cc Mon Sep 17 00:00:00 2001 From: Edvin Bryntesson Date: Fri, 23 Aug 2024 11:51:37 +0200 Subject: [PATCH] add serverbound packet SContainerClose --- pumpkin-inventory/src/lib.rs | 34 +++++++++++++++++++ pumpkin-protocol/src/server/play/mod.rs | 2 ++ .../src/server/play/s_close_container.rs | 8 +++++ pumpkin/src/client/player_packet.rs | 13 +++++++ 4 files changed, 57 insertions(+) create mode 100644 pumpkin-protocol/src/server/play/s_close_container.rs diff --git a/pumpkin-inventory/src/lib.rs b/pumpkin-inventory/src/lib.rs index 0230ac189..3595bd6c2 100644 --- a/pumpkin-inventory/src/lib.rs +++ b/pumpkin-inventory/src/lib.rs @@ -52,3 +52,37 @@ impl WindowType { "WINDOW TITLE" } } +impl TryFrom for WindowType { + type Error = (); + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(WindowType::Generic9x1), + 1 => Ok(WindowType::Generic9x2), + 2 => Ok(WindowType::Generic9x3), + 3 => Ok(WindowType::Generic9x4), + 4 => Ok(WindowType::Generic9x5), + 5 => Ok(WindowType::Generic9x6), + 6 => Ok(WindowType::Generic3x3), + 7 => Ok(WindowType::Craft3x3), + 8 => Ok(WindowType::Anvil), + 9 => Ok(WindowType::Beacon), + 10 => Ok(WindowType::BlastFurnace), + 11 => Ok(WindowType::BrewingStand), + 12 => Ok(WindowType::CraftingTable), + 13 => Ok(WindowType::EnchantmentTable), + 14 => Ok(WindowType::Furnace), + 15 => Ok(WindowType::Grindstone), + 16 => Ok(WindowType::Hopper), + 17 => Ok(WindowType::Lectern), + 18 => Ok(WindowType::Loom), + 19 => Ok(WindowType::Merchant), + 20 => Ok(WindowType::ShulkerBox), + 21 => Ok(WindowType::SmithingTable), + 22 => Ok(WindowType::Smoker), + 23 => Ok(WindowType::CartographyTable), + 24 => Ok(WindowType::Stonecutter), + _ => Err(()), + } + } +} diff --git a/pumpkin-protocol/src/server/play/mod.rs b/pumpkin-protocol/src/server/play/mod.rs index c29a11775..2cccb9c2e 100644 --- a/pumpkin-protocol/src/server/play/mod.rs +++ b/pumpkin-protocol/src/server/play/mod.rs @@ -1,6 +1,7 @@ mod s_chat_command; mod s_chat_message; mod s_client_information; +mod s_close_container; mod s_confirm_teleport; mod s_interact; mod s_ping_request; @@ -17,6 +18,7 @@ mod s_use_item_on; pub use s_chat_command::*; pub use s_chat_message::*; pub use s_client_information::*; +pub use s_close_container::*; pub use s_confirm_teleport::*; pub use s_interact::*; pub use s_ping_request::*; diff --git a/pumpkin-protocol/src/server/play/s_close_container.rs b/pumpkin-protocol/src/server/play/s_close_container.rs new file mode 100644 index 000000000..5ba6a5698 --- /dev/null +++ b/pumpkin-protocol/src/server/play/s_close_container.rs @@ -0,0 +1,8 @@ +use pumpkin_macros::packet; +use serde::Serialize; + +#[derive(Serialize)] +#[packet(0x0F)] +pub struct SCloseContainer { + pub window_id: u8, +} diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 52d1defad..70522718a 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -9,6 +9,8 @@ use crate::{ use num_traits::FromPrimitive; use pumpkin_core::text::TextComponent; use pumpkin_entity::EntityId; +use pumpkin_inventory::WindowType; +use pumpkin_protocol::server::play::SCloseContainer; use pumpkin_protocol::{ client::play::{ Animation, CAcknowledgeBlockChange, CBlockUpdate, CEntityAnimation, CEntityVelocity, @@ -414,4 +416,15 @@ impl Client { inventory.set_slot(packet.slot as usize, packet.clicked_item.to_item(), false); } + + // TODO: + // This function will in the future be used to keep track of if the client is in a valid state. + // But this is not possible yet + pub fn handle_close_container(&mut self, packet: SCloseContainer) { + // window_id 0 represents both 9x1 Generic AND inventory here + let Ok(_window_type) = WindowType::try_from(packet.window_id) else { + self.kick("Invalid window ID"); + return; + }; + } }