pub struct Item {
item_id: Arc<ItemId>,
item_base: ItemBase,
components: Vec<Item>,
amount: NonZeroU32,
slots: Vec<InvSlot>,
item_config: Option<Box<ItemConfig>>,
hash: u64,
durability_lost: Option<u32>,
}
Expand description
NOTE: Do not call Item::clone
without consulting the core devs! It only
exists due to being required for message serialization at the moment, and
should not be used for any other purpose.
FIXME: Turn on a Clippy lint forbidding the use of Item::clone
using the
disallowed_method
feature.
Fields§
§item_id: Arc<ItemId>
item_id is hidden because it represents the persistent, storage entity ID for any item that has been saved to the database. Additionally, it (currently) holds interior mutable state, making it very dangerous to expose. We will work to eliminate this issue soon; for now, we try to make the system as foolproof as possible by greatly restricting opportunities for cloning the item_id.
item_base: ItemBase
item_def is hidden because changing the item definition for an item could change invariants like whether it was stackable (invalidating the amount).
components: Vec<Item>
components is hidden to maintain the following invariants:
- It should only contain modular components (and enhancements, once they exist)
- Enhancements (once they exist) should be compatible with the available slot shapes
- Modular components should agree with the tool kind
- There should be exactly one damage component and exactly one held component for modular weapons
amount: NonZeroU32
amount is hidden because it needs to maintain the invariant that only stackable items can have > 1 amounts.
slots: Vec<InvSlot>
The slots for items that this item has
item_config: Option<Box<ItemConfig>>
§hash: u64
§durability_lost: Option<u32>
Tracks how many deaths occurred while item was equipped, which is converted into the items durability. Only tracked for tools and armor currently.
Implementations§
source§impl Item
impl Item
pub const MAX_DURABILITY: u32 = 12u32
pub fn empty() -> Self
pub fn new_from_item_base( inner_item: ItemBase, components: Vec<Item>, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Self
pub fn new_from_item_definition_id( item_definition_id: ItemDefinitionId<'_>, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Result<Self, Error>
sourcepub fn new_from_asset_expect(asset_specifier: &str) -> Self
pub fn new_from_asset_expect(asset_specifier: &str) -> Self
Creates a new instance of an Item
from the provided asset identifier
Panics if the asset does not exist.
sourcepub fn new_from_asset_glob(asset_glob: &str) -> Result<Vec<Self>, Error>
pub fn new_from_asset_glob(asset_glob: &str) -> Result<Vec<Self>, Error>
Creates a Vec containing one of each item that matches the provided asset glob pattern
sourcepub fn new_from_asset(asset: &str) -> Result<Self, Error>
pub fn new_from_asset(asset: &str) -> Result<Self, Error>
Creates a new instance of an `Item from the provided asset identifier if it exists
sourcepub fn frontend_item(
&self,
ability_map: &AbilityMap,
msm: &MaterialStatManifest,
) -> FrontendItem
pub fn frontend_item( &self, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> FrontendItem
Creates a FrontendItem
out of this item for frontend use
sourcepub fn duplicate(
&self,
ability_map: &AbilityMap,
msm: &MaterialStatManifest,
) -> Self
pub fn duplicate( &self, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Self
Duplicates an item, creating an exact copy but with a new item ID
pub fn stacked_duplicates<'a>( &'a self, ability_map: &'a AbilityMap, msm: &'a MaterialStatManifest, count: u32, ) -> impl Iterator<Item = Self> + 'a
sourcefn reset_item_id(&mut self)
fn reset_item_id(&mut self)
Resets the item’s item ID to None, giving it a new identity. Used when dropping items into the world so that a new database record is created when they are picked up again.
NOTE: The creation of a new Arc
when resetting the item ID is critical
because every time a new Item
instance is created, it is cloned from
a single asset which results in an Arc
pointing to the same value in
memory. Therefore, every time an item instance is created this
method must be called in order to give it a unique identity.
sourcepub fn put_in_world(&mut self)
pub fn put_in_world(&mut self)
Removes the unique identity of an item - used when dropping an item on the floor. In the future this will need to be changed if we want to maintain a unique ID for an item even when it’s dropped and picked up by another player.
pub fn increase_amount( &mut self, increase_by: u32, ) -> Result<(), OperationFailure>
pub fn decrease_amount( &mut self, decrease_by: u32, ) -> Result<(), OperationFailure>
pub fn set_amount(&mut self, give_amount: u32) -> Result<(), OperationFailure>
pub fn persistence_access_add_component(&mut self, component: Item)
pub fn persistence_access_mutable_component( &mut self, index: usize, ) -> Option<&mut Self>
sourcepub fn update_item_state(
&mut self,
ability_map: &AbilityMap,
msm: &MaterialStatManifest,
)
pub fn update_item_state( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, )
Updates state of an item (important for creation of new items, persistence, and if components are ever added to items after initial creation)
sourcepub fn drain(&mut self) -> impl Iterator<Item = Item> + '_
pub fn drain(&mut self) -> impl Iterator<Item = Item> + '_
Returns an iterator that drains items contained within the item’s slots
pub fn item_definition_id(&self) -> ItemDefinitionId<'_>
pub fn is_same_item_def(&self, item_def: &ItemDef) -> bool
pub fn matches_recipe_input( &self, recipe_input: &RecipeInput, amount: u32, ) -> bool
pub fn is_salvageable(&self) -> bool
pub fn salvage_output(&self) -> impl Iterator<Item = (&str, u32)>
pub fn name(&self) -> Cow<'_, str>
pub fn description(&self) -> &str
pub fn kind(&self) -> Cow<'_, ItemKind>
pub fn amount(&self) -> u32
pub fn is_stackable(&self) -> bool
pub fn num_slots(&self) -> u16
sourcepub fn max_amount(&self) -> u32
pub fn max_amount(&self) -> u32
NOTE: invariant that amount() ≤ max_amount(), 1 ≤ max_amount(), and if !self.is_stackable(), self.max_amount() = 1.
pub fn quality(&self) -> Quality
pub fn components(&self) -> &[Item]
pub fn slots(&self) -> &[InvSlot]
pub fn slots_mut(&mut self) -> &mut [InvSlot]
pub fn item_config(&self) -> Option<&ItemConfig>
pub fn free_slots(&self) -> usize
pub fn populated_slots(&self) -> usize
pub fn slot(&self, slot: usize) -> Option<&InvSlot>
pub fn slot_mut(&mut self, slot: usize) -> Option<&mut InvSlot>
pub fn try_reclaim_from_block(block: Block) -> Option<Vec<(u32, Self)>>
pub fn ability_spec(&self) -> Option<Cow<'_, AbilitySpec>>
pub fn is_modular(&self) -> bool
pub fn item_hash(&self) -> u64
pub fn persistence_item_id(&self) -> String
pub fn durability_lost(&self) -> Option<u32>
pub fn stats_durability_multiplier(&self) -> DurabilityMultiplier
pub fn has_durability(&self) -> bool
pub fn increment_damage( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, )
pub fn persistence_durability(&self) -> Option<NonZeroU32>
pub fn persistence_set_durability(&mut self, value: Option<NonZeroU32>)
pub fn reset_durability( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, )
sourcepub fn take_half(
&mut self,
ability_map: &AbilityMap,
msm: &MaterialStatManifest,
) -> Option<Item>
pub fn take_half( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Option<Item>
If an item is stackable and has an amount greater than 1, creates a new item with half the amount (rounded down), and decreases the amount of the original item by the same quantity.
sourcepub fn can_merge(&self, other: &Self) -> bool
pub fn can_merge(&self, other: &Self) -> bool
Checks if this item and another are suitable for grouping into the same
[PickItem
].
Also see Item::try_merge
.
sourcepub fn try_merge(&mut self, other: Self) -> Result<Option<Self>, Self>
pub fn try_merge(&mut self, other: Self) -> Result<Option<Self>, Self>
Checks if this item and another are suitable for grouping into the same
[PickItem
] and combines stackable items if possible.
If the sum of both amounts is larger than their max amount, a remainder
item is returned as Ok(Some(remainder))
. A remainder item will
always be produced for non-stackable items.
If the items are not suitable for grouping Err(other)
will be
returned.
pub fn persistence_item_base(&self) -> &ItemBase
Trait Implementations§
source§impl<'de> Deserialize<'de> for Item
impl<'de> Deserialize<'de> for Item
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl ItemDesc for Item
impl ItemDesc for Item
source§fn description(&self) -> &str
fn description(&self) -> &str
fn kind(&self) -> Cow<'_, ItemKind>
fn amount(&self) -> NonZeroU32
fn quality(&self) -> Quality
fn num_slots(&self) -> u16
fn item_definition_id(&self) -> ItemDefinitionId<'_>
fn is_modular(&self) -> bool
fn components(&self) -> &[Item]
fn has_durability(&self) -> bool
fn durability_lost(&self) -> Option<u32>
fn stats_durability_multiplier(&self) -> DurabilityMultiplier
fn tool_info(&self) -> Option<ToolKind>
source§impl PartialEq for Item
impl PartialEq for Item
NOTE: This PartialEq instance is pretty broken! It doesn’t check item amount or any child items (and, arguably, doing so should be able to ignore things like item order within the main inventory or within each bag, and possibly even coalesce amounts, though these may be more controversial). Until such time as we find an actual need for a proper PartialEq instance, please don’t rely on this for anything!
Auto Trait Implementations§
impl Freeze for Item
impl RefUnwindSafe for Item
impl Send for Item
impl Sync for Item
impl Unpin for Item
impl UnwindSafe for Item
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<C, M> ConvertSaveload<M> for C
impl<C, M> ConvertSaveload<M> for C
§type Error = Infallible
type Error = Infallible
§fn convert_into<F>(
&self,
_: F,
) -> Result<<C as ConvertSaveload<M>>::Data, <C as ConvertSaveload<M>>::Error>
fn convert_into<F>( &self, _: F, ) -> Result<<C as ConvertSaveload<M>>::Data, <C as ConvertSaveload<M>>::Error>
Data
) using
entity to marker mapping function§fn convert_from<F>(
data: <C as ConvertSaveload<M>>::Data,
_: F,
) -> Result<C, <C as ConvertSaveload<M>>::Error>
fn convert_from<F>( data: <C as ConvertSaveload<M>>::Data, _: F, ) -> Result<C, <C as ConvertSaveload<M>>::Error>
Data
) using
entity to marker mapping function§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>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more