Skip to main content

Inventory

Struct Inventory 

Source
pub struct Inventory {
    pub readonly: bool,
    /* private fields */
}

Fields§

§readonly: bool

Makes an inventory read-only for clients. This will prevent adding or removing items. If this is a player inventory This will also make it impossible to drop items while not in the inventory (e.g. by pressing Q)

Implementations§

Source§

impl Inventory

Source

pub fn new(kind: InventoryKind) -> Self

Source

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

Source

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

Source

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

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));
assert_eq!(inv.slot(0).item, ItemKind::Diamond);
§Panics

If the slot index is out of bounds.

Source

pub fn replace_slot<I: Into<ItemStack>>( &mut self, idx: u16, item: I, ) -> 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));
let old = inv.replace_slot(0, ItemStack::new(ItemKind::IronIngot, 1));
assert_eq!(old.item, ItemKind::Diamond);
§Panics

If the slot index is out of bounds.

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));
assert!(inv.slot(1).is_empty());
inv.swap_slot(0, 1);
assert_eq!(inv.slot(1).item, ItemKind::Diamond);
§Panics

If either slot index is out of bounds.

Source

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

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));
inv.set_slot_amount(0, 64);
assert_eq!(inv.slot(0).count, 64);
§Panics

If the slot index is out of bounds.

Source

pub fn slot_count(&self) -> u16

Source

pub fn slots( &self, ) -> impl ExactSizeIterator<Item = &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, T: IntoText<'a>>(&mut self, title: T)

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, T: IntoText<'a>>(&mut self, title: T) -> 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));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 1));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1));
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));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 1));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1));
assert_eq!(inv.first_empty_slot(), Some(1));
Source

pub fn first_slot_with_item_in( &self, item: ItemKind, stack_max: i8, 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));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1));
inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1));
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: i8) -> 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));
inv.set_slot(2, ItemStack::new(ItemKind::GoldIngot, 64));
inv.set_slot(3, ItemStack::new(ItemKind::IronIngot, 1));
inv.set_slot(4, ItemStack::new(ItemKind::GoldIngot, 1));
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 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 Component for Inventory
where Self: Send + Sync + 'static,

Source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table

A constant indicating the storage type used for this component.
Source§

type Mutability = Mutable

A marker type to assist Bevy with determining if this component is mutable, or immutable. Mutable components will have [Component<Mutability = Mutable>], while immutable components will instead have [Component<Mutability = Immutable>]. Read more
Source§

fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )

Registers required components. Read more
Source§

fn clone_behavior() -> ComponentCloneBehavior

Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component. Read more
Source§

fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>>

Returns [ComponentRelationshipAccessor] required for working with relationships in dynamic contexts. Read more
§

fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_add [ComponentHook] for this [Component] if one is defined.
§

fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_insert [ComponentHook] for this [Component] if one is defined.
§

fn on_discard() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_discard [ComponentHook] for this [Component] if one is defined.
§

fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_remove [ComponentHook] for this [Component] if one is defined.
§

fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_despawn [ComponentHook] for this [Component] if one is defined.
§

fn map_entities<E>(_this: &mut Self, _mapper: &mut E)
where E: EntityMapper,

Maps the entities on this component using the given [EntityMapper]. This is used to remap entities in contexts like scenes and entity cloning. When deriving [Component], this is populated by annotating fields containing entities with #[entities] Read more
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 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
§

impl<C> Bundle for C
where C: Component,

§

fn component_ids( components: &mut ComponentsRegistrator<'_>, ) -> impl Iterator<Item = ComponentId> + use<C>

§

fn get_component_ids( components: &Components, ) -> impl Iterator<Item = Option<ComponentId>>

Return a iterator over this [Bundle]’s component ids. This will be None if the component has not been registered.
§

impl<C> BundleFromComponents for C
where C: Component,

§

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

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
§

impl<T> Downcast for T
where T: Any,

§

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

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

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

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

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

Converts &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)

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

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<C> DynamicBundle for C
where C: Component,

§

type Effect = ()

An operation on the entity that happens after inserting this bundle.
§

unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect

Moves the components out of the bundle. Read more
§

unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )

Applies the after-effects of spawning this bundle. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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

§

impl<T> IntoResult<T> for T

§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
§

impl<A> Is for A
where A: Any,

§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ConditionalSend for T
where T: Send,