azalea/client_impl/
inventory.rs

1use azalea_client::inventory::SetSelectedHotbarSlotEvent;
2use azalea_entity::inventory::Inventory;
3use azalea_inventory::Menu;
4
5use crate::Client;
6
7impl Client {
8    /// Return the menu that is currently open, or the player's inventory if no
9    /// menu is open.
10    ///
11    /// If you need to interact with the menu, consider using
12    /// [`Self::open_inventory`] instead.
13    pub fn menu(&self) -> Menu {
14        self.component::<Inventory>().menu().clone()
15    }
16
17    /// Returns the index of the hotbar slot that's currently selected.
18    ///
19    /// If you want to access the actual held item, you can get the current menu
20    /// with [`Client::menu`] and then get the slot index by offsetting from
21    /// the start of [`azalea_inventory::Menu::hotbar_slots_range`].
22    ///
23    /// You can use [`Self::set_selected_hotbar_slot`] to change it.
24    pub fn selected_hotbar_slot(&self) -> u8 {
25        self.component::<Inventory>().selected_hotbar_slot
26    }
27
28    /// Update the selected hotbar slot index.
29    ///
30    /// This will run next `Update`, so you might want to call
31    /// `bot.wait_updates(1)` after calling this if you're using `azalea`.
32    ///
33    /// # Panics
34    ///
35    /// This will panic if `new_hotbar_slot_index` is not in the range 0..=8.
36    pub fn set_selected_hotbar_slot(&self, new_hotbar_slot_index: u8) {
37        assert!(
38            new_hotbar_slot_index < 9,
39            "Hotbar slot index must be in the range 0..=8"
40        );
41
42        let mut ecs = self.ecs.write();
43        ecs.trigger(SetSelectedHotbarSlotEvent {
44            entity: self.entity,
45            slot: new_hotbar_slot_index,
46        });
47    }
48}