diff options
| author | mat <git@matdoes.dev> | 2025-12-22 00:38:41 -1030 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-12-22 00:38:41 -1030 |
| commit | cce026029d5c9c087db8f5828c46c83fbc2db29b (patch) | |
| tree | 6576d477fbdc3d4632ccfad18971ed3a72fb5d95 | |
| parent | e9a18ec41e039c65b926102781bf2ffefa20b4c9 (diff) | |
| download | azalea-drasl-cce026029d5c9c087db8f5828c46c83fbc2db29b.tar.xz | |
add ContainerHandleRef::title
| -rw-r--r-- | azalea-client/src/plugins/events.rs | 2 | ||||
| -rw-r--r-- | 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<Menu> { - let ecs = self.client.ecs.lock(); - let inventory = ecs - .get::<Inventory>(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<R>(&self, f: impl FnOnce(&Inventory) -> R) -> Option<R> { + 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<FormattedText> { + 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<usize>) { self.click(PickupClick::Left { |
