From cce026029d5c9c087db8f5828c46c83fbc2db29b Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 22 Dec 2025 00:38:41 -1030 Subject: add ContainerHandleRef::title --- azalea-client/src/plugins/events.rs | 2 +- azalea/src/container.rs | 41 ++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/azalea-client/src/plugins/events.rs b/azalea-client/src/plugins/events.rs index c1b77abe..bd0419cb 100644 --- a/azalea-client/src/plugins/events.rs +++ b/azalea-client/src/plugins/events.rs @@ -97,7 +97,7 @@ pub enum Event { /// # use azalea_protocol::packets::game::ClientboundGamePacket; /// # async fn example(event: Event) { /// # match event { - /// Event::Packet(packet) => match *packet { + /// Event::Packet(packet) => match &*packet { /// ClientboundGamePacket::Login(_) => { /// println!("login packet"); /// } diff --git a/azalea/src/container.rs b/azalea/src/container.rs index c01b16be..d12fb380 100644 --- a/azalea/src/container.rs +++ b/azalea/src/container.rs @@ -1,5 +1,6 @@ use std::{fmt, fmt::Debug}; +use azalea_chat::FormattedText; use azalea_client::{ Client, inventory::{CloseContainerEvent, ContainerClickEvent}, @@ -234,21 +235,24 @@ impl ContainerHandleRef { /// actually cause any packets to be sent. If you're trying to modify your /// inventory, use [`Self::click`] instead pub fn menu(&self) -> Option { - let ecs = self.client.ecs.lock(); - 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 { + self.map_inventory(|inv| { if self.id == 0 { - Some(inventory.inventory_menu.clone()) + inv.inventory_menu.clone() } else { - Some(inventory.container_menu.clone().unwrap()) + inv.container_menu.clone().unwrap() } - } else { - None - } + }) + } + + fn map_inventory(&self, f: impl FnOnce(&Inventory) -> R) -> Option { + self.client.query_self::<&Inventory, _>(|inv| { + if inv.id == self.id { + Some(f(inv)) + } else { + // a different inventory is open + None + } + }) } /// Returns the item slots in the container, not including the player's @@ -266,6 +270,19 @@ impl ContainerHandleRef { self.menu().map(|menu| menu.slots()) } + /// Returns the title of the container, or `None` if no container is open. + /// + /// ```no_run + /// let inventory = bot.get_inventory(); + /// let inventory_title = inventory.title.unwrap_or_default().to_string(); + /// // would be true if an unnamed chest is open + /// assert_eq!(inventory_title, "Chest"); + /// ``` + pub fn title(&self) -> Option { + self.map_inventory(|inv| inv.container_menu_title.clone()) + .flatten() + } + /// A shortcut for [`Self::click`] with `PickupClick::Left`. pub fn left_click(&self, slot: impl Into) { self.click(PickupClick::Left { -- cgit v1.2.3