aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/client_impl/inventory.rs
blob: 93e599aa9f30673f09e771ebc0a1262824871a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use azalea_client::inventory::SetSelectedHotbarSlotEvent;
use azalea_entity::inventory::Inventory;
use azalea_inventory::Menu;

use crate::{Client, client_impl::error::AzaleaResult};

impl Client {
    /// Return the menu that is currently open, or the player's inventory if no
    /// menu is open.
    ///
    /// If you need to interact with the menu, consider using
    /// [`Self::open_inventory`] instead.
    pub fn menu(&self) -> AzaleaResult<Menu> {
        Ok(self.component::<Inventory>()?.menu().clone())
    }

    /// Returns the index of the hotbar slot that's currently selected.
    ///
    /// If you want to access the actual held item, you can get the current menu
    /// with [`Client::menu`] and then get the slot index by offsetting from
    /// the start of [`azalea_inventory::Menu::hotbar_slots_range`].
    ///
    /// You can use [`Self::set_selected_hotbar_slot`] to change it.
    pub fn selected_hotbar_slot(&self) -> AzaleaResult<u8> {
        Ok(self.component::<Inventory>()?.selected_hotbar_slot)
    }

    /// Update the selected hotbar slot index.
    ///
    /// This will run next `Update`, so you might want to call
    /// `bot.wait_updates(1)` after calling this if you're using `azalea`.
    ///
    /// # Panics
    ///
    /// This will panic if `new_hotbar_slot_index` is not in the range 0..=8.
    pub fn set_selected_hotbar_slot(&self, new_hotbar_slot_index: u8) {
        assert!(
            new_hotbar_slot_index < 9,
            "Hotbar slot index must be in the range 0..=8"
        );

        let mut ecs = self.ecs.write();
        ecs.trigger(SetSelectedHotbarSlotEvent {
            entity: self.entity,
            slot: new_hotbar_slot_index,
        });
    }
}