pub struct Inventory<I: ItemType> {
pub slots: Vec<Option<InventorySlot<I>>>,
pub capacity: usize,
}Expand description
Fixed-capacity player inventory backed by a flat slot vector.
The inventory holds up to capacity stacks, each stored as
Option<InventorySlot<I>>. None denotes an empty slot.
Items of the same type are automatically stacked: Inventory::add
fills existing stacks of the matching type before opening a new slot.
Inventory::remove drains stacks in order and clears slots that reach
zero.
The default capacity is 36 slots (4 rows × 9 columns), matching the standard Minecraft-style layout expected by the built-in hotbar and inventory UI widgets.
§Examples
use voxel_framework::items::{Inventory, DefaultItem};
let mut inv = Inventory::<DefaultItem>::new(9);
inv.add(DefaultItem::IronOre, 5);
assert_eq!(inv.count_of(DefaultItem::IronOre), 5);Fields§
§slots: Vec<Option<InventorySlot<I>>>The backing slot vector. None at index i means slot i is empty.
The length always equals capacity; slots are never removed from the
vector.
capacity: usizeMaximum number of distinct item stacks the inventory can hold.
Set at construction time; cannot be changed after the fact without rebuilding the inventory.
Implementations§
Source§impl<I: ItemType> Inventory<I>
impl<I: ItemType> Inventory<I>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new empty inventory with the given slot capacity.
Sourcepub fn add(&mut self, item: I, count: u32) -> u32
pub fn add(&mut self, item: I, count: u32) -> u32
Adds count of item to the inventory.
Items are stacked onto existing slots of the same type first, then into
empty slots. Returns the number of items that could not be added
because slot capacity or the per-item u32 representational ceiling is
exceeded. This is not an author-configured stack limit. Invalid public
fields or positive default/empty sentinels are rejected without mutation.
Sourcepub fn remove(&mut self, item: I, count: u32) -> u32
pub fn remove(&mut self, item: I, count: u32) -> u32
Removes up to count of item from the inventory.
Returns the number of items that were actually removed (may be less than
count if the inventory held fewer).
Trait Implementations§
Source§impl<I: ItemType> Container<I> for Inventory<I>
impl<I: ItemType> Container<I> for Inventory<I>
Source§fn try_exchange_atomic(
&mut self,
remove: &[(I, u32)],
add: &[(I, u32)],
) -> Option<Result<(), ContainerExchangeError>>
fn try_exchange_atomic( &mut self, remove: &[(I, u32)], add: &[(I, u32)], ) -> Option<Result<(), ContainerExchangeError>>
Source§fn add(&mut self, item: I, count: u32) -> u32
fn add(&mut self, item: I, count: u32) -> u32
count of item. Returns the number of items that could NOT be
inserted due to capacity (0 = all inserted).Source§fn remove(&mut self, item: I, count: u32) -> u32
fn remove(&mut self, item: I, count: u32) -> u32
count of item. Returns the number actually removed.Source§fn iter_slots(&self) -> impl Iterator<Item = Option<&InventorySlot<I>>>
fn iter_slots(&self) -> impl Iterator<Item = Option<&InventorySlot<I>>>
Some(&InventorySlot<I>) for occupied
slots and None for empty ones. Yields exactly capacity() items.impl<I: ItemType> Resource for Inventory<I>
Auto Trait Implementations§
impl<I> Freeze for Inventory<I>
impl<I> RefUnwindSafe for Inventory<I>
impl<I> Send for Inventory<I>
impl<I> Sync for Inventory<I>
impl<I> Unpin for Inventory<I>
impl<I> UnsafeUnpin for Inventory<I>
impl<I> UnwindSafe for Inventory<I>
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> 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 more