Struct veloren_common::comp::inventory::item::Item

source ·
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

source

pub const MAX_DURABILITY: u32 = 12u32

source

pub fn empty() -> Self

source

pub fn new_from_item_base( inner_item: ItemBase, components: Vec<Item>, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Self

source

pub fn new_from_item_definition_id( item_definition_id: ItemDefinitionId<'_>, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Result<Self, Error>

source

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.

source

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

source

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

source

pub fn frontend_item( &self, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> FrontendItem

Creates a FrontendItem out of this item for frontend use

source

pub fn duplicate( &self, ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Self

Duplicates an item, creating an exact copy but with a new item ID

source

pub fn stacked_duplicates<'a>( &'a self, ability_map: &'a AbilityMap, msm: &'a MaterialStatManifest, count: u32, ) -> impl Iterator<Item = Self> + 'a

source

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.

source

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.

source

pub fn increase_amount( &mut self, increase_by: u32, ) -> Result<(), OperationFailure>

source

pub fn decrease_amount( &mut self, decrease_by: u32, ) -> Result<(), OperationFailure>

source

pub fn set_amount(&mut self, give_amount: u32) -> Result<(), OperationFailure>

source

pub fn persistence_access_add_component(&mut self, component: Item)

source

pub fn persistence_access_mutable_component( &mut self, index: usize, ) -> Option<&mut Self>

source

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)

source

pub fn drain(&mut self) -> impl Iterator<Item = Item> + '_

Returns an iterator that drains items contained within the item’s slots

source

pub fn item_definition_id(&self) -> ItemDefinitionId<'_>

source

pub fn is_same_item_def(&self, item_def: &ItemDef) -> bool

source

pub fn matches_recipe_input( &self, recipe_input: &RecipeInput, amount: u32, ) -> bool

source

pub fn is_salvageable(&self) -> bool

source

pub fn salvage_output(&self) -> impl Iterator<Item = (&str, u32)>

source

pub fn name(&self) -> Cow<'_, str>

👎Deprecated: since item i18n
source

pub fn description(&self) -> &str

👎Deprecated: since item i18n
source

pub fn kind(&self) -> Cow<'_, ItemKind>

source

pub fn amount(&self) -> u32

source

pub fn is_stackable(&self) -> bool

source

pub fn num_slots(&self) -> u16

source

pub fn max_amount(&self) -> u32

NOTE: invariant that amount() ≤ max_amount(), 1 ≤ max_amount(), and if !self.is_stackable(), self.max_amount() = 1.

source

pub fn quality(&self) -> Quality

source

pub fn components(&self) -> &[Item]

source

pub fn slots(&self) -> &[InvSlot]

source

pub fn slots_mut(&mut self) -> &mut [InvSlot]

source

pub fn item_config(&self) -> Option<&ItemConfig>

source

pub fn free_slots(&self) -> usize

source

pub fn populated_slots(&self) -> usize

source

pub fn slot(&self, slot: usize) -> Option<&InvSlot>

source

pub fn slot_mut(&mut self, slot: usize) -> Option<&mut InvSlot>

source

pub fn try_reclaim_from_block(block: Block) -> Option<Vec<(u32, Self)>>

source

pub fn ability_spec(&self) -> Option<Cow<'_, AbilitySpec>>

source

pub fn tags(&self) -> Vec<ItemTag>

source

pub fn is_modular(&self) -> bool

source

pub fn item_hash(&self) -> u64

source

pub fn persistence_item_id(&self) -> String

source

pub fn durability_lost(&self) -> Option<u32>

source

pub fn stats_durability_multiplier(&self) -> DurabilityMultiplier

source

pub fn has_durability(&self) -> bool

source

pub fn increment_damage( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, )

source

pub fn persistence_durability(&self) -> Option<NonZeroU32>

source

pub fn persistence_set_durability(&mut self, value: Option<NonZeroU32>)

source

pub fn reset_durability( &mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest, )

source

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.

source

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.

source

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.

source

pub fn persistence_item_base(&self) -> &ItemBase

Trait Implementations§

source§

impl Clone for Item

source§

fn clone(&self) -> Item

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 Debug for Item

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Item

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<&Item> for Body

source§

fn from(item: &Item) -> Self

Converts to this type from the input type.
source§

impl Hash for Item

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl ItemDesc for Item

source§

fn description(&self) -> &str

👎Deprecated: since item i18n
source§

fn name(&self) -> Cow<'_, str>

👎Deprecated: since item i18n
source§

fn kind(&self) -> Cow<'_, ItemKind>

source§

fn amount(&self) -> NonZeroU32

source§

fn quality(&self) -> Quality

source§

fn num_slots(&self) -> u16

source§

fn item_definition_id(&self) -> ItemDefinitionId<'_>

source§

fn tags(&self) -> Vec<ItemTag>

source§

fn is_modular(&self) -> bool

source§

fn components(&self) -> &[Item]

source§

fn has_durability(&self) -> bool

source§

fn durability_lost(&self) -> Option<u32>

source§

fn stats_durability_multiplier(&self) -> DurabilityMultiplier

source§

fn tool_info(&self) -> Option<ToolKind>

source§

fn i18n(&self, i18n: &ItemI18n) -> (Content, Content)

Return name’s and description’s localization descriptors
source§

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!

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Item

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> 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<T> CallHasher for T
where T: Hash,

§

fn get_hash<H>(&self, hasher: H) -> u64
where H: Hasher,

source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<C, M> ConvertSaveload<M> for C

§

type Data = C

(De)Serializable data representation for data type
§

type Error = Infallible

Error may occur during serialization or deserialization of component
§

fn convert_into<F>( &self, _: F, ) -> Result<<C as ConvertSaveload<M>>::Data, <C as ConvertSaveload<M>>::Error>
where F: FnMut(Entity) -> Option<M>,

Convert this data type into serializable form (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>
where F: FnMut(M) -> Option<Entity>,

Convert this data from a deserializable form (Data) using entity to marker mapping function
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.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<Context> SubContext<Context> for Context

source§

fn sub_context(self) -> Context

source§

impl<T> ToOwned for T
where 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 T
where 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 T
where 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 T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

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

§

impl<T> Resource for T
where T: Any + Send + Sync,

§

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