Skip to content

ItemStackHelper ​

com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackHelper

A script-facing view of an item stack, including its count and components. Use RegistryHelper.getItemStack(String) to obtain one from the registry, or get one from an entity with LivingEntityHelper.getMainHand().
getCreative() exposes setters that modify this same stack; use copy() first to preserve the original.

js
// a stack, and the things worth knowing about it
const stack = Client.getRegistryManager().getItemStack("minecraft:diamond_sword", `[{id:"minecraft:sharpness",lvl:5}]`);  
Chat.log(`${stack.getCount()} of ${stack.getName().getString()}`);  
Chat.log(`${stack.getItemId()}, ${stack.getDurability()} of ${stack.getMaxDurability()} durability`);  

// the enchantments on it, with their levels
for (const ench of stack.getEnchantments()) {
 Chat.log(ench.getRomanLevelName().getString());  
}

// and the two ways of asking whether two stacks match
const fresh = Client.getRegistryManager().getItemStack("minecraft:diamond_sword");  
Chat.log(`same item: ${stack.isItemEqual(fresh)}`);  
Chat.log(`same everything: ${stack.isNBTEqual(fresh)}`);

Constructors ​

new ItemStackHelper ​

new ItemStackHelper(String id, int count)#
Wraps a newly constructed stack for the item registry entry identified by id.

Parameters:

  • id: item identifier; if it has no namespace, minecraft is used
  • count: requested stack count passed to the native stack constructor
new ItemStackHelper(ItemStack i)#
Wraps the supplied stack reference without copying it.

Parameters:

  • i: stack to wrap

Methods ​

areEnchantmentsHidden ​

areEnchantmentsHidden(): booleanSince1.8.4#
Returns whether the enchantments component is marked hidden in this stack's tooltip display.
Returns: true if enchantments are hidden, false otherwise.

areModifiersHidden ​

areModifiersHidden(): booleanSince1.8.4#
Returns whether the attribute modifiers component is marked hidden in this stack's tooltip display.
Returns: true if modifiers are hidden, false otherwise.

canBeApplied ​

canBeApplied(EnchantmentHelper enchantment): booleanSince1.8.4#
Whether this enchantment supports this stack's item type and is compatible with all enchantments already on it. Unlike EnchantmentHelper.canBeApplied(ItemHelper), this checks the stack's current enchantments too.
// whether an enchantment still fits on a stack that already has things on it
const reg = Client.getRegistryManager();
const stack = reg.getItemStack("minecraft:diamond_sword", `[{id:"minecraft:sharpness",lvl:5}]`);
const unbreaking = reg.getEnchantment("minecraft:unbreaking");
const looting = reg.getEnchantment("minecraft:looting");
Chat.log(`unbreaking fits: ${unbreaking.canBeApplied(stack)}`);
Chat.log(`looting fits: ${looting.canBeApplied(stack)}`);

Parameters:

  • enchantment: the enchantment to check
Returns: true if the specified enchantment can be applied to this item, false otherwise.

copy ​

copy(): ItemStackHelperSince1.2.0#
Returns an independent copy of this stack, including its count and components.
Returns: a copy of this stack, with everything on it.

equals ​

equals(ItemStackHelper ish): booleanSince1.2.0#
Compares item and components with another helper, ignoring count.

Parameters:

  • ish: the other stack to compare against.
Returns: true if the two stacks have the same item and components, ignoring count
equals(ItemStack is): booleanSince1.2.0#
Compares item and components with a raw stack, ignoring count. A null raw stack causes a null-pointer exception.

Parameters:

  • is: the raw stack to compare against.
Returns: true if the two stacks have the same item and components, ignoring count
equals(Object obj): boolean#
Compares wrapped values when obj is a BaseHelper; otherwise compares the wrapped value directly with obj. This delegates to the stored value's equals and throws NullPointerException if that value is null.

Parameters:

  • obj: object to compare
Returns: whether the delegated comparison reports equality

getAttackDamage ​

getAttackDamage(): doubleSince1.8.4#
Computes this stack's main-hand attack damage using its attribute modifiers and the local player's base attack-damage attribute value. Requires a local player.
Returns: the computed main-hand attack damage.

getCooldownProgress ​

getCooldownProgress(): floatSince1.6.5#
Returns this stack's cooldown progress: 1 at the start, decreasing to 0 at expiry; it is 0 when no cooldown exists. The result uses the current partial tick. Cooldowns are keyed by group, normally item identity but possibly overridden by the stack's use-cooldown component.
Returns: the remaining cooldown fraction, from zero to one

getCount ​

getCount(): int#
Returns the number of items in this stack. It can exceed getMaxCount() when set through CreativeItemStackHelper.setCount(int).
Returns: this stack's item count.

getCreative ​

getCreative(): CreativeItemStackHelperSince1.8.4#
Returns a mutable helper around the same stack, not a copy. Its setters mutate this stack; call copy() first to preserve the original. This is a client-side stack operation, not a direct inventory or server update.
Returns: a CreativeItemStackHelper instance for this item.

getCreativeTab ​

getCreativeTab(): List<TextHelper>Since1.1.3#
Returns the display names of creative tabs containing an item equal to this stack. Tabs aligned to the right are excluded; an item in no included tab produces an empty list.
Returns: included creative-tab display names.

getDamage ​

getDamage(): intSince1.8.4#
The damage value stored on this stack.
Returns: the damage taken by this item.

getDefaultName ​

getDefaultName(): TextHelperSince1.2.0#
Returns the item's name, regardless of any custom name set on this stack.
Returns: the item's default name.

getDestroyRestrictions ​

getDestroyRestrictions(): List<BlockPredicateHelper>Since1.8.4#
Returns the block predicates in this stack's can-destroy restriction, or an empty list if the component is absent. Predicates can include block, state, and block-entity NBT criteria.
Returns: a list of all filters set for the can destroy flag.

getDurability ​

getDurability(): intSince1.8.4#
The stack's effective maximum damage value minus the damage it has taken.
Returns: the current durability of this item.

getEnchantment ​

getEnchantment(String id): EnchantmentHelperSince1.8.4#
Returns the matching enchantment and its level, or null if the stack does not carry it. Accepts a namespaced or unnamespaced id, or the enchantment's translated name.

Parameters:

  • id: the id of the enchantment to check for
Returns: the enchantment instance, containing the level, or null if the item is not enchanted with the specified enchantment.

getEnchantments ​

getEnchantments(): List<EnchantmentHelper>Since1.8.4#
Returns each enchantment on this stack with its level. No enchantments produces an empty list; each call creates a new list.
Returns: a list of all enchantments on this item.

getItem ​

getItem(): ItemHelperSince1.8.4#
Returns a helper for the item type, without this stack's count or components.
Returns: the item this stack is made of.

getItemId ​

getItemId(): StringSince1.6.4#
Returns this item's full registry id.
Returns: the item's full registry id.

getLore ​

getLore(): List<TextHelper>Since1.8.4#
Returns this stack's lore lines wrapped as text. An absent lore component produces an empty list; changing the returned list does not change the stack. Use getCreative() to edit the stack's lore.
Returns: a list of all lines of lore on this item.

getMaxCount ​

getMaxCount(): int#
Returns the maximum stack size for this stack's item; the current count may exceed it.
Returns: the item's maximum stack size.

getMaxDamage ​

getMaxDamage(): intSince1.8.4#
The maximum damage value for this stack; unlike getMaxDurability(), this comes from the stack's item.
Returns: the maximum damage value for the stack's item.

getMaxDurability ​

getMaxDurability(): intSince1.8.4#
The stack's effective maximum damage value. getDamage() is what this stack has already taken, and getDurability() is what is left of this number.
// the item's limit against what this stack has used
const stack = Client.getRegistryManager().getItemStack("minecraft:diamond_pickaxe").getCreative();
stack.setDamage(1000);
Chat.log(`${stack.getDamage()} of ${stack.getMaxDurability()}, ${stack.getDurability()} left`);

// an item that cannot be damaged gives zero
Chat.log(Client.getRegistryManager().getItemStack("minecraft:stone").getMaxDurability());
Returns: the maximum durability of this item.

getName ​

getName(): TextHelperSince1.2.0#
Returns the name shown for this stack, including any custom name.
Returns: the stack's displayed name.

getNBT ​

getNBT(): NBTElementHelper<?>Since1.1.6#
Returns this stack's component changes encoded as an NBT compound, or null when the component patch is empty. The returned data uses Minecraft's component names.
Returns: the components on this stack, or null if it has none.

Note: was a String until 1.5.1

getPlaceRestrictions ​

getPlaceRestrictions(): List<BlockPredicateHelper>Since1.8.4#
Returns the block predicates in this stack's can-place-on restriction, or an empty list if the component is absent. Predicates can include block, state, and block-entity NBT criteria.
Returns: a list of all filters set for the can place on flag.

getPossibleEnchantments ​

getPossibleEnchantments(): List<EnchantmentHelper>Since1.8.4#
Enchantments in the registry whose item-type criteria accept this stack. Existing enchantments and their compatibility are not considered.
Returns: a list of all enchantments that can be applied to this item.

getPossibleEnchantmentsFromTable ​

getPossibleEnchantmentsFromTable(): List<EnchantmentHelper>Since1.8.4#
The subset of getPossibleEnchantments() whose enchantments are in the game's IN_ENCHANTING_TABLE tag and can be applied to this stack.
// what an enchanting table would offer for this item
const stack = Client.getRegistryManager().getItemStack("minecraft:diamond_sword");
const fromTable = stack.getPossibleEnchantmentsFromTable();
const all = stack.getPossibleEnchantments();
Chat.log(`${fromTable.size()} from a table out of ${all.size()} in total`);
for (const ench of fromTable) {
  Chat.log(`  ${ench.getId()}, treasure ${ench.isTreasure()}, cursed ${ench.isCursed()}`);
}
Returns: a list of all enchantments that can be applied to this item through an enchanting table.

getRepairCost ​

getRepairCost(): intSince1.8.4#
The stack's repair-cost component, or 0 if it is absent. This is not necessarily the experience-level cost of a particular repair operation.
Returns: the current repair cost of this item.

getTags ​

getTags(): List<String>Since1.8.2#
Returns the full registry ids of the tags containing this stack's item.
Returns: a list of the item tags this item is in.

hasDestroyRestrictions ​

hasDestroyRestrictions(): booleanSince1.8.4#
Returns whether this stack has a can-destroy restriction component.
Returns: true if the can destroy flag is set, false otherwise.

hasEnchantment ​

hasEnchantment(EnchantmentHelper enchantment): booleanSince1.8.4#
Whether this stack carries the given enchantment. A helper without a level matches any level; two helpers with levels match only when their levels are equal.
// the same enchantment, asked about at a level and without one
const reg = Client.getRegistryManager();
const stack = reg.getItemStack("minecraft:diamond_sword", `[{id:"minecraft:sharpness",lvl:5}]`);
const anyLevel = reg.getEnchantment("minecraft:sharpness");
const levelFive = reg.getEnchantment("minecraft:sharpness", 5);
const levelThree = reg.getEnchantment("minecraft:sharpness", 3);
Chat.log(`any level: ${stack.hasEnchantment(anyLevel)}`);
Chat.log(`level five: ${stack.hasEnchantment(levelFive)}`);
Chat.log(`level three: ${stack.hasEnchantment(levelThree)}`);

Parameters:

  • enchantment: the enchantment to check for
Returns: true if the item is enchanted with the specified enchantment, false otherwise.
hasEnchantment(String enchantment): booleanSince1.8.4#
Whether this stack carries the enchantment with the given id, regardless of its level.

Parameters:

  • enchantment: the id of the enchantment to check for
Returns: true if the item is enchanted with the given enchantment, false otherwise.

hasPlaceRestrictions ​

hasPlaceRestrictions(): booleanSince1.8.4#
Returns whether this stack has a can-place-on restriction component.
Returns: true if the can place on flag is set, false otherwise.

isCanDestroyHidden ​

isCanDestroyHidden(): booleanSince1.8.4#
Returns whether the can-destroy component is marked hidden in this stack's tooltip display, independently of whether that restriction component is present.
Returns: true if the can destroy flag is hidden, false otherwise.

isCanPlaceHidden ​

isCanPlaceHidden(): booleanSince1.8.4#
Returns whether the can-place-on component is marked hidden in this stack's tooltip display.
Returns: true if the component is marked hidden, or false if it is not or no tooltip display is set.

isDamageable ​

isDamageable(): booleanSince1.2.0#
Whether this stack can take damage. Returns false if it is unbreakable or lacks any of the max-damage and damage components.
Returns: true if this stack can take damage, false otherwise.

isDyeHidden ​

isDyeHidden(): booleanSince1.8.4#
Returns whether the dyed-color component is marked hidden in this stack's tooltip display.
Returns: true if the component is marked hidden, or false if it is not or no tooltip display is set.

isEmpty ​

isEmpty(): boolean#
Returns whether this stack has no contents. Air stacks and stacks with a non-positive count are empty.
Returns: true if this stack holds nothing, false otherwise.

isEnchantable ​

isEnchantable(): booleanSince1.2.0#
Whether this stack has the enchantable component and has no enchantments yet.
Returns: true if this item can be enchanted, false otherwise.

isEnchanted ​

isEnchanted(): booleanSince1.8.4#
Whether this stack has any enchantments, regardless of whether its item is enchantable.
Returns: true if the item is enchanted, false otherwise.

isFood ​

isFood(): booleanSince1.8.2#
Returns whether this stack has a food component.
Returns: true if this item can be eaten, false otherwise.

isItemEqual ​

isItemEqual(ItemStackHelper ish): booleanSince1.2.0#
Compares item identity and damage with another helper; other components are ignored.

Parameters:

  • ish: the other stack to compare against.
Returns: true if the two stacks are the same item at the same damage.
isItemEqual(ItemStack is): booleanSince1.2.0#
Compares item identity and damage with a raw stack; other components are ignored.

Parameters:

  • is: the raw stack to compare against.
Returns: true if the two stacks are the same item at the same damage.

isItemEqualIgnoreDamage ​

isItemEqualIgnoreDamage(ItemStackHelper ish): booleanSince1.2.0#
Compares item identity with another helper, ignoring damage and all components.

Parameters:

  • ish: the other stack to compare against.
Returns: true if the two stacks are the same item, whatever else differs.
isItemEqualIgnoreDamage(ItemStack is): booleanSince1.2.0#
Compares item identity with a raw stack, ignoring damage and all components.

Parameters:

  • is: the raw stack to compare against.
Returns: true if the two stacks are the same item, whatever else differs.

isNBTEqual ​

isNBTEqual(ItemStackHelper ish): booleanSince1.2.0#
Compares component maps with another helper; item identity and count are not checked separately, and the maps include item component defaults.
// count is ignored, but damage is part of the component state
const reg = Client.getRegistryManager();
const sword = reg.getItemStack("minecraft:diamond_sword", `[{id:"minecraft:sharpness",lvl:5}]`);
const sameItem = reg.getItemStack("minecraft:diamond_sword", `[{id:"minecraft:sharpness",lvl:5}]`)
  .getCreative().setCount(2);
Chat.log(`same components: ${sword.isNBTEqual(sameItem)}`);
const worn = sword.copy().getCreative().setDamage(1);
Chat.log(`damage differs: ${sword.isNBTEqual(worn)}`);

// and against a stack with something else on it
const named = sword.getCreative().setName("Excalibur");
Chat.log(`after naming: ${sword.isNBTEqual(named)}`);

Parameters:

  • ish: the other stack to compare against.
Returns: true if the component maps are equal.
isNBTEqual(ItemStack is): booleanSince1.2.0#
Compares component maps with a raw stack; item identity and count are not checked separately, and the maps include item component defaults.

Parameters:

  • is: the raw stack to compare against.
Returns: true if the component maps are equal.

isOnCooldown ​

isOnCooldown(): booleanSince1.6.5#
Checks whether the local player's cooldown manager has a cooldown for this stack's cooldown group.
Returns: true if the item is on cooldown, false otherwise.

isSuitableFor ​

isSuitableFor(BlockHelper block): booleanSince1.8.4#
Checks whether this stack's tool component is correct for the block's default state.

Parameters:

  • block: the block to check
Returns: true if the given block can be mined and drops when broken with this item, false otherwise.
isSuitableFor(BlockStateHelper block): booleanSince1.8.4#
Checks whether this stack's tool component is correct for the given block state.

Parameters:

  • block: the block to check
Returns: true if the given block can be mined and drops when broken with this item, false otherwise.

isTool ​

isTool(): booleanSince1.8.2#
Returns whether this stack has a tool component.
Returns: true if this item is a tool, false otherwise.

isUnbreakable ​

isUnbreakable(): booleanSince1.8.4#
Whether this stack has the unbreakable component. This is distinct from isUnbreakableHidden(), which reports tooltip visibility.
Returns: true if this item is unbreakable, false otherwise.

isUnbreakableHidden ​

isUnbreakableHidden(): booleanSince1.8.4#
Returns whether the unbreakable component is marked hidden in this stack's tooltip display.
Returns: true if the unbreakable flag is hidden, false otherwise.

isWearable ​

isWearable(): booleanSince1.8.2#
Returns whether this stack has an equippable component.
Returns: true if this item can be worn, false otherwise.

toString ​

toString(): String#
Returns a diagnostic string containing this stack's item id, damage value, and count.
Returns: a formatted diagnostic representation of the stack