Struct Inventory

Source
pub struct Inventory<K, S: SlotType, U: Default> {
    pub content: Vec<Option<ItemInstance<K, U>>>,
    pub slot_restriction: Vec<Option<S>>,
    pub move_to_front: MoveToFrontMode,
    pub sizing_mode: InventorySizingMode,
}
Expand description

§Generics

  • K: Item Type
  • S: Type of inventory location
  • U: Custom item data

Fields§

§content: Vec<Option<ItemInstance<K, U>>>

The contents of the Inventory. None values indicate empty but existing inventory slots.

§slot_restriction: Vec<Option<S>>

Restricts what kind of item can go in different slots. This is not compatible with InventorySizingMode::Dynamic.

Maps to the inventory content using the index. None values indicate that there are no restrictions for that slot.

§move_to_front: MoveToFrontMode

Configures how item deletion is handled.

§sizing_mode: InventorySizingMode

Configures if the inventory resizes when item are inserted/removed or not.

Implementations§

Source§

impl<K, S: SlotType, U: Default> Inventory<K, S, U>

Source

pub fn new( content: Vec<Option<ItemInstance<K, U>>>, slot_restriction: Vec<Option<S>>, move_to_front: MoveToFrontMode, sizing_mode: InventorySizingMode, ) -> Self

Constructs a new Inventory.

Source§

impl<K: PartialEq + Clone + Debug + Hash + Eq, S: SlotType, U: Default + Clone + Debug + PartialEq> Inventory<K, S, U>

Source

pub fn new_fixed(count: usize) -> Inventory<K, S, U>

Creates a new Inventory with a fixed slot count.

Source

pub fn new_dynamic(minimum: usize, maximum: usize) -> Inventory<K, S, U>

Creates a new dynamically sized Inventory. A minimum of minimum slots are garanteed to be present at all time. The quantity of slots will not go over maximum.

Source

pub fn use_item(&mut self, idx: usize) -> Result<Option<usize>, ItemError<K, U>>

Will attempt to decrease the durability of the item at the specified index. If the item has no durability value (None) or a non zero durability, it will return this value. If the item has a durability of 0 when using it, it will break and ItemError::ItemDestroyed will be returned.

Source

pub fn consume(&mut self, idx: usize) -> Result<usize, ItemError<K, U>>

Decreases the stack size by one and returns the current value. Once the stack size hits zero, it will return ItemError::StackConsumed.

Source

pub fn has_space(&self) -> bool

Looks if there is enough space to add another item stack.

Source

pub fn transfer<U2: Default>( &mut self, from_idx: usize, target: &mut Inventory<K, S, U>, to_idx: usize, quantity: usize, _with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Transfers a specified quantity of item from one slot of this inventory to a specified slot of the provided target inventory. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Transform::delete and Transform::insert_into.

Source

pub fn transfer_stack<U2: Default>( &mut self, from_idx: usize, target: &mut Inventory<K, S, U>, to_idx: usize, with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Transfers a whole stack from the specified slot into a specified slot of the provided target directory. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Transform::delete and Transform::insert_into.

Source

pub fn move_item<U2: Default>( &mut self, from_idx: usize, to_idx: usize, quantity: usize, _with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Moves a specified quantity of item from a slot to another. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Inventory::delete and Inventory::insert_into.

Source

pub fn move_stack<U2: Default>( &mut self, from_idx: usize, to_idx: usize, with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Moves a full stack of item from a slot to another. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors:

  • SlotEmpty: Nothing is present in the specified slot.
Source

pub fn delete( &mut self, idx: usize, quantity: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes a specified quantity of item from the specified slot.

Errors:

  • NotEnoughQuantity: Not enough items are present in the item stack.
  • SlotEmpty: Nothing is present in the specified slot.
Source

pub fn delete_stack( &mut self, idx: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes a full stack of item at the provided index and returns it.

Errors: See Transform::delete.

Source

pub fn delete_key( &mut self, key: &K, quantity: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes items by matching the key until the deleted quantity reaches the specified quantity.

Errors:

  • NotEnoughQuantity: Not enough items with the specified key are present in the inventory.
Source

pub fn has_quantity(&self, key: &K, quantity: usize) -> bool

Checks if the total quantity of items of the specified key are present in the inventory.

Source

pub fn has(&self, key: &K) -> bool

Checks if the inventory contains at least one ItemInstance of the specified key.

Source

pub fn get(&self, idx: usize) -> &Option<ItemInstance<K, U>>

Gets an immutable reference to the ItemInstance at the specified index.

Source

pub fn get_mut(&mut self, idx: usize) -> Option<&mut ItemInstance<K, U>>

Gets a mutable reference to the ItemInstance at the specified index.

Source

pub fn get_key(&self, key: &K) -> impl Iterator<Item = &ItemInstance<K, U>>

Finds the item instances using the specified key. Returns an iterator of immutable references.

Source

pub fn get_key_mut( &mut self, key: &K, ) -> impl Iterator<Item = &mut ItemInstance<K, U>>

Finds the item instances using the specified key. Returns an iterator of mutable references.

Source

pub fn insert_into<U2: Default>( &mut self, idx: usize, item: ItemInstance<K, U>, _item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Inserts the ItemInstance into the specified index.

It will eventually attempt to merge stacks together, but this is not implemented yet.

Errors:

  • SlotOccupied: The slot is currently occupied by another item type.
Source

pub fn insert<U2: Default>( &mut self, item: ItemInstance<K, U>, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>

Inserts the ItemInstance at the first available inventory space. If the inventory is dynamically size, it will attempt to create a slot and insert into it.

It will eventually attempt to merge stacks together, but this is not implemented yet.

Errors:

  • InventoryFull: The inventory is full and no more space can be created.
Source

pub fn first_empty_slot(&self) -> Option<usize>

Returns the first empty slot if any is available.

Trait Implementations§

Source§

impl<K: Clone, S: Clone + SlotType, U: Clone + Default> Clone for Inventory<K, S, U>

Source§

fn clone(&self) -> Inventory<K, S, U>

Returns a copy 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<K: Debug, S: Debug + SlotType, U: Debug + Default> Debug for Inventory<K, S, U>

Source§

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

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

impl<'de, K, S, U> Deserialize<'de> for Inventory<K, S, U>
where K: Deserialize<'de>, S: Deserialize<'de> + SlotType, U: Deserialize<'de> + Default,

Source§

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

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

impl<K, S, U> Serialize for Inventory<K, S, U>

Source§

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

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, S, U> Freeze for Inventory<K, S, U>

§

impl<K, S, U> RefUnwindSafe for Inventory<K, S, U>

§

impl<K, S, U> Send for Inventory<K, S, U>
where S: Send, K: Send, U: Send,

§

impl<K, S, U> Sync for Inventory<K, S, U>
where S: Sync, K: Sync, U: Sync,

§

impl<K, S, U> Unpin for Inventory<K, S, U>
where S: Unpin, K: Unpin, U: Unpin,

§

impl<K, S, U> UnwindSafe for Inventory<K, S, U>
where S: UnwindSafe, K: UnwindSafe, U: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,