From 8b0b86bbcfdf59ecc8e6785028cfbd43f3e4360c Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 5 May 2023 16:54:42 +0000 Subject: add Client::inventory --- azalea/src/container.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'azalea/src/container.rs') diff --git a/azalea/src/container.rs b/azalea/src/container.rs index 0016caad..24774d43 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -21,10 +21,12 @@ impl Plugin for ContainerPlugin { pub trait ContainerClientExt { async fn open_container(&mut self, pos: BlockPos) -> Option; + fn inventory(&mut self) -> Option; } impl ContainerClientExt for Client { - /// Open a container in the world, like a chest. + /// Open a container in the world, like a chest. Use [`Client::inventory`] + /// to open your own inventory. /// /// ``` /// # use azalea::prelude::*; @@ -72,12 +74,36 @@ impl ContainerClientExt for Client { }) } } + + /// Open the player's inventory. This will return None if another + /// container is open. + /// + /// Note that this will send a packet to the server once it's dropped. Also, + /// due to how it's implemented, you could call this function multiple times + /// while another inventory handle already exists (but you shouldn't). + fn inventory(&mut self) -> Option { + let ecs = self.ecs.lock(); + let inventory = ecs + .get::(self.entity) + .expect("no inventory"); + + if inventory.id == 0 { + Some(ContainerHandle { + id: 0, + client: self.clone(), + }) + } else { + None + } + } } /// A handle to the open container. The container will be closed once this is /// dropped. pub struct ContainerHandle { - pub id: u8, + /// The id of the container. If this is 0, that means it's the player's + /// inventory. + id: u8, client: Client, } impl Drop for ContainerHandle { @@ -96,6 +122,13 @@ impl Debug for ContainerHandle { } } impl ContainerHandle { + /// Get the id of the container. If this is 0, that means it's the player's + /// inventory. Otherwise, the number isn't really meaningful since only one + /// container can be open at a time. + pub fn id(&self) -> u8 { + self.id + } + /// Returns the menu of the container. If the container is closed, this /// will return `None`. pub fn menu(&self) -> Option { @@ -103,8 +136,14 @@ impl ContainerHandle { let inventory = ecs .get::(self.client.entity) .expect("no inventory"); + + // this also makes sure we can't access the inventory while a container is open if inventory.id == self.id { - Some(inventory.container_menu.clone().unwrap()) + if self.id == 0 { + Some(inventory.inventory_menu.clone()) + } else { + Some(inventory.container_menu.clone().unwrap()) + } } else { None } -- cgit v1.2.3 From 3702b2cb2117b586c41a79d2f74864a99c144bdf Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 5 May 2023 17:01:15 +0000 Subject: rename Client::inventory to open_inventory --- azalea/src/container.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'azalea/src/container.rs') diff --git a/azalea/src/container.rs b/azalea/src/container.rs index 24774d43..c8849cdd 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -21,7 +21,7 @@ impl Plugin for ContainerPlugin { pub trait ContainerClientExt { async fn open_container(&mut self, pos: BlockPos) -> Option; - fn inventory(&mut self) -> Option; + fn open_inventory(&mut self) -> Option; } impl ContainerClientExt for Client { @@ -81,7 +81,7 @@ impl ContainerClientExt for Client { /// Note that this will send a packet to the server once it's dropped. Also, /// due to how it's implemented, you could call this function multiple times /// while another inventory handle already exists (but you shouldn't). - fn inventory(&mut self) -> Option { + fn open_inventory(&mut self) -> Option { let ecs = self.ecs.lock(); let inventory = ecs .get::(self.entity) -- cgit v1.2.3 From df167a5a391ef2a9bf2290a24d99ef8f559d9084 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 5 May 2023 17:15:49 +0000 Subject: fix some warnings --- azalea-inventory/azalea-inventory-macros/src/location_enum.rs | 2 +- azalea-inventory/azalea-inventory-macros/src/menu_impl.rs | 4 ++-- azalea-inventory/src/item/mod.rs | 2 ++ azalea/src/container.rs | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) (limited to 'azalea/src/container.rs') diff --git a/azalea-inventory/azalea-inventory-macros/src/location_enum.rs b/azalea-inventory/azalea-inventory-macros/src/location_enum.rs index 6cec88e6..cfcf3d48 100644 --- a/azalea-inventory/azalea-inventory-macros/src/location_enum.rs +++ b/azalea-inventory/azalea-inventory-macros/src/location_enum.rs @@ -26,7 +26,7 @@ pub fn generate(input: &DeclareMenus) -> TokenStream { name_snake_case.span(), ); let enum_name = Ident::new( - &format!("{}MenuLocation", variant_name), + &format!("{variant_name}MenuLocation"), variant_name.span(), ); menu_location_variants.extend(quote! { diff --git a/azalea-inventory/azalea-inventory-macros/src/menu_impl.rs b/azalea-inventory/azalea-inventory-macros/src/menu_impl.rs index 804f69f2..6bb37e8e 100644 --- a/azalea-inventory/azalea-inventory-macros/src/menu_impl.rs +++ b/azalea-inventory/azalea-inventory-macros/src/menu_impl.rs @@ -406,13 +406,13 @@ fn generate_menu_consts(menu: &Menu) -> TokenStream { if field.length == 1 { let field_name = Ident::new( - format!("{}_SLOT", field_name_start).as_str(), + format!("{field_name_start}_SLOT").as_str(), field.name.span(), ); menu_consts.extend(quote! { pub const #field_name: usize = #field_index_start; }); } else { let field_name = Ident::new( - format!("{}_SLOTS", field_name_start).as_str(), + format!("{field_name_start}_SLOTS").as_str(), field.name.span(), ); menu_consts.extend(quote! { pub const #field_name: RangeInclusive = #field_index_start..=#field_index_end; }); diff --git a/azalea-inventory/src/item/mod.rs b/azalea-inventory/src/item/mod.rs index 07e51363..0ad7b2c0 100644 --- a/azalea-inventory/src/item/mod.rs +++ b/azalea-inventory/src/item/mod.rs @@ -3,6 +3,8 @@ pub trait MaxStackSizeExt { /// /// This is a signed integer to be consistent with the `count` field of /// [`ItemSlotData`]. + /// + /// [`ItemSlotData`]: crate::ItemSlotData fn max_stack_size(&self) -> i8; /// Whether this item can be stacked with other items. diff --git a/azalea/src/container.rs b/azalea/src/container.rs index c8849cdd..fefcf189 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -25,8 +25,8 @@ pub trait ContainerClientExt { } impl ContainerClientExt for Client { - /// Open a container in the world, like a chest. Use [`Client::inventory`] - /// to open your own inventory. + /// Open a container in the world, like a chest. Use + /// [`Client::open_inventory`] to open your own inventory. /// /// ``` /// # use azalea::prelude::*; -- cgit v1.2.3