Skip to content

Item components ​

Use stack.getComponents() to inspect what a particular stack can do or what it stores. A pickaxe can inherit its mining rules from the item type, while a potion can carry different effects from another stack of the same item. The component view reads the item's defaults after applying that stack's additions, overrides, and removals.

Read the held stack ​

Hold a damageable item and run this script to see its remaining durability and the maximum damage it can take:

js
const stack = Player.getPlayer()?.getMainHand();
if (stack != null && !stack.isEmpty()) {
  const maxDamage = stack.getComponents().getMaxDamage();
  if (maxDamage !== null) {
    Chat.log(`${stack.getName().getString()}: ${stack.getDurability()} / ${maxDamage} durability`);
  }
}

Typed getters return null when the component is absent. A present component can still contain 0 or an empty list, so check for null explicitly. getComponents().getDamage() reads the stored damage value; stack.getDurability() gives Minecraft's calculation of how much is left.

The component view is read-only and follows the stack object you passed to it. If that stack changes in place, reading through the view again sees the change. If the player switches items or the slot gets a replacement stack, get the stack and its component view again.

Choose a typed getter ​

Typed getters cover the components scripts ask for most often. When a component is not listed here, read it by ID instead; see Look up a component by ID.

To readStart with
Nutrition, use animation and effects, or the leftover itemgetFood(), getConsumable(), getUseRemainder()
Bundle contents and fullness, or items in a shulker boxgetBundleContents(), getContainerContents()
Potion effects and colorgetPotionContents()
Book pages, author and title, or enchantments in an enchanted bookgetWrittenBookContents(), getWritableBookContents(), getStoredEnchantments()
Rocket flight duration and explosions, or a firework star's appearancegetFireworks(), getFireworkExplosion()
Mining rules, attribute modifiers, or configured cooldowngetTool(), getAttributeModifiers(), getUseCooldown()
Equipment rules, trim, dye color, or resource-pack model datagetEquippable(), getArmorTrim(), getDyedColor(), getCustomModelData()
A compass's lodestone target or a filled map's IDgetLodestoneTracker(), getMapId()
Furnace or brewing fuel settings (26.3 and later)getCookingFuel(), getBrewingFuel()

A few readings answer different questions even on the same stack:

  • getFood() describes nutrition. getConsumable() describes how the item is used and which effects are configured by that component. A drink can have a consumable component without providing nutrition, and a potion can carry additional effects in getPotionContents().
  • getStoredEnchantments() reads what the stack stores for transfer. stack.getEnchantments() reads enchantments already applied to the stack.
  • getUseCooldown() gives the configured duration and optional shared group. stack.getCooldownProgress() gives the fraction of the local player's current cooldown remaining, from 1 to 0.

For example, hold a signed book to read it without opening the book screen:

js
const book = Player.getPlayer()?.getMainHand().getComponents().getWrittenBookContents();
if (book != null) {
  Chat.log(`${book.getTitle()} by ${book.getAuthor()}`);
  for (const page of book.getPages()) {
    Chat.log(page);
  }
}

Written pages are TextHelper values, so they retain formatting. page.getString() gives plain text if you need to search it. Writable pages are plain strings. Pass true to getPages(true) or getTitle(true) to prefer the stored filtered version when available.

Look up a component by ID ​

The typed getters are shortcuts for common components, not a limit on what you can read. Every component on the stack is available by ID, including components added by newer Minecraft versions or by other mods. Use getIds() to see which ones are present, then get(id) to read a value. For example, hold an enchanted golden apple to inspect its rarity:

js
const components = Player.getPlayer()?.getMainHand().getComponents();
if (components != null) {
  const rarity = components.get("minecraft:rarity");
  if (rarity !== null) {
    Chat.log(rarity.asString());
  }
}

get(id) returns a detached NBT helper. It returns null if the ID is unknown or invalid, if the component is absent, or if Minecraft cannot encode its value. Use has(id) to check presence separately. For vanilla components, you can omit the minecraft: prefix. getChanges() gives the corresponding view of only the changes saved on this stack.

The same component IDs and value shapes appear in the game's item syntax, so you can build a stack from them:

js
const sword = Client.getRegistryManager().getItem("minecraft:diamond_sword");
const named = sword.getStackWithNbt('[custom_name=\'"Excalibur"\', damage=10]');
Chat.log(`${named.getName()} has ${named.getDurability()} durability left`);

getStackWithNbt() returns a new stack with a count of one, so this builds a stack rather than changing the one in your hand. Client.getRegistryManager() offers the same call as getItemStack(id, nbt) when you already have the item's string ID. Both calls need an active connection, because Minecraft resolves the item and its components through the connected server's registries. You can test component syntax with /give in a creative world and reuse it here.

Reading by ID returns NBT rather than a typed object, and the IDs and value shapes are Minecraft's rather than JsMacrosCE's. That is the trade-off: the ID path reaches components that have no typed getter, but it is more exposed to version and mod changes. Prefer a typed getter when one exists.

Component reference ​

The Minecraft Wiki data component format lists the vanilla component IDs and their value shapes. The item format and /give pages describe the item syntax used above. These pages track the latest Java Edition, so check their version selector or test in the version you play. Mods can add component IDs and change existing values; getIds() reports what is actually present on a stack.

Migrate from getNBT() ​

The deprecated stack.getNBT() serialized only the stack's changes from the item's defaults. For example, a stack could have a maximum damage value even though the old call showed no changes.

If your script wantsUse
The effective value, including the item's defaultA typed getter or stack.getComponents().get(id)
The saved changes from the item's defaultsstack.getComponents().getChanges()
The arbitrary NBT stored in minecraft:custom_datastack.getComponents().getCustomData()

getChanges() returns a detached compound keyed by component ID. A removal uses a key such as !minecraft:damage. An empty patch now returns an empty compound; use changes.getKeys().isEmpty() after checking that changes is non-null. The new call can still return null if a saved change cannot be encoded.