pub struct ItemStack {
pub item: ItemRef,
pub count: i32,
patch: DataComponentPatch,
}Expand description
A stack of items with a count and component modifications.
Fields§
§item: ItemRefThe item type. AIR represents an empty stack.
count: i32The number of items in this stack.
patch: DataComponentPatchModifications to the prototype components.
Implementations§
Source§impl ItemStack
impl ItemStack
Sourcepub fn with_count(item: ItemRef, count: i32) -> Self
pub fn with_count(item: ItemRef, count: i32) -> Self
Creates a new item stack with the specified count.
Sourcepub fn with_count_and_patch(
item: ItemRef,
count: i32,
patch: DataComponentPatch,
) -> Self
pub fn with_count_and_patch( item: ItemRef, count: i32, patch: DataComponentPatch, ) -> Self
Creates a new item stack with the specified count and component patch.
fn prototype(&self) -> &'static DataComponentMap
pub fn is_empty(&self) -> bool
pub fn item(&self) -> ItemRef
pub fn count(&self) -> i32
pub fn set_count(&mut self, count: i32)
Sourcepub fn split(&mut self, amount: i32) -> Self
pub fn split(&mut self, amount: i32) -> Self
Splits off the specified amount from this stack and returns it as a new stack.
If the amount is greater than or equal to the current count, this stack becomes empty and the entire contents are returned.
Sourcepub fn copy_with_count(&self, count: i32) -> Self
pub fn copy_with_count(&self, count: i32) -> Self
Copies the identity (item type and patch) from another stack.
Used when splitting stacks to preserve components.
Sourcepub fn is_stackable(&self) -> bool
pub fn is_stackable(&self) -> bool
Returns true if this item can stack (max stack size > 1 and not damaged). Damaged items cannot stack.
Sourcepub fn is_damageable_item(&self) -> bool
pub fn is_damageable_item(&self) -> bool
Returns true if this item can take damage.
Sourcepub fn is_damaged(&self) -> bool
pub fn is_damaged(&self) -> bool
Returns true if this item has taken damage.
Sourcepub fn get_damage_value(&self) -> i32
pub fn get_damage_value(&self) -> i32
Gets the current damage value of this item.
Sourcepub fn set_damage_value(&mut self, value: i32)
pub fn set_damage_value(&mut self, value: i32)
Sets the damage value of this item.
Sourcepub fn get_max_damage(&self) -> i32
pub fn get_max_damage(&self) -> i32
Gets the maximum damage this item can take before breaking.
Sourcepub fn next_damage_will_break(&self) -> bool
pub fn next_damage_will_break(&self) -> bool
Returns vanilla ItemStack.nextDamageWillBreak().
Sourcepub fn hurt_and_break(
&mut self,
amount: i32,
has_infinite_materials: bool,
) -> bool
pub fn hurt_and_break( &mut self, amount: i32, has_infinite_materials: bool, ) -> bool
Damages the item and breaks it if durability reaches zero.
Returns true if the item broke and should be removed/replaced.
Sourcepub fn has<T: 'static>(&self, component: DataComponentType<T>) -> bool
pub fn has<T: 'static>(&self, component: DataComponentType<T>) -> bool
Returns true if this item has the specified component (by type).
Sourcepub fn has_component(&self, key: &Identifier) -> bool
pub fn has_component(&self, key: &Identifier) -> bool
Returns true if this item has the specified component (by key).
pub fn is_same_item(a: &Self, b: &Self) -> bool
Sourcepub fn is_same_item_same_components(a: &Self, b: &Self) -> bool
pub fn is_same_item_same_components(a: &Self, b: &Self) -> bool
Checks if two stacks have the same item and components.
pub fn matches(a: &Self, b: &Self) -> bool
pub fn is(&self, item: ItemRef) -> bool
pub fn max_stack_size(&self) -> i32
Sourcepub fn get_equippable(&self) -> Option<&Equippable>
pub fn get_equippable(&self) -> Option<&Equippable>
Returns the equippable component if this item has one.
Sourcepub fn get_equippable_slot(&self) -> Option<EquippableSlot>
pub fn get_equippable_slot(&self) -> Option<EquippableSlot>
Returns the equipment slot this item can be equipped to, if any.
Sourcepub fn is_equippable_in_slot(&self, slot: EquippableSlot) -> bool
pub fn is_equippable_in_slot(&self, slot: EquippableSlot) -> bool
Returns true if this item can be equipped in the given slot.
Sourcepub fn get_effective_value_raw(
&self,
key: &Identifier,
) -> Option<&ComponentData>
pub fn get_effective_value_raw( &self, key: &Identifier, ) -> Option<&ComponentData>
Gets the raw component data by key.
Sourcepub fn get<T: Component>(&self, component: DataComponentType<T>) -> Option<&T>
pub fn get<T: Component>(&self, component: DataComponentType<T>) -> Option<&T>
Gets the effective value of a component, considering the patch and prototype.
Returns None if the component is not present or has been removed.
Sourcepub fn get_or_default<T: Component>(
&self,
component: DataComponentType<T>,
default: T,
) -> T
pub fn get_or_default<T: Component>( &self, component: DataComponentType<T>, default: T, ) -> T
Gets the effective value of a component, or returns the default value if not present.
Sourcepub fn set<T: Component>(&mut self, component: DataComponentType<T>, value: T)
pub fn set<T: Component>(&mut self, component: DataComponentType<T>, value: T)
Sets a component value in this item’s patch, overriding the prototype.
Sourcepub fn remove<T: 'static>(&mut self, component: DataComponentType<T>)
pub fn remove<T: 'static>(&mut self, component: DataComponentType<T>)
Removes a component from this item (marks it as removed in the patch). This will hide the component even if it exists in the prototype.
Sourcepub fn clear<T: 'static>(&mut self, component: DataComponentType<T>)
pub fn clear<T: 'static>(&mut self, component: DataComponentType<T>)
Clears any patch entry for this component (neither set nor removed). The prototype value will be visible again.
Sourcepub fn patch(&self) -> &DataComponentPatch
pub fn patch(&self) -> &DataComponentPatch
Returns a reference to the component patch.
Sourcepub fn get_destroy_speed(&self, block_state_id: BlockStateId) -> f32
pub fn get_destroy_speed(&self, block_state_id: BlockStateId) -> f32
Returns the mining speed for the given block state ID. If no Tool component is present, returns 1.0 (hand speed).
Sourcepub fn is_correct_tool_for_drops(&self, block_state_id: BlockStateId) -> bool
pub fn is_correct_tool_for_drops(&self, block_state_id: BlockStateId) -> bool
Returns true if this tool is correct for getting drops from the block.
Sourcepub fn get_tool_damage_per_block(&self) -> i32
pub fn get_tool_damage_per_block(&self) -> i32
Returns the damage per block for this tool (how much durability is consumed per block mined). Returns 0 if no Tool component is present.
Sourcepub fn can_destroy_blocks_in_creative(&self) -> bool
pub fn can_destroy_blocks_in_creative(&self) -> bool
Returns true if this tool can destroy blocks in creative mode. Returns true if no Tool component is present (default behavior).
pub fn get_enchantment_level(&self, enchantment: &Identifier) -> i32
pub fn get_enchantments(&self) -> Option<&ItemEnchantments>
Sourcepub fn set_damage_fraction(&mut self, _fraction: f32, _add: bool)
pub fn set_damage_fraction(&mut self, _fraction: f32, _add: bool)
Sets the damage/durability as a fraction (0.0 = broken, 1.0 = full).
If add is true, adds to current damage instead of setting.
Sourcepub fn enchant_randomly<R: Rng>(
&mut self,
_options: &EnchantmentOptions,
_rng: &mut R,
)
pub fn enchant_randomly<R: Rng>( &mut self, _options: &EnchantmentOptions, _rng: &mut R, )
Enchants this item randomly with enchantments from the given options.
Sourcepub fn enchant_with_levels<R: Rng>(
&mut self,
_level: i32,
_options: &EnchantmentOptions,
_rng: &mut R,
)
pub fn enchant_with_levels<R: Rng>( &mut self, _level: i32, _options: &EnchantmentOptions, _rng: &mut R, )
Enchants this item as if using an enchanting table at the given level.
Sourcepub fn copy_components<R: Rng>(
&mut self,
_source: CopySource,
_include: &[Identifier],
_ctx: &LootContext<'_, R>,
)
pub fn copy_components<R: Rng>( &mut self, _source: CopySource, _include: &[Identifier], _ctx: &LootContext<'_, R>, )
Copies components from a source (block entity, attacker, etc.) to this item.
Sourcepub fn copy_block_state<R: Rng>(
&mut self,
_block: &Identifier,
_properties: &[&str],
_ctx: &LootContext<'_, R>,
)
pub fn copy_block_state<R: Rng>( &mut self, _block: &Identifier, _properties: &[&str], _ctx: &LootContext<'_, R>, )
Copies block state properties to this item (for blocks like note_block).
Sourcepub fn set_components_from_json(&mut self, _components: &str)
pub fn set_components_from_json(&mut self, _components: &str)
Sets components from a JSON string representation.
Sourcepub fn set_custom_data(&mut self, _tag: &str)
pub fn set_custom_data(&mut self, _tag: &str)
Sets custom NBT data on this item (merges with existing custom_data).
Sourcepub fn apply_furnace_smelt(&mut self)
pub fn apply_furnace_smelt(&mut self)
Applies furnace smelting to convert this item (e.g., raw iron -> iron ingot).
Sourcepub fn create_exploration_map(
&mut self,
_destination: &Identifier,
_decoration: &Identifier,
_zoom: i32,
_skip_existing_chunks: bool,
)
pub fn create_exploration_map( &mut self, _destination: &Identifier, _decoration: &Identifier, _zoom: i32, _skip_existing_chunks: bool, )
Creates an exploration map pointing to a structure.
Sourcepub fn set_name(&mut self, _name: &str, _target: NameTarget)
pub fn set_name(&mut self, _name: &str, _target: NameTarget)
Sets the custom name or item name of this item.
Sourcepub fn set_ominous_bottle_amplifier(&mut self, _amplifier: i32)
pub fn set_ominous_bottle_amplifier(&mut self, _amplifier: i32)
Sets the ominous bottle amplifier.
Sourcepub fn set_potion(&mut self, _id: &Identifier)
pub fn set_potion(&mut self, _id: &Identifier)
Sets the potion type for this item.
Sourcepub fn set_stew_effects<R: Rng>(
&mut self,
_effects: &[StewEffect],
_rng: &mut R,
)
pub fn set_stew_effects<R: Rng>( &mut self, _effects: &[StewEffect], _rng: &mut R, )
Sets the suspicious stew effects for this item.
Sourcepub fn set_instrument<R: Rng>(&mut self, _options: &Identifier, _rng: &mut R)
pub fn set_instrument<R: Rng>(&mut self, _options: &Identifier, _rng: &mut R)
Sets the instrument for a goat horn.
pub fn set_enchantments( &mut self, enchantments: &[(Identifier, u32)], add: bool, )
Sourcepub fn upgrade_enchantment(&mut self, enchantment: Identifier, level: u32)
pub fn upgrade_enchantment(&mut self, enchantment: Identifier, level: u32)
Vanilla ItemStack.enchant → Mutable.upgrade: keeps the higher of existing vs new level.
Sourcepub fn set_item(&mut self, new_item: &Identifier)
pub fn set_item(&mut self, new_item: &Identifier)
Changes the item type entirely.
Sourcepub fn copy_name<R: Rng>(
&mut self,
_source: CopySource,
_ctx: &LootContext<'_, R>,
)
pub fn copy_name<R: Rng>( &mut self, _source: CopySource, _ctx: &LootContext<'_, R>, )
Copies the name from a source entity/block to this item.
Sourcepub fn set_lore(&mut self, _lore: &[&str], _mode: ListOperation)
pub fn set_lore(&mut self, _lore: &[&str], _mode: ListOperation)
Sets lore lines on this item.
Sourcepub fn set_contents<R: Rng>(
&mut self,
_entries: &[LootEntry],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)
pub fn set_contents<R: Rng>( &mut self, _entries: &[LootEntry], _component_type: &Identifier, _ctx: &mut LootContext<'_, R>, )
Sets container inventory contents.
Sourcepub fn modify_contents<R: Rng>(
&mut self,
_modifier: &[ConditionalLootFunction],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)
pub fn modify_contents<R: Rng>( &mut self, _modifier: &[ConditionalLootFunction], _component_type: &Identifier, _ctx: &mut LootContext<'_, R>, )
Modifies existing container contents.
Sourcepub fn set_loot_table(&mut self, _loot_table: &Identifier, _seed: Option<i64>)
pub fn set_loot_table(&mut self, _loot_table: &Identifier, _seed: Option<i64>)
Sets the container’s loot table reference.
Sourcepub fn set_attributes<R: Rng>(
&mut self,
_modifiers: &[AttributeModifier],
_replace: bool,
_rng: &mut R,
)
pub fn set_attributes<R: Rng>( &mut self, _modifiers: &[AttributeModifier], _replace: bool, _rng: &mut R, )
Sets attribute modifiers on this item.
Sourcepub fn fill_player_head<R: Rng>(
&mut self,
_entity: LootContextEntity,
_ctx: &LootContext<'_, R>,
)
pub fn fill_player_head<R: Rng>( &mut self, _entity: LootContextEntity, _ctx: &LootContext<'_, R>, )
Fills a player head with texture from an entity.
Sourcepub fn copy_custom_data<R: Rng>(
&mut self,
_source: CopySource,
_operations: &[CopyDataOperation],
_ctx: &LootContext<'_, R>,
)
pub fn copy_custom_data<R: Rng>( &mut self, _source: CopySource, _operations: &[CopyDataOperation], _ctx: &LootContext<'_, R>, )
Copies custom NBT data from a source.
Sets banner pattern layers.
Sourcepub fn set_fireworks(
&mut self,
_explosions: Option<&[FireworkExplosion]>,
_flight_duration: Option<i32>,
)
pub fn set_fireworks( &mut self, _explosions: Option<&[FireworkExplosion]>, _flight_duration: Option<i32>, )
Sets firework rocket properties.
Sourcepub fn set_firework_explosion(&mut self, _explosion: &FireworkExplosion)
pub fn set_firework_explosion(&mut self, _explosion: &FireworkExplosion)
Sets firework star explosion properties.
Sourcepub fn set_book_cover(
&mut self,
_title: Option<&str>,
_author: Option<&str>,
_generation: Option<i32>,
)
pub fn set_book_cover( &mut self, _title: Option<&str>, _author: Option<&str>, _generation: Option<i32>, )
Sets book cover (title/author for written books).
Sourcepub fn set_written_book_pages(&mut self, _pages: &[&str], _mode: ListOperation)
pub fn set_written_book_pages(&mut self, _pages: &[&str], _mode: ListOperation)
Sets written book page contents.
Sourcepub fn set_writable_book_pages(&mut self, _pages: &[&str], _mode: ListOperation)
pub fn set_writable_book_pages(&mut self, _pages: &[&str], _mode: ListOperation)
Sets writable book page contents.
Sourcepub fn toggle_tooltips(&mut self, _toggles: &[(Identifier, bool)])
pub fn toggle_tooltips(&mut self, _toggles: &[(Identifier, bool)])
Toggles tooltip visibility for components.
Sourcepub fn set_custom_model_data(&mut self, _value: i32)
pub fn set_custom_model_data(&mut self, _value: i32)
Sets custom model data.
pub fn components_equal(&self, other: &Self) -> bool
Source§impl ItemStack
impl ItemStack
Sourcepub fn read_untrusted(data: &mut Cursor<&[u8]>) -> Result<Self>
pub fn read_untrusted(data: &mut Cursor<&[u8]>) -> Result<Self>
Reads an item stack using the delimited (untrusted) component format.
Vanilla uses this for serverbound packets where component data is
length-prefixed (e.g., ServerboundSetCreativeModeSlotPacket).
Source§impl ItemStack
impl ItemStack
Sourcepub fn to_nbt_tag_ref(&self) -> NbtTag
pub fn to_nbt_tag_ref(&self) -> NbtTag
Converts this item stack to an NBT tag for persistent storage without consuming it.
Source§impl ItemStack
impl ItemStack
Sourcepub fn from_borrowed_compound(
compound: &NbtCompoundView<'_, '_>,
) -> Option<Self>
pub fn from_borrowed_compound( compound: &NbtCompoundView<'_, '_>, ) -> Option<Self>
Parses an ItemStack from a borrowed NbtCompoundView.
This is useful for loading items from disk where we have borrowed NBT data and want to avoid the overhead of converting to an owned tag first.
Trait Implementations§
Source§impl FromNbtTag for ItemStack
impl FromNbtTag for ItemStack
Source§fn from_nbt_tag(tag: BorrowedNbtTag<'_, '_>) -> Option<Self>
fn from_nbt_tag(tag: BorrowedNbtTag<'_, '_>) -> Option<Self>
Parses an item stack from an NBT tag.
Accepts the vanilla format:
{
id: "minecraft:stone",
count: 64,
components: { ... }
}fn from_optional_nbt_tag( tag: Option<NbtTag<'_, '_>>, ) -> Result<Option<Self>, DeserializeError>
impl StructuralPartialEq for ItemStack
Source§impl ToNbtTag for ItemStack
impl ToNbtTag for ItemStack
Source§fn to_nbt_tag(self) -> NbtTag
fn to_nbt_tag(self) -> NbtTag
Converts this item stack to an NBT tag for persistent storage.
Format (matching vanilla Minecraft):
{
id: "minecraft:stone",
count: 64,
components: { ... } // Only present if patch is non-empty
}