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: MoveToFrontModeConfigures how item deletion is handled.
sizing_mode: InventorySizingModeConfigures if the inventory resizes when item are inserted/removed or not.
Implementations§
Source§impl<K, S: SlotType, U: Default> Inventory<K, S, U>
impl<K, S: SlotType, U: Default> Inventory<K, S, U>
Sourcepub fn new(
content: Vec<Option<ItemInstance<K, U>>>,
slot_restriction: Vec<Option<S>>,
move_to_front: MoveToFrontMode,
sizing_mode: InventorySizingMode,
) -> Self
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>
impl<K: PartialEq + Clone + Debug + Hash + Eq, S: SlotType, U: Default + Clone + Debug + PartialEq> Inventory<K, S, U>
Sourcepub fn new_fixed(count: usize) -> Inventory<K, S, U>
pub fn new_fixed(count: usize) -> Inventory<K, S, U>
Creates a new Inventory with a fixed slot count.
Sourcepub fn new_dynamic(minimum: usize, maximum: usize) -> Inventory<K, S, U>
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.
Sourcepub fn use_item(&mut self, idx: usize) -> Result<Option<usize>, ItemError<K, U>>
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.
Sourcepub fn consume(&mut self, idx: usize) -> Result<usize, ItemError<K, U>>
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.
Sourcepub 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>>
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.
Sourcepub 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>>
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.
Sourcepub 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>>
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.
Sourcepub 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>>
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.
Sourcepub fn delete(
&mut self,
idx: usize,
quantity: usize,
) -> Result<ItemInstance<K, U>, ItemError<K, U>>
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.
Sourcepub fn delete_stack(
&mut self,
idx: usize,
) -> Result<ItemInstance<K, U>, ItemError<K, U>>
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.
Sourcepub fn delete_key(
&mut self,
key: &K,
quantity: usize,
) -> Result<ItemInstance<K, U>, ItemError<K, U>>
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.
Sourcepub fn has_quantity(&self, key: &K, quantity: usize) -> bool
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.
Sourcepub fn has(&self, key: &K) -> bool
pub fn has(&self, key: &K) -> bool
Checks if the inventory contains at least one ItemInstance of the specified key.
Sourcepub fn get(&self, idx: usize) -> &Option<ItemInstance<K, U>>
pub fn get(&self, idx: usize) -> &Option<ItemInstance<K, U>>
Gets an immutable reference to the ItemInstance at the specified index.
Sourcepub fn get_mut(&mut self, idx: usize) -> Option<&mut ItemInstance<K, U>>
pub fn get_mut(&mut self, idx: usize) -> Option<&mut ItemInstance<K, U>>
Gets a mutable reference to the ItemInstance at the specified index.
Sourcepub fn get_key(&self, key: &K) -> impl Iterator<Item = &ItemInstance<K, U>>
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.
Sourcepub fn get_key_mut(
&mut self,
key: &K,
) -> impl Iterator<Item = &mut ItemInstance<K, U>>
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.
Sourcepub fn insert_into<U2: Default>(
&mut self,
idx: usize,
item: ItemInstance<K, U>,
_item_defs: &ItemDefinitions<K, S, U2>,
) -> Result<(), ItemError<K, U>>
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.
Sourcepub fn insert<U2: Default>(
&mut self,
item: ItemInstance<K, U>,
item_defs: &ItemDefinitions<K, S, U2>,
) -> Result<(), ItemError<K, U>>
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.
Sourcepub fn first_empty_slot(&self) -> Option<usize>
pub fn first_empty_slot(&self) -> Option<usize>
Returns the first empty slot if any is available.