Struct ItemStack
pub struct ItemStack {
pub item: &'static Item,
pub count: i32,
patch: DataComponentPatch,
}Expand description
A stack of items with a count and component modifications.
Fields§
§item: &'static ItemThe item type. AIR represents an empty stack.
count: i32The number of items in this stack.
patch: DataComponentPatchImplementations§
§impl ItemStack
impl ItemStack
pub fn with_count(item: &'static Item, count: i32) -> ItemStack
pub fn with_count(item: &'static Item, count: i32) -> ItemStack
Creates a new item stack with the specified count.
pub fn with_count_and_patch(
item: &'static Item,
count: i32,
patch: DataComponentPatch,
) -> ItemStack
pub fn with_count_and_patch( item: &'static Item, count: i32, patch: DataComponentPatch, ) -> ItemStack
Creates a new item stack with the specified count and component patch.
pub fn is_empty(&self) -> bool
pub fn item(&self) -> &'static Item
pub fn count(&self) -> i32
pub const fn components_patch(&self) -> &DataComponentPatch
pub const fn set_count(&mut self, count: i32)
pub fn split(&mut self, amount: i32) -> ItemStack
pub fn split(&mut self, amount: i32) -> ItemStack
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.
pub fn copy_with_count(&self, count: i32) -> ItemStack
pub fn copy_with_count(&self, count: i32) -> ItemStack
Copies the identity (item type and patch) from another stack.
Used when splitting stacks to preserve components.
pub 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.
pub fn is_damageable_item(&self) -> bool
pub fn is_damageable_item(&self) -> bool
Returns true if this item can take damage.
pub fn is_damaged(&self) -> bool
pub fn is_damaged(&self) -> bool
Returns true if this item has taken damage.
pub fn get_damage_value(&self) -> i32
pub fn get_damage_value(&self) -> i32
Gets the current damage value of this item.
pub fn set_damage_value(&mut self, value: i32)
pub fn set_damage_value(&mut self, value: i32)
Sets the damage value of this item.
pub fn get_max_damage(&self) -> i32
pub fn get_max_damage(&self) -> i32
Gets the maximum damage this item can take before breaking.
pub fn next_damage_will_break(&self) -> bool
pub fn next_damage_will_break(&self) -> bool
Returns vanilla ItemStack.nextDamageWillBreak().
pub 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.
pub fn hurt_and_break_with_random(
&mut self,
amount: i32,
has_infinite_materials: bool,
random: &mut impl Random,
) -> bool
pub fn hurt_and_break_with_random( &mut self, amount: i32, has_infinite_materials: bool, random: &mut impl Random, ) -> bool
Damages the item using the supplied random source for data-driven
minecraft:item_damage enchantment effects.
Returns true if the item broke and should be removed/replaced.
pub fn has<T>(&self, component: DataComponentType<T>) -> boolwhere
T: 'static,
pub fn has<T>(&self, component: DataComponentType<T>) -> boolwhere
T: 'static,
Returns true if this item has the specified component (by type).
pub 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: &ItemStack, b: &ItemStack) -> bool
pub fn is_same_item_same_components(a: &ItemStack, b: &ItemStack) -> bool
pub fn is_same_item_same_components(a: &ItemStack, b: &ItemStack) -> bool
Checks if two stacks have the same item and components.
pub fn matches(a: &ItemStack, b: &ItemStack) -> bool
pub fn is(&self, item: &'static Item) -> bool
pub fn max_stack_size(&self) -> i32
pub fn validate_strict(&self) -> Result<(), Error>
pub fn validate_strict(&self) -> Result<(), Error>
Validates the complete stack constraints enforced by Vanilla’s ItemStack.validateStrict.
pub fn get_equippable(&self) -> Option<&Equippable>
pub fn get_equippable(&self) -> Option<&Equippable>
Returns the equippable component if this item has one.
pub fn get_attribute_modifiers(&self) -> Option<&ItemAttributeModifiers>
pub fn get_attribute_modifiers(&self) -> Option<&ItemAttributeModifiers>
Returns the item attribute modifiers component.
pub fn get_equippable_slot(&self) -> Option<EquipmentSlot>
pub fn get_equippable_slot(&self) -> Option<EquipmentSlot>
Returns the equipment slot this item can be equipped to, if any.
pub fn is_equippable_in_slot(&self, slot: EquipmentSlot) -> bool
pub fn is_equippable_in_slot(&self, slot: EquipmentSlot) -> bool
Returns true if this item can be equipped in the given slot.
pub 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.
pub fn get<T>(&self, component: DataComponentType<T>) -> Option<&T>where
T: Component + DowncastType,
pub fn get<T>(&self, component: DataComponentType<T>) -> Option<&T>where
T: Component + DowncastType,
Gets the effective value of a component, considering the patch and prototype.
Returns None if the component is not present or has been removed.
pub fn get_or_default<T>(
&self,
component: DataComponentType<T>,
default: T,
) -> Twhere
T: Component + DowncastType + Clone,
pub fn get_or_default<T>(
&self,
component: DataComponentType<T>,
default: T,
) -> Twhere
T: Component + DowncastType + Clone,
Gets the effective value of a component, or returns the default value if not present.
pub fn set<T>(&mut self, component: DataComponentType<T>, value: T)where
T: Component + DowncastType,
pub fn set<T>(&mut self, component: DataComponentType<T>, value: T)where
T: Component + DowncastType,
Sets a component value in this item’s patch, overriding the prototype.
pub fn remove<T>(&mut self, component: DataComponentType<T>)where
T: 'static,
pub fn remove<T>(&mut self, component: DataComponentType<T>)where
T: 'static,
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.
pub fn clear<T>(&mut self, component: DataComponentType<T>)where
T: 'static,
pub fn clear<T>(&mut self, component: DataComponentType<T>)where
T: 'static,
Clears any patch entry for this component (neither set nor removed). The prototype value will be visible again.
pub const fn patch(&self) -> &DataComponentPatch
pub const fn patch(&self) -> &DataComponentPatch
Returns a reference to the component patch.
pub fn get_weapon(&self) -> Option<&Weapon>
pub fn get_weapon(&self) -> Option<&Weapon>
Gets the Weapon component if present.
pub fn get_attack_range(&self) -> Option<&AttackRange>
pub fn get_attack_range(&self) -> Option<&AttackRange>
Gets the AttackRange component if present.
pub fn minimum_attack_charge(&self) -> f32
pub fn minimum_attack_charge(&self) -> f32
Returns vanilla DataComponents.MINIMUM_ATTACK_CHARGE, defaulting to 0.
pub fn get_damage_type(&self) -> Option<&'static DamageType>
pub fn get_damage_type(&self) -> Option<&'static DamageType>
Gets the vanilla damage type component if present.
pub fn can_be_hurt_by(&self, damage_type: &'static DamageType) -> bool
pub fn can_be_hurt_by(&self, damage_type: &'static DamageType) -> bool
Returns vanilla ItemStack.canBeHurtBy for a damage type.
pub fn is_valid_repair_item(&self, repair_item: &ItemStack) -> bool
pub fn is_valid_repair_item(&self, repair_item: &ItemStack) -> bool
Returns vanilla ItemStack.isValidRepairItem.
pub fn is_piercing_weapon(&self) -> bool
pub fn is_piercing_weapon(&self) -> bool
Returns whether this item has the vanilla piercing weapon component.
pub fn get_piercing_weapon(&self) -> Option<&PiercingWeapon>
pub fn get_piercing_weapon(&self) -> Option<&PiercingWeapon>
Gets the PiercingWeapon component if present.
pub 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).
pub 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.
pub 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.
pub 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>
pub fn get_enchantments_for_crafting(&self) -> Option<&ItemEnchantments>
pub fn get_enchantments_for_crafting(&self) -> Option<&ItemEnchantments>
Vanilla EnchantmentHelper.getEnchantmentsForCrafting: enchanted books
expose STORED_ENCHANTMENTS to crafting operations, while every other
item exposes ENCHANTMENTS.
pub fn is_enchantable(&self) -> bool
pub fn is_enchantable(&self) -> bool
Mirrors Vanilla’s component-based ItemStack.isEnchantable check.
pub fn has_enchantment_effect( &self, component: EnchantmentEffectComponent, ) -> bool
pub fn apply_unconditional_enchantment_value_effects( &self, component: EnchantmentEffectComponent, input: f32, ) -> f32
pub const fn set_damage_fraction(&mut self, _fraction: f32, _add: bool)
pub const 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.
pub const fn enchant_randomly<R>(
&mut self,
_options: &EnchantmentOptions,
_rng: &mut R,
)where
R: Rng,
pub const fn enchant_randomly<R>(
&mut self,
_options: &EnchantmentOptions,
_rng: &mut R,
)where
R: Rng,
Enchants this item randomly with enchantments from the given options.
pub const fn enchant_with_levels<R>(
&mut self,
_level: i32,
_options: &EnchantmentOptions,
_rng: &mut R,
)where
R: Rng,
pub const fn enchant_with_levels<R>(
&mut self,
_level: i32,
_options: &EnchantmentOptions,
_rng: &mut R,
)where
R: Rng,
Enchants this item as if using an enchanting table at the given level.
pub const fn copy_components<R>(
&mut self,
_source: CopySource,
_include: &[Identifier],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
pub const fn copy_components<R>(
&mut self,
_source: CopySource,
_include: &[Identifier],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
Copies components from a source (block entity, attacker, etc.) to this item.
pub const fn copy_block_state<R>(
&mut self,
_block: &Identifier,
_properties: &[&str],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
pub const fn copy_block_state<R>(
&mut self,
_block: &Identifier,
_properties: &[&str],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
Copies block state properties to this item (for blocks like note_block).
pub const fn set_components_from_json(&mut self, _components: &str)
pub const fn set_components_from_json(&mut self, _components: &str)
Sets components from a JSON string representation.
pub fn set_custom_data(&mut self, value: &CustomData)
pub fn set_custom_data(&mut self, value: &CustomData)
Merges custom NBT data into this item’s custom_data component.
pub fn apply_furnace_smelt(&mut self, use_input_count: bool)
pub fn apply_furnace_smelt(&mut self, use_input_count: bool)
Applies furnace smelting to convert this item (e.g., raw iron -> iron ingot).
pub const fn create_exploration_map(
&mut self,
_destination: &Identifier,
_decoration: &Identifier,
_zoom: i32,
_skip_existing_chunks: bool,
)
pub const fn create_exploration_map( &mut self, _destination: &Identifier, _decoration: &Identifier, _zoom: i32, _skip_existing_chunks: bool, )
Creates an exploration map pointing to a structure.
pub const fn set_name(&mut self, _name: &str, _target: NameTarget)
pub const fn set_name(&mut self, _name: &str, _target: NameTarget)
Sets the custom name or item name of this item.
pub fn set_ominous_bottle_amplifier(&mut self, amplifier: i32)
pub fn set_ominous_bottle_amplifier(&mut self, amplifier: i32)
Sets the ominous bottle amplifier component.
pub const fn set_potion(&mut self, _id: &Identifier)
pub const fn set_potion(&mut self, _id: &Identifier)
Sets the potion type for this item.
pub const fn set_stew_effects<R>(
&mut self,
_effects: &[StewEffect],
_rng: &mut R,
)where
R: Rng,
pub const fn set_stew_effects<R>(
&mut self,
_effects: &[StewEffect],
_rng: &mut R,
)where
R: Rng,
Sets the suspicious stew effects for this item.
pub fn set_enchantments( &mut self, enchantments: &[(Identifier, u32)], add: bool, )
pub 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.
pub fn set_item(&mut self, new_item: &Identifier)
pub fn set_item(&mut self, new_item: &Identifier)
Changes the item type entirely.
pub const fn copy_name<R>(
&mut self,
_source: CopySource,
_ctx: &LootContext<'_, R>,
)where
R: Rng,
pub const fn copy_name<R>(
&mut self,
_source: CopySource,
_ctx: &LootContext<'_, R>,
)where
R: Rng,
Copies the name from a source entity/block to this item.
pub const fn set_contents<R>(
&mut self,
_entries: &[LootEntry],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)where
R: Rng,
pub const fn set_contents<R>(
&mut self,
_entries: &[LootEntry],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)where
R: Rng,
Sets container inventory contents.
pub const fn modify_contents<R>(
&mut self,
_modifier: &[ConditionalLootFunction],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)where
R: Rng,
pub const fn modify_contents<R>(
&mut self,
_modifier: &[ConditionalLootFunction],
_component_type: &Identifier,
_ctx: &mut LootContext<'_, R>,
)where
R: Rng,
Modifies existing container contents.
pub const fn set_loot_table(
&mut self,
_loot_table: &Identifier,
_seed: Option<i64>,
)
pub const fn set_loot_table( &mut self, _loot_table: &Identifier, _seed: Option<i64>, )
Sets the container’s loot table reference.
pub const fn set_attributes<R>(
&mut self,
_modifiers: &[AttributeModifier],
_replace: bool,
_rng: &mut R,
)where
R: Rng,
pub const fn set_attributes<R>(
&mut self,
_modifiers: &[AttributeModifier],
_replace: bool,
_rng: &mut R,
)where
R: Rng,
Sets attribute modifiers on this item.
pub const fn fill_player_head<R>(
&mut self,
_entity: LootContextEntity,
_ctx: &LootContext<'_, R>,
)where
R: Rng,
pub const fn fill_player_head<R>(
&mut self,
_entity: LootContextEntity,
_ctx: &LootContext<'_, R>,
)where
R: Rng,
Fills a player head with texture from an entity.
pub const fn copy_custom_data<R>(
&mut self,
_source: CopySource,
_operations: &[CopyDataOperation],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
pub const fn copy_custom_data<R>(
&mut self,
_source: CopySource,
_operations: &[CopyDataOperation],
_ctx: &LootContext<'_, R>,
)where
R: Rng,
Copies custom NBT data from a source.
Sets banner pattern layers.
pub const fn set_fireworks(
&mut self,
_explosions: Option<&[FireworkExplosion]>,
_flight_duration: Option<i32>,
)
pub const fn set_fireworks( &mut self, _explosions: Option<&[FireworkExplosion]>, _flight_duration: Option<i32>, )
Sets firework rocket properties.
pub const fn set_firework_explosion(&mut self, _explosion: &FireworkExplosion)
pub const fn set_firework_explosion(&mut self, _explosion: &FireworkExplosion)
Sets firework star explosion properties.
pub const fn set_book_cover(
&mut self,
_title: Option<&str>,
_author: Option<&str>,
_generation: Option<i32>,
)
pub const fn set_book_cover( &mut self, _title: Option<&str>, _author: Option<&str>, _generation: Option<i32>, )
Sets book cover (title/author for written books).
pub const fn set_written_book_pages(
&mut self,
_pages: &[&str],
_mode: ListOperation,
)
pub const fn set_written_book_pages( &mut self, _pages: &[&str], _mode: ListOperation, )
Sets written book page contents.
pub const fn set_writable_book_pages(
&mut self,
_pages: &[&str],
_mode: ListOperation,
)
pub const fn set_writable_book_pages( &mut self, _pages: &[&str], _mode: ListOperation, )
Sets writable book page contents.
pub fn toggle_tooltips(&mut self, toggles: &[(Identifier, bool)])
pub fn toggle_tooltips(&mut self, toggles: &[(Identifier, bool)])
Runs vanilla ToggleTooltips: each boolean says whether the component is shown.
pub fn components_equal(&self, other: &ItemStack) -> bool
pub fn custom_name(&self) -> Option<Cow<'_, TextComponent>>
pub fn custom_name(&self) -> Option<Cow<'_, TextComponent>>
Vanilla ItemStack.getCustomName: an explicit custom name, or a
nonblank written-book title.
pub fn custom_or_component_name(&self) -> Cow<'_, TextComponent>
pub fn custom_or_component_name(&self) -> Cow<'_, TextComponent>
Returns the custom name if set, otherwise the effective ITEM_NAME
component.
This does not apply item-class name overrides such as potion contents; callers with access to item behaviors should use their behavior-aware hover-name API.
§impl ItemStack
impl ItemStack
pub fn validate_persistent_encoding(&self) -> Result<(), Error>
pub fn validate_persistent_encoding(&self) -> Result<(), Error>
Checks that this stack can be encoded by Vanilla’s persistent
ItemStack.CODEC before untrusted network data enters server state.
This is an ingress check rather than a type invariant: programmatic component mutation can still create values whose save codec reports and omits invalid fields.
pub 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.
§impl ItemStack
impl ItemStack
pub fn from_borrowed_compound(
compound: &NbtCompound<'_, '_>,
) -> Option<ItemStack>
pub fn from_borrowed_compound( compound: &NbtCompound<'_, '_>, ) -> Option<ItemStack>
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§
§impl FromNbtTag for ItemStack
impl FromNbtTag for ItemStack
§fn from_nbt_tag(tag: NbtTag<'_, '_>) -> Option<ItemStack>
fn from_nbt_tag(tag: NbtTag<'_, '_>) -> Option<ItemStack>
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>
Source§impl ItemInstanceView for ItemStack
impl ItemInstanceView for ItemStack
fn item_ref(&self) -> ItemRef
fn item_count(&self) -> i32
fn effective_value_raw(&self, key: &Identifier) -> Option<&ComponentData>
fn component<T: Component + DowncastType>( &self, component: DataComponentType<T>, ) -> Option<&T>
impl StructuralPartialEq for ItemStack
§impl ToNbtTag for ItemStack
impl ToNbtTag for ItemStack
§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
}fn to_optional_nbt_tag(self) -> Option<NbtTag>
Auto Trait Implementations§
impl !RefUnwindSafe for ItemStack
impl !UnwindSafe for ItemStack
impl Freeze for ItemStack
impl Send for ItemStack
impl Sync for ItemStack
impl Unpin for ItemStack
impl UnsafeUnpin for ItemStack
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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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 moreArc<SyncMutex<>>