Skip to main content

ItemStack

Struct ItemStack 

Source
pub struct ItemStack {
    pub item_id: u32,
    pub amount: u32,
    pub durability: Option<f32>,
}
Expand description

A stack of identical items in an inventory.

This is the primary ECS component for representing items in the world or in a container. It is serialised via bincode when crossing the WASM boundary (get_component / set_component).

§Example

use openstranded_common_items::ItemStack;

let stack = ItemStack::new(23, 1);       // one stone
let tool = ItemStack::with_durability(30, 1, 100.0); // hammer at full health

Fields§

§item_id: u32

Item type ID (matches ItemDef.id).

§amount: u32

Number of items in this stack.

§durability: Option<f32>

Remaining durability for tools / weapons.

None means the item doesn’t have durability (non-tools). Some(n) means current durability; when it reaches 0 the item breaks.

Implementations§

Source§

impl ItemStack

Source

pub const MAX_STACK: u32 = 999

Maximum amount per stack.

This is a game-wide constant from the original game (999). Individual item types may override this in the future.

Source

pub const fn new(item_id: u32, amount: u32) -> ItemStack

Create a new stack of amount items of type item_id.

Durability is set to None (non-relevant for non-tools).

Source

pub const fn with_durability( item_id: u32, amount: u32, durability: f32, ) -> ItemStack

Create a new stack with a specific durability value.

Useful for tools and weapons that degrade over time.

Source

pub const fn is_empty(&self) -> bool

Whether this stack is empty (amount == 0).

Source

pub fn merge(&mut self, other: &ItemStack) -> u32

Merge another stack into this one, respecting max stack size.

Returns the amount that could NOT be merged (overflow), or 0 if all items fit.

Source

pub fn split(&mut self, count: u32) -> Option<ItemStack>

Split this stack, taking count items.

Returns None if count exceeds the current amount. Otherwise decrements the stack and returns a new stack with count items.

Trait Implementations§

Source§

impl Clone for ItemStack

Source§

fn clone(&self) -> ItemStack

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ItemStack

Source§

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

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

impl<'de> Deserialize<'de> for ItemStack

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<ItemStack, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ItemStack

Source§

fn eq(&self, other: &ItemStack) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for ItemStack

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ItemStack

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.