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: u8Currently selected hotbar slot (0-8).
times_changed: u32Counter incremented on every change.
Implementations§
Source§impl PlayerInventory
impl PlayerInventory
Sourcepub fn apply_filled_result(
&mut self,
hand: InteractionHand,
result_stack: ItemStack,
has_infinite_materials: bool,
limit_creative_stack_size: bool,
) -> ItemStack
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
impl PlayerInventory
Sourcepub const INVENTORY_SIZE: usize = 36
pub const INVENTORY_SIZE: usize = 36
Number of main inventory slots.
Sourcepub const CONTAINER_SIZE: usize = 43
pub const CONTAINER_SIZE: usize = 43
Number of logical container slots, including equipment.
Sourcepub const SELECTION_SIZE: usize = 9
pub const SELECTION_SIZE: usize = 9
Number of hotbar slots.
Sourcepub const SLOT_OFFHAND: usize = 40
pub const SLOT_OFFHAND: usize = 40
Slot index for offhand.
Sourcepub const MAIN: Range<usize>
pub const MAIN: Range<usize>
Main storage container indices (everything except hotbar, armor, offhand).
Sourcepub const ARMOR_TOP_DOWN: [usize; 4]
pub const ARMOR_TOP_DOWN: [usize; 4]
Armor container indices in display order (head, chest, legs, feet).
Sourcepub const fn is_hotbar_slot(slot: usize) -> bool
pub const fn is_hotbar_slot(slot: usize) -> bool
Returns true if the given slot index is a hotbar slot (0-8).
Sourcepub const fn get_selected_slot(&self) -> u8
pub const fn get_selected_slot(&self) -> u8
Returns the currently selected hotbar slot (0-8).
Sourcepub(crate) fn to_vanilla_inventory_nbt(&self) -> NbtList
pub(crate) fn to_vanilla_inventory_nbt(&self) -> NbtList
Serializes the main inventory with vanilla’s ItemStackWithSlot shape.
Sourcepub fn set_selected_slot(&mut self, slot: u8)
pub fn set_selected_slot(&mut self, slot: u8)
Sourcepub fn try_set_selected_slot_from_packet(
&mut self,
slot: i16,
) -> Result<(), InvalidHotbarSlot>
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.
Sourcepub fn with_selected_item<R>(&self, f: impl FnOnce(&ItemStack) -> R) -> R
pub fn with_selected_item<R>(&self, f: impl FnOnce(&ItemStack) -> R) -> R
Executes a function with a reference to the currently selected item.
Sourcepub const fn get_selected_item(&self) -> &ItemStack
pub const fn get_selected_item(&self) -> &ItemStack
Returns a mutable reference to the currently selected item (main hand).
Sourcepub fn get_selected_item_mut(&mut self) -> &mut ItemStack
pub fn get_selected_item_mut(&mut self) -> &mut ItemStack
Returns the currently selected item (main hand).
Sourcepub fn set_selected_item(&mut self, item: ItemStack)
pub fn set_selected_item(&mut self, item: ItemStack)
Sets the currently selected item (main hand).
Sourcepub fn get_offhand_item(&self) -> &ItemStack
pub fn get_offhand_item(&self) -> &ItemStack
Returns the offhand item.
Sourcepub fn get_offhand_item_mut(&mut self) -> &mut ItemStack
pub fn get_offhand_item_mut(&mut self) -> &mut ItemStack
Returns a mutable reference to the offhand item.
Sourcepub fn set_offhand_item(&mut self, item: ItemStack)
pub fn set_offhand_item(&mut self, item: ItemStack)
Sets the offhand item.
Sourcepub fn with_selected_item_mut<R>(
&mut self,
f: impl FnOnce(&mut ItemStack) -> R,
) -> R
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.
pub(in player) fn with_equipment_item_mut<R>( &mut self, slot: EquipmentSlot, f: impl FnOnce(&mut ItemStack) -> R, ) -> R
Sourcepub const fn get_times_changed(&self) -> u32
pub const fn get_times_changed(&self) -> u32
Returns the number of times this inventory has been modified.
Sourcepub fn get_free_slot(&self) -> i32
pub fn get_free_slot(&self) -> i32
Finds the first empty slot in the inventory, or -1 if full.
Sourcepub fn find_slot_matching_item(&self, stack: &ItemStack) -> i32
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.
Sourcepub fn pick_slot(&mut self, slot: i32)
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.
Sourcepub fn add_and_pick_item(&mut self, stack: ItemStack) -> bool
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
impl PlayerInventory
Sourcepub fn get_item_in_hand(&self, hand: InteractionHand) -> &ItemStack
pub fn get_item_in_hand(&self, hand: InteractionHand) -> &ItemStack
Gets the item in the specified hand.
Sourcepub fn get_item_in_hand_mut(&mut self, hand: InteractionHand) -> &mut ItemStack
pub fn get_item_in_hand_mut(&mut self, hand: InteractionHand) -> &mut ItemStack
Gets the item in the specified hand.
Sourcepub fn set_item_in_hand(&mut self, hand: InteractionHand, item: ItemStack)
pub fn set_item_in_hand(&mut self, hand: InteractionHand, item: ItemStack)
Sets the item in the specified hand.
Sourcepub fn shrink_item_in_hand(&mut self, hand: InteractionHand, amount: i32)
pub fn shrink_item_in_hand(&mut self, hand: InteractionHand, amount: i32)
Shrinks the item in the specified hand and records inventory/equipment changes.
Sourcepub fn split_item_in_hand(
&mut self,
hand: InteractionHand,
amount: i32,
) -> ItemStack
pub fn split_item_in_hand( &mut self, hand: InteractionHand, amount: i32, ) -> ItemStack
Splits items from the specified hand and records inventory/equipment changes.
Sourcepub fn hurt_item_in_hand(
&mut self,
hand: InteractionHand,
amount: i32,
has_infinite_materials: bool,
)
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.
Sourcepub fn mutate_item_in_hand<R>(
&mut self,
hand: InteractionHand,
f: impl FnOnce(&mut ItemStack) -> R,
) -> R
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.
Sourcepub fn hurt_and_convert_item_in_hand_on_break(
&mut self,
hand: InteractionHand,
amount: i32,
replacement_item: ItemRef,
has_infinite_materials: bool,
)
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.
Sourcepub fn swap_hands(&mut self) -> bool
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.
Sourcepub fn try_swap_with_equipment_slot(
&mut self,
hand: InteractionHand,
slot: EquipmentSlot,
has_infinite_materials: bool,
) -> EquipmentSwapResult
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.
Sourcepub fn repair_random_equipped_item_with_xp(&mut self, amount: i32) -> i32
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.
fn swap_single_item_with_equipment_slot( &mut self, hand: InteractionHand, slot: EquipmentSlot, has_infinite_materials: bool, )
fn repair_with_xp_candidate_slots(&self) -> Vec<EquipmentSlot>
fn take_item_in_hand(&mut self, hand: InteractionHand) -> ItemStack
Source§impl PlayerInventory
impl PlayerInventory
pub(super) const fn equipment_slot_index(&self, slot: EquipmentSlot) -> usize
Trait Implementations§
Source§impl Container for PlayerInventory
impl Container for PlayerInventory
Source§fn add(&mut self, stack: &mut ItemStack) -> bool
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_mut(&mut self) -> &mut [ItemStack]
fn items_mut(&mut self) -> &mut [ItemStack]
Source§fn get_container_size(&self) -> usize
fn get_container_size(&self) -> usize
Source§fn get_item(&self, slot: usize) -> &ItemStack
fn get_item(&self, slot: usize) -> &ItemStack
Source§fn get_item_mut(&mut self, slot: usize) -> &mut ItemStack
fn get_item_mut(&mut self, slot: usize) -> &mut ItemStack
Source§fn set_changed(&mut self)
fn set_changed(&mut self)
Source§fn clear_content(&mut self) -> i32
fn clear_content(&mut self) -> i32
Source§fn clear_content_matching(
&mut self,
predicate: &mut dyn FnMut(&mut ItemStack) -> bool,
) -> i32
fn clear_content_matching( &mut self, predicate: &mut dyn FnMut(&mut ItemStack) -> bool, ) -> i32
Source§fn contains_stack(&self, search_stack: &ItemStack) -> bool
fn contains_stack(&self, search_stack: &ItemStack) -> bool
Source§fn remove_item(&mut self, slot: usize, count: i32) -> ItemStack
fn remove_item(&mut self, slot: usize, count: i32) -> ItemStack
count items from the specified slot and returns them.Source§fn remove_item_no_update(&mut self, slot: usize) -> ItemStack
fn remove_item_no_update(&mut self, slot: usize) -> ItemStack
Source§fn get_max_stack_size(&self) -> i32
fn get_max_stack_size(&self) -> i32
Source§fn get_max_stack_size_for_item(&self, item: &ItemStack) -> i32
fn get_max_stack_size_for_item(&self, item: &ItemStack) -> i32
Source§fn can_place_item(&self, _slot: usize, _stack: &ItemStack) -> bool
fn can_place_item(&self, _slot: usize, _stack: &ItemStack) -> bool
Source§fn can_take_item(&self, _slot: usize, _stack: &ItemStack) -> bool
fn can_take_item(&self, _slot: usize, _stack: &ItemStack) -> bool
Source§fn clear_or_count_matching_items(
&mut self,
predicate: &dyn Fn(&ItemStack) -> bool,
amount_to_remove: i32,
counting_only: bool,
) -> i32
fn clear_or_count_matching_items( &mut self, predicate: &dyn Fn(&ItemStack) -> bool, amount_to_remove: i32, counting_only: bool, ) -> i32
/clear semantics.Source§fn with_indices<const N: usize>(
&mut self,
indices: [usize; N],
) -> [&mut ItemStack; N]where
Self: Sized,
fn with_indices<const N: usize>(
&mut self,
indices: [usize; N],
) -> [&mut ItemStack; N]where
Self: Sized,
N disjoint slots. Read moreSource§impl Default for PlayerInventory
impl Default for PlayerInventory
Source§impl DowncastType for PlayerInventory
impl DowncastType for PlayerInventory
Source§const TYPE_KEY: DowncastTypeKey
const TYPE_KEY: DowncastTypeKey
Source§impl EntityEquipment for PlayerInventory
impl EntityEquipment for PlayerInventory
Source§fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack
fn get_ref(&self, slot: EquipmentSlot) -> &ItemStack
Source§fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack
fn get_mut(&mut self, slot: EquipmentSlot) -> &mut ItemStack
Source§fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack
fn set(&mut self, slot: EquipmentSlot, stack: ItemStack) -> ItemStack
Source§fn take(&mut self, slot: EquipmentSlot) -> ItemStack
fn take(&mut self, slot: EquipmentSlot) -> ItemStack
Source§fn non_empty_items(&self) -> Vec<(EquipmentSlot, ItemStack)>
fn non_empty_items(&self) -> Vec<(EquipmentSlot, ItemStack)>
Auto Trait Implementations§
impl !RefUnwindSafe for PlayerInventory
impl !UnwindSafe for PlayerInventory
impl Freeze for PlayerInventory
impl Send for PlayerInventory
impl Sync for PlayerInventory
impl Unpin for PlayerInventory
impl UnsafeUnpin for PlayerInventory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: ErasedType + ?Sized,
impl<T> Downcast for Twhere
T: ErasedType + ?Sized,
Source§fn is<T>(&self) -> boolwhere
T: DowncastType,
fn is<T>(&self) -> boolwhere
T: DowncastType,
T.Source§fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastType,
fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastType,
T when its key matches.Source§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: DowncastType,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: DowncastType,
T when its key matches.Source§impl<T> ErasedType for Twhere
T: DowncastType,
impl<T> ErasedType for Twhere
T: DowncastType,
fn downcast_type_key(&self) -> DowncastTypeKey
fn downcast_data(&self) -> *const ()
fn downcast_data_mut(&mut self) -> *mut ()
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreArc<SyncMutex<>>