pub struct Inventory { /* private fields */ }

Implementations§

source§

impl Inventory

source

pub fn new(kind: InventoryKind) -> Self

source

pub fn with_title<'a>(kind: InventoryKind, title: impl IntoText<'a>) -> Self

source

pub fn slot(&self, idx: u16) -> Option<&ItemStack>

source

pub fn set_slot(&mut self, idx: u16, item: impl Into<Option<ItemStack>>)

Sets the slot at the given index to the given item stack.

See also Inventory::replace_slot.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
assert_eq!(inv.slot(0).unwrap().item, ItemKind::Diamond);
source

pub fn replace_slot( &mut self, idx: u16, item: impl Into<Option<ItemStack>> ) -> Option<ItemStack>

Replaces the slot at the given index with the given item stack, and returns the old stack in that slot.

See also Inventory::set_slot.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
let old = inv.replace_slot(0, ItemStack::new(ItemKind::IronIngot, 1, None));
assert_eq!(old.unwrap().item, ItemKind::Diamond);
source

pub fn swap_slot(&mut self, idx_a: u16, idx_b: u16)

Swap the contents of two slots. If the slots are the same, nothing happens.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
assert_eq!(inv.slot(1), None);
inv.swap_slot(0, 1);
assert_eq!(inv.slot(1).unwrap().item, ItemKind::Diamond);
source

pub fn set_slot_amount(&mut self, idx: u16, amount: u8)

Set the amount of items in the given slot without replacing the slot entirely. Valid values are 1-127, inclusive, and amount will be clamped to this range. If the slot is empty, nothing happens.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
inv.set_slot_amount(0, 64);
assert_eq!(inv.slot(0).unwrap().count(), 64);
source

pub fn slot_count(&self) -> u16

source

pub fn slots( &self ) -> impl ExactSizeIterator<Item = Option<&ItemStack>> + DoubleEndedIterator + FusedIterator + Clone + '_

source

pub fn kind(&self) -> InventoryKind

source

pub fn title(&self) -> &Text

The text displayed on the inventory’s title bar.

let inv = Inventory::with_title(InventoryKind::Generic9x3, "Box of Holding");
assert_eq!(inv.title(), &Text::from("Box of Holding"));
source

pub fn set_title<'a>(&mut self, title: impl IntoText<'a>)

Set the text displayed on the inventory’s title bar.

To get the old title, use Inventory::replace_title.

let mut inv = Inventory::new(InventoryKind::Generic9x3);
inv.set_title("Box of Holding");
source

pub fn replace_title<'a>(&mut self, title: impl IntoText<'a>) -> Text

Replace the text displayed on the inventory’s title bar, and returns the old text.

source

pub fn first_empty_slot_in(&self, range: Range<u16>) -> Option<u16>

Returns the first empty slot in the given range, or None if there are no empty slots in the range.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 1, None));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
assert_eq!(inv.first_empty_slot_in(0..6), Some(1));
assert_eq!(inv.first_empty_slot_in(2..6), Some(4));
source

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

Returns the first empty slot in the inventory, or None if there are no empty slots.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 1, None));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
assert_eq!(inv.first_empty_slot(), Some(1));
source

pub fn first_slot_with_item_in( &self, item: ItemKind, stack_max: u8, range: Range<u16> ) -> Option<u16>

Returns the first slot with the given [ItemKind] in the inventory where count() < stack_max, or None if there are no empty slots.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64, None));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1, None));
assert_eq!(
    inv.first_slot_with_item_in(ItemKind::GoldIngot, 64, 0..5),
    Some(4)
);
source

pub fn first_slot_with_item(&self, item: ItemKind, stack_max: u8) -> Option<u16>

Returns the first slot with the given [ItemKind] in the inventory where count() < stack_max, or None if there are no empty slots.

let mut inv = Inventory::new(InventoryKind::Generic9x1);
inv.set_slot(0, ItemStack::new(ItemKind::Diamond, 1, None));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64, None));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1, None));
inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1, None));
assert_eq!(inv.first_slot_with_item(ItemKind::GoldIngot, 64), Some(4));

Trait Implementations§

source§

impl Clone for Inventory

source§

fn clone(&self) -> Inventory

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 Component for Inventorywhere Self: Send + Sync + 'static,

§

type Storage = TableStorage

A marker type indicating the storage type used for this component. This must be either TableStorage or SparseStorage.
source§

impl Debug for Inventory

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<C> Bundle for Cwhere C: Component,

source§

fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId) )

source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Cwhere F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a, Aligned>,

§

impl<T> Downcast for Twhere T: Any,

§

fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for Twhere T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T, Global>) -> Arc<dyn Any + Sync + Send, Global>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<C> DynamicBundle for Cwhere C: Component,

source§

fn get_components( self, func: &mut impl FnMut(StorageType, OwningPtr<'_, Aligned>) )

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more