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
impl Inventory
pub fn new(kind: InventoryKind) -> Inventory
pub fn with_title<'a, T>(kind: InventoryKind, title: T) -> Inventorywhere
T: IntoText<'a>,
pub fn slot(&self, idx: u16) -> &ItemStack
sourcepub fn set_slot<I>(&mut self, idx: u16, item: I)
pub fn set_slot<I>(&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, None));
assert_eq!(inv.slot(0).item, ItemKind::Diamond);
sourcepub fn replace_slot<I>(&mut self, idx: u16, item: I) -> ItemStack
pub fn replace_slot<I>(&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, None));
let old = inv.replace_slot(0, ItemStack::new(ItemKind::IronIngot, 1, None));
assert_eq!(old.item, ItemKind::Diamond);
sourcepub fn swap_slot(&mut self, idx_a: u16, idx_b: u16)
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!(inv.slot(1).is_empty());
inv.swap_slot(0, 1);
assert_eq!(inv.slot(1).item, ItemKind::Diamond);
sourcepub fn set_slot_amount(&mut self, idx: u16, amount: i8)
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, None));
inv.set_slot_amount(0, 64);
assert_eq!(inv.slot(0).count, 64);
pub fn slot_count(&self) -> u16
pub fn slots( &self, ) -> impl ExactSizeIterator + DoubleEndedIterator + FusedIterator + Clone
pub fn kind(&self) -> InventoryKind
sourcepub fn title(&self) -> &Text
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"));
sourcepub fn set_title<'a, T>(&mut self, title: T)where
T: IntoText<'a>,
pub fn set_title<'a, T>(&mut self, title: T)where
T: 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");
sourcepub fn replace_title<'a, T>(&mut self, title: T) -> Textwhere
T: IntoText<'a>,
pub fn replace_title<'a, T>(&mut self, title: T) -> Textwhere
T: IntoText<'a>,
Replace the text displayed on the inventory’s title bar, and returns the old text.
sourcepub fn first_empty_slot_in(&self, range: Range<u16>) -> Option<u16>
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));
sourcepub fn first_empty_slot(&self) -> Option<u16>
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));
sourcepub fn first_slot_with_item_in(
&self,
item: ItemKind,
stack_max: i8,
range: Range<u16>,
) -> Option<u16>
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, 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)
);
sourcepub fn first_slot_with_item(&self, item: ItemKind, stack_max: i8) -> Option<u16>
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, 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 Component for Inventory
impl Component for Inventory
source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
§fn register_component_hooks(_hooks: &mut ComponentHooks)
fn register_component_hooks(_hooks: &mut ComponentHooks)
ComponentHooks
.Auto Trait Implementations§
impl Freeze for Inventory
impl RefUnwindSafe for Inventory
impl Send for Inventory
impl Sync for Inventory
impl Unpin for Inventory
impl UnwindSafe for Inventory
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
§fn get_component_ids(
components: &Components,
ids: &mut impl FnMut(Option<ComponentId>),
)
fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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 T
impl<T> DowncastSync for T
§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.