Skip to content

ItemStack

Namespace Mc.Scripting.Inventories · Kind struct (readonly)

public readonly struct ItemStack : IEquatable<ItemStack>

An immutable item stack value: an item type, a count and data components. Every With... method returns a new stack; the value can be kept, compared and put into inventories, menus and the world (IInventory.Set, {{cref:M:Mc.Scripting.Inventories.Menu.Slot(System.Int32,Mc.Scripting.Inventories.ItemStack,System.Action{Mc.Scripting.Inventories.MenuClick})}}, World.DropItem).

Components are addressed by their registry key ("minecraft:custom_name") and carry NBT values. The ones the server converts are given in a plugin friendly form: text components (custom_name, item_name, lore lines) as JSON strings (what Component.ToJson makes), integers (damage, max_damage, max_stack_size, repair_cost, map_id, enchantable, dyed_color), rarity as "common" .. "epic", enchantment_glint_override as a byte, unbreakable / glider as an empty compound, custom_data as a compound, item_model and tooltip_style as identifier strings, custom_model_data as {floats, flags, strings, colors}, enchantments and stored_enchantments as {"minecraft:sharpness": 3}, tooltip_display as {hide_tooltip, hidden_components}, use_cooldown as {seconds, group?} and food as {nutrition, saturation, can_always_eat}. Every other component the server knows is read as an opaque byte array holding its network encoding: it survives copying a stack but cannot be edited. A component key starting with ! removes a component of the item’s prototype (ItemStack.WithoutDefault).

A stack that no inventory accepts (unknown item, a count above the stack size, a component whose value does not convert) is refused by the call that receives it with an ArgumentException.

Member Description
ItemStack(string type, int count = 1) Creates a stack.
public ItemStack(string type, int count = 1)

Creates a stack.

Parameters

Name Type Description
type string The item key, "minecraft:diamond_sword" (the namespace may be left out).
count int = 1 The stack size (at least 1).
Member Description
IEnumerable<string> ComponentKeys { get; } The keys of the components added to the stack (and !key entries for removed defaults).
int Count { get; } The stack size (0 for an empty stack).
NbtCompound? CustomData { get; } The plugin data of the stack (minecraft:custom_data), or null.
string? CustomId { get; } The id of the custom item this stack is (custom_data.mc.id, written by the content layer), or null for an ordinary stack.
bool IsEmpty { get; } Whether the stack holds nothing.
int MaxStackSize { get; } The largest size a stack like this can have (the item’s own, or its max_stack_size component).
string Type { get; } The item key ("minecraft:air" for an empty stack).
static ItemStack Empty { get; } No item.
public IEnumerable<string> ComponentKeys { get; }

The keys of the components added to the stack (and !key entries for removed defaults).

public int Count { get; }

The stack size (0 for an empty stack).

public NbtCompound? CustomData { get; }

The plugin data of the stack (minecraft:custom_data), or null.

public string? CustomId { get; }

The id of the custom item this stack is (custom_data.mc.id, written by the content layer), or null for an ordinary stack.

public bool IsEmpty { get; }

Whether the stack holds nothing.

public int MaxStackSize { get; }

The largest size a stack like this can have (the item’s own, or its max_stack_size component). Asks the server.

public string Type { get; }

The item key ("minecraft:air" for an empty stack).

public static ItemStack Empty { get; }

No item.

Member Description
bool Equals(ItemStack other)
override bool Equals(object? obj)
NbtTag? GetComponent(string key) Reads a data component.
override int GetHashCode()
bool HasComponent(string key) Whether the stack carries a component that was added to it.
NbtCompound ToNbt() The stack in its NBT form ({id:"minecraft:stick",count:2,components:{...}}), the form the server exchanges.
override string ToString()
ItemStack WithComponent(string key, NbtTag value) The same stack with a data component set.
ItemStack WithCount(int count) The same stack with another size.
ItemStack WithCustomData(NbtCompound data) The same stack with plugin data (minecraft:custom_data) replaced.
ItemStack WithCustomId(string customId) The same stack marked as a custom item (custom_data.mc.id); other plugin data is kept.
ItemStack WithCustomModelData(float value) The same stack with one float of minecraft:custom_model_data (what range_dispatch model definitions of a resource pack read).
ItemStack WithDamage(int damage) The same stack with durability damage (minecraft:damage).
ItemStack WithEnchantment(string enchantment, int level) The same stack with an enchantment level (minecraft:enchantments).
ItemStack WithItemModel(string modelId) The same stack with another model (minecraft:item_model): the id of an item definition of a resource pack (assets/<namespace>/items/<path>.json).
ItemStack WithItemName(Component name) The same stack with an item name (minecraft:item_name: the base name of the item, not italic, replaced by a rename in an anvil).
ItemStack WithLore(params Component[] lines) The same stack with lore lines (minecraft:lore).
ItemStack WithMaxStackSize(int max) The same stack with another maximum stack size (minecraft:max_stack_size, 1 to 99).
ItemStack WithName(Component name) The same stack with a custom name (minecraft:custom_name: shown in italics like a renamed item).
ItemStack WithUnbreakable() The same stack that cannot take durability damage (minecraft:unbreakable).
ItemStack WithoutComponent(string key) The same stack without a component that was added to it (the item’s own default of it, if it has one, stays).
ItemStack WithoutDefault(string key) The same stack without the default value of a component of its item (minecraft:max_damage of a tool, for example).
static ItemStack FromNbt(NbtCompound nbt) Reads a stack from its NBT form.
static bool IsSimilar(ItemStack a, ItemStack b) Whether two stacks are the same item with the same components, whatever their sizes (the test of merging stacks).
public bool Equals(ItemStack other)

Parameters

Name Type Description
other ItemStack

Returns bool

public override bool Equals(object? obj)

Parameters

Name Type Description
obj object?

Returns bool

public NbtTag? GetComponent(string key)

Reads a data component.

Parameters

Name Type Description
key string The component key.

Returns NbtTag?: A copy of the component, or null when the stack does not have it as an added component.

public override int GetHashCode()

Returns int

public bool HasComponent(string key)

Whether the stack carries a component that was added to it.

Parameters

Name Type Description
key string The component key.

Returns bool: True when it does.

public NbtCompound ToNbt()

The stack in its NBT form ({id:"minecraft:stick",count:2,components:{...}}), the form the server exchanges.

Returns NbtCompound: The compound.

public override string ToString()

Returns string

public ItemStack WithComponent(string key, NbtTag value)

The same stack with a data component set.

Parameters

Name Type Description
key string The component key, "minecraft:custom_data".
value NbtTag The component value in its NBT form.

Returns ItemStack: The new stack.

public ItemStack WithCount(int count)

The same stack with another size.

Parameters

Name Type Description
count int The new size (0 gives the empty stack).

Returns ItemStack: The new stack.

public ItemStack WithCustomData(NbtCompound data)

The same stack with plugin data (minecraft:custom_data) replaced.

Parameters

Name Type Description
data NbtCompound The data.

Returns ItemStack: The new stack.

public ItemStack WithCustomId(string customId)

The same stack marked as a custom item (custom_data.mc.id); other plugin data is kept.

Parameters

Name Type Description
customId string The id, "myplugin:ruby".

Returns ItemStack: The new stack.

public ItemStack WithCustomModelData(float value)

The same stack with one float of minecraft:custom_model_data (what range_dispatch model definitions of a resource pack read).

Parameters

Name Type Description
value float The value.

Returns ItemStack: The new stack.

public ItemStack WithDamage(int damage)

The same stack with durability damage (minecraft:damage).

Parameters

Name Type Description
damage int The damage.

Returns ItemStack: The new stack.

public ItemStack WithEnchantment(string enchantment, int level)

The same stack with an enchantment level (minecraft:enchantments).

Parameters

Name Type Description
enchantment string The enchantment key, "minecraft:sharpness".
level int The level.

Returns ItemStack: The new stack.

public ItemStack WithItemModel(string modelId)

The same stack with another model (minecraft:item_model): the id of an item definition of a resource pack (assets/<namespace>/items/<path>.json).

Parameters

Name Type Description
modelId string The model id, "myplugin:ruby".

Returns ItemStack: The new stack.

public ItemStack WithItemName(Component name)

The same stack with an item name (minecraft:item_name: the base name of the item, not italic, replaced by a rename in an anvil).

Parameters

Name Type Description
name Component The name.

Returns ItemStack: The new stack.

public ItemStack WithLore(params Component[] lines)

The same stack with lore lines (minecraft:lore).

Parameters

Name Type Description
lines params Component[] The lines.

Returns ItemStack: The new stack.

public ItemStack WithMaxStackSize(int max)

The same stack with another maximum stack size (minecraft:max_stack_size, 1 to 99).

Parameters

Name Type Description
max int The size.

Returns ItemStack: The new stack.

public ItemStack WithName(Component name)

The same stack with a custom name (minecraft:custom_name: shown in italics like a renamed item).

Parameters

Name Type Description
name Component The name.

Returns ItemStack: The new stack.

public ItemStack WithUnbreakable()

The same stack that cannot take durability damage (minecraft:unbreakable).

Returns ItemStack: The new stack.

public ItemStack WithoutComponent(string key)

The same stack without a component that was added to it (the item’s own default of it, if it has one, stays).

Parameters

Name Type Description
key string The component key.

Returns ItemStack: The new stack.

public ItemStack WithoutDefault(string key)

The same stack without the default value of a component of its item (minecraft:max_damage of a tool, for example).

Parameters

Name Type Description
key string The component key.

Returns ItemStack: The new stack.

public static ItemStack FromNbt(NbtCompound nbt)

Reads a stack from its NBT form.

Parameters

Name Type Description
nbt NbtCompound The compound with id, count and optionally components.

Returns ItemStack: The stack.

Throws

  • System.FormatException: The compound has no item id.
public static bool IsSimilar(ItemStack a, ItemStack b)

Whether two stacks are the same item with the same components, whatever their sizes (the test of merging stacks).

Parameters

Name Type Description
a ItemStack One stack.
b ItemStack The other.

Returns bool: True when they would stack.

Member Description
static bool operator !=(ItemStack a, ItemStack b) Inequality.
static bool operator ==(ItemStack a, ItemStack b) Equality.
public static bool operator !=(ItemStack a, ItemStack b)

Inequality.

Parameters

Name Type Description
a ItemStack One stack.
b ItemStack The other.

Returns bool: True when different.

public static bool operator ==(ItemStack a, ItemStack b)

Equality.

Parameters

Name Type Description
a ItemStack One stack.
b ItemStack The other.

Returns bool: True when equal.

Component · NbtCompound · NbtTag