Skip to main content

PlayerInventory

Struct PlayerInventory 

Source
pub struct PlayerInventory {
    pub(super) items: [ItemStack; 43],
    pub(super) selected: u8,
    pub(super) times_changed: u32,
}
Expand description

Player inventory container managing the main inventory and equipment.

Contains 36 main inventory slots (0-8 hotbar, 9-35 main) plus equipment slots (armor, offhand, etc.) accessed through the Container trait.

Fields§

§items: [ItemStack; 43]

All 43 logical inventory slots in vanilla container order.

§selected: u8

Currently selected hotbar slot (0-8).

§times_changed: u32

Counter incremented on every change.

Implementations§

Source§

impl PlayerInventory

Source

pub fn apply_filled_result( &mut self, hand: InteractionHand, result_stack: ItemStack, has_infinite_materials: bool, limit_creative_stack_size: bool, ) -> ItemStack

Applies vanilla ItemUtils.createFilledResult to a held item.

Mutates the held stack and inventory, returning only the result stack that should be dropped by the caller. Creative inventory insertion discards leftover result items instead of dropping them.

Source§

impl PlayerInventory

Source

pub const INVENTORY_SIZE: usize = 36

Number of main inventory slots.

Source

pub const CONTAINER_SIZE: usize = 43

Number of logical container slots, including equipment.

Source

pub const SELECTION_SIZE: usize = 9

Number of hotbar slots.

Source

pub const SLOT_OFFHAND: usize = 40

Slot index for offhand.

Source

pub const HOTBAR: Range<usize>

Hotbar container indices.

Source

pub const MAIN: Range<usize>

Main storage container indices (everything except hotbar, armor, offhand).

Source

pub const ARMOR_TOP_DOWN: [usize; 4]

Armor container indices in display order (head, chest, legs, feet).

Source

pub fn new() -> Self

Creates a new player inventory with empty slots.

Source

pub const fn is_hotbar_slot(slot: usize) -> bool

Returns true if the given slot index is a hotbar slot (0-8).

Source

pub const fn get_selected_slot(&self) -> u8

Returns the currently selected hotbar slot (0-8).

Source

pub(crate) fn to_vanilla_inventory_nbt(&self) -> NbtList

Serializes the main inventory with vanilla’s ItemStackWithSlot shape.

Source

pub fn set_selected_slot(&mut self, slot: u8)

Sets the selected hotbar slot.

§Panics

Panics if the slot is not a valid hotbar slot (must be 0-8).

Source

pub fn try_set_selected_slot_from_packet( &mut self, slot: i16, ) -> Result<(), InvalidHotbarSlot>

Sets the selected hotbar slot from the signed protocol field.

Returns an error when the packet value is outside the vanilla hotbar range instead of wrapping or panicking.

Source

pub fn with_selected_item<R>(&self, f: impl FnOnce(&ItemStack) -> R) -> R

Executes a function with a reference to the currently selected item.

Source

pub const fn get_selected_item(&self) -> &ItemStack

Returns a mutable reference to the currently selected item (main hand).

Source

pub fn get_selected_item_mut(&mut self) -> &mut ItemStack

Returns the currently selected item (main hand).

Source

pub fn set_selected_item(&mut self, item: ItemStack)

Sets the currently selected item (main hand).

Source

pub fn get_offhand_item(&self) -> &ItemStack

Returns the offhand item.

Source

pub fn get_offhand_item_mut(&mut self) -> &mut ItemStack

Returns a mutable reference to the offhand item.

Source

pub fn set_offhand_item(&mut self, item: ItemStack)

Sets the offhand item.

Source

pub fn with_selected_item_mut<R>( &mut self, f: impl FnOnce(&mut ItemStack) -> R, ) -> R

Executes a function with a mutable reference to the currently selected item.

Source

pub(in player) fn with_equipment_item_mut<R>( &mut self, slot: EquipmentSlot, f: impl FnOnce(&mut ItemStack) -> R, ) -> R

Source

pub const fn get_times_changed(&self) -> u32

Returns the number of times this inventory has been modified.

Source

pub fn get_items(&self) -> &[ItemStack; 36]

Returns the non-equipment items (main 36 slots).

Source

pub fn get_free_slot(&self) -> i32

Finds the first empty slot in the inventory, or -1 if full.

Source

pub fn find_slot_matching_item(&self, stack: &ItemStack) -> i32

Finds a slot containing an item matching the given stack (same item type). Returns -1 if not found.

Source

pub fn pick_slot(&mut self, slot: i32)

Swaps items between selected hotbar slot and the given slot. Used for pick block when item is in main inventory but not hotbar.

Source

pub fn add_and_pick_item(&mut self, stack: ItemStack) -> bool

Adds an item to the hotbar (for creative pick block) and selects it. Returns true if successful.

Source§

impl PlayerInventory

Source

pub fn get_item_in_hand(&self, hand: InteractionHand) -> &ItemStack

Gets the item in the specified hand.

Source

pub fn get_item_in_hand_mut(&mut self, hand: InteractionHand) -> &mut ItemStack

Gets the item in the specified hand.

Source

pub fn set_item_in_hand(&mut self, hand: InteractionHand, item: ItemStack)

Sets the item in the specified hand.

Source

pub fn shrink_item_in_hand(&mut self, hand: InteractionHand, amount: i32)

Shrinks the item in the specified hand and records inventory/equipment changes.

Source

pub fn split_item_in_hand( &mut self, hand: InteractionHand, amount: i32, ) -> ItemStack

Splits items from the specified hand and records inventory/equipment changes.

Source

pub fn hurt_item_in_hand( &mut self, hand: InteractionHand, amount: i32, has_infinite_materials: bool, )

Damages the held item and records inventory/equipment changes.

Source

pub fn mutate_item_in_hand<R>( &mut self, hand: InteractionHand, f: impl FnOnce(&mut ItemStack) -> R, ) -> R

Mutates the held item and records inventory/equipment changes if its stack state changed.

Source

pub fn hurt_and_convert_item_in_hand_on_break( &mut self, hand: InteractionHand, amount: i32, replacement_item: ItemRef, has_infinite_materials: bool, )

Damages the held item and converts it to replacement_item if it breaks.

Mirrors vanilla ItemStack.hurtAndConvertOnBreak for hand-held player items.

Source

pub fn swap_hands(&mut self) -> bool

Swaps the selected main-hand item with the offhand item.

Returns true when the visible hand contents changed.

Source

pub fn try_swap_with_equipment_slot( &mut self, hand: InteractionHand, slot: EquipmentSlot, has_infinite_materials: bool, ) -> EquipmentSwapResult

Attempts to equip the held item into the target equipment slot.

Source

pub fn repair_random_equipped_item_with_xp(&mut self, amount: i32) -> i32

Repairs a random damaged equipped item with REPAIR_WITH_XP, returning leftover XP.

Source

fn swap_single_item_with_equipment_slot( &mut self, hand: InteractionHand, slot: EquipmentSlot, has_infinite_materials: bool, )

Source

fn repair_with_xp_candidate_slots(&self) -> Vec<EquipmentSlot>

Source

fn take_item_in_hand(&mut self, hand: InteractionHand) -> ItemStack

Source§

impl PlayerInventory

Source

pub(super) const fn equipment_slot_index(&self, slot: EquipmentSlot) -> usize

Trait Implementations§

Source§

impl Container for PlayerInventory

Source§

fn add(&mut self, stack: &mut ItemStack) -> bool

Adds an item to the player’s main inventory (slots 0-35 only).

Overrides the default Container::add() to prevent items from being placed in armor or equipment slots. Matches vanilla’s Inventory.add() behavior which only adds to this.items (the 36 main slots).

Source§

fn items(&self) -> &[ItemStack]

Returns the items in this container
Source§

fn items_mut(&mut self) -> &mut [ItemStack]

Returns mutable references to the items in this container.
Source§

fn get_container_size(&self) -> usize

Returns the number of slots in this container.
Source§

fn get_item(&self, slot: usize) -> &ItemStack

Returns a reference to the item in the specified slot.
Source§

fn get_item_mut(&mut self, slot: usize) -> &mut ItemStack

Returns a mutable reference to the item in the specified slot.
Source§

fn set_item(&mut self, slot: usize, stack: ItemStack)

Sets the item in the specified slot.
Source§

fn is_empty(&self) -> bool

Returns true if all slots in this container are empty.
Source§

fn set_changed(&mut self)

Marks this container as changed (dirty) for saving/syncing.
Source§

fn clear_content(&mut self) -> i32

Clears all items from this container.
Source§

fn clear_content_matching( &mut self, predicate: &mut dyn FnMut(&mut ItemStack) -> bool, ) -> i32

Clears all items from this container.
Source§

fn contains_stack(&self, search_stack: &ItemStack) -> bool

Returns true if this container has a non-empty stack with the same item and components. Read more
Source§

fn remove_item(&mut self, slot: usize, count: i32) -> ItemStack

Removes up to count items from the specified slot and returns them.
Source§

fn remove_item_no_update(&mut self, slot: usize) -> ItemStack

Removes the item from the specified slot without triggering updates.
Source§

fn get_max_stack_size(&self) -> i32

Returns the maximum stack size for this container.
Source§

fn get_max_stack_size_for_item(&self, item: &ItemStack) -> i32

Returns the maximum stack size for a specific item in this container. Read more
Source§

fn can_place_item(&self, _slot: usize, _stack: &ItemStack) -> bool

Returns true if the specified item can be placed in the specified slot.
Source§

fn can_take_item(&self, _slot: usize, _stack: &ItemStack) -> bool

Returns true if the specified item can be taken from the specified slot.
Source§

fn clear_or_count_matching_items( &mut self, predicate: &dyn Fn(&ItemStack) -> bool, amount_to_remove: i32, counting_only: bool, ) -> i32

Removes or counts matching items using vanilla /clear semantics.
Source§

fn with_indices<const N: usize>( &mut self, indices: [usize; N], ) -> [&mut ItemStack; N]
where Self: Sized,

Returns mutable references to N disjoint slots. Read more
Source§

fn iter(&self) -> Box<dyn Iterator<Item = &ItemStack> + '_>

Returns a boxed iterator to the items in this container
Source§

fn iter_mut(&mut self) -> Box<dyn Iterator<Item = &mut ItemStack> + '_>

Returns a boxed iterator to mutable references of the items in this container
Source§

impl Default for PlayerInventory

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl DowncastType for PlayerInventory

Source§

const TYPE_KEY: DowncastTypeKey

The process-wide key for this concrete type.
Source§

impl EntityEquipment for PlayerInventory

Source§

fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack

Gets a reference to the item in a slot.
Source§

fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack

Gets a mutable reference to the item in a slot.
Source§

fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack

Sets the item in a slot, returning the old item.
Source§

fn take(&mut self, slot: EquipmentSlot) -> ItemStack

Takes the item from a slot, leaving an empty stack in its place.
Source§

fn clear(&mut self)

Clears all equipment slots.
Source§

fn non_empty_items(&self) -> Vec<(EquipmentSlot, ItemStack)>

Returns non-empty equipment slots for initial spawn synchronization.

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> Downcast for T
where T: ErasedType + ?Sized,

Source§

fn is<T>(&self) -> bool
where T: DowncastType,

Returns whether the erased value has concrete type T.
Source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: DowncastType,

Returns the erased value as T when its key matches.
Source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: DowncastType,

Returns the erased value as mutable T when its key matches.
Source§

impl<T> ErasedType for T
where T: DowncastType,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoShared for T

Source§

fn into_shared(self) -> Arc<Mutex<RawMutex, Self>>

Wraps this value in an Arc<SyncMutex<>>
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more