Skip to main content

Inventory

Struct Inventory 

Source
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: u8

Currently selected hotbar index (0-8).

§slots: [Slot; 36]

All 36 inventory slots (0-8 hotbar, 9-35 main).

§cursor: Slot

Item currently held on the mouse cursor (in an open window).

Implementations§

Source§

impl Inventory

Source

pub const HOTBAR_START: usize = 0

First hotbar slot index within slots.

Source

pub const MAIN_START: usize = 9

First main inventory slot index within slots.

Source

pub fn empty() -> Self

Creates an empty inventory with slot 0 selected.

Source

pub fn held_item(&self) -> &Slot

Returns the currently held item (from the hotbar).

Source

pub fn hotbar(&self) -> &[Slot]

Returns a reference to the hotbar (9 slots).

Source

pub fn hotbar_mut(&mut self) -> &mut [Slot]

Returns a mutable reference to the hotbar (9 slots).

Source

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.

Source

pub fn index_to_window(index: usize) -> Option<i16>

Converts an internal slot index to a protocol window slot.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Inventory

Source§

fn clone(&self) -> Inventory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Inventory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Component for Inventory

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.