pub struct Inventory {
pub held_slot: u8,
pub slots: [Slot; 36],
pub cursor: Slot,
}Expand description
Player inventory — 36 slots (27 main + 9 hotbar).
Slot layout matches Minecraft’s raw player inventory:
- Slots 0–8: hotbar
- Slots 9–35: main inventory (3 rows of 9)
This matches the SetPlayerInventory packet (1.21.4) directly —
no slot conversion needed when syncing individual slots.
For the player inventory window (SetContainerContent), slots remap: window 9-35 = main (our 9-35, same), window 36-44 = hotbar (our 0-8).
Fields§
§held_slot: u8Currently selected hotbar index (0-8).
slots: [Slot; 36]All 36 inventory slots (0-8 hotbar, 9-35 main).
cursor: SlotItem currently held on the mouse cursor (in an open window).
Implementations§
Source§impl Inventory
impl Inventory
Sourcepub const HOTBAR_START: usize = 0
pub const HOTBAR_START: usize = 0
First hotbar slot index within slots.
Sourcepub const MAIN_START: usize = 9
pub const MAIN_START: usize = 9
First main inventory slot index within slots.
Sourcepub fn hotbar_mut(&mut self) -> &mut [Slot]
pub fn hotbar_mut(&mut self) -> &mut [Slot]
Returns a mutable reference to the hotbar (9 slots).
Sourcepub fn window_to_index(window_slot: i16) -> Option<usize>
pub fn window_to_index(window_slot: i16) -> Option<usize>
Converts a protocol window slot to an internal slot index.
Window layout: 9-35 = main, 36-44 = hotbar. Internal layout: 0-8 = hotbar, 9-35 = main.
Sourcepub fn index_to_window(index: usize) -> Option<i16>
pub fn index_to_window(index: usize) -> Option<i16>
Converts an internal slot index to a protocol window slot.
Sourcepub fn try_insert(&mut self, item_id: i32, count: i32) -> Option<usize>
pub fn try_insert(&mut self, item_id: i32, count: i32) -> Option<usize>
Tries to insert an item into the inventory.
Searches hotbar first (for convenience), then main inventory.
Tries matching stacks (count < 64) first, then empty slots.
Returns Some(internal_index) if inserted, None if full.
Sourcepub fn to_protocol_slots(&self) -> Vec<Slot>
pub fn to_protocol_slots(&self) -> Vec<Slot>
Builds the 46-slot protocol representation for SetContainerContent.
Window slot layout for player inventory (id=0): 0 = crafting output, 1-4 = crafting grid, 5-8 = armor, 9-35 = main inventory, 36-44 = hotbar, 45 = offhand.