# ItemStackComponentsHelper

`com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper`

**Since:** 2.3.0

Reads the current components of an item stack. Typed getters and ID lookup include the item's
defaults, with any changes or removals made to this stack. Use [getIds()](#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getIds-) and
[get(String)](#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_get-String-) to read components that have no typed getter, and
[ItemHelper.getStackWithNbt(String)](ItemHelper.md#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemHelper_getStackWithNbt-String-) to build a new stack from the same component syntax.  
Use [getChanges()](#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getChanges-) to read only the saved changes. This view follows the stack passed to
it; it does not follow a slot when the slot gets a different stack.  
```js
const components = Player.getPlayer()?.getMainHand().getComponents();  
if (components != null) {
  const maxDamage = components.getMaxDamage();  
  if (maxDamage !== null) Chat.log(`Maximum damage: ${maxDamage}`);  
  const effects = components.getPotionContents()?.getEffects();  
  if (effects != null) {
    for (const effect of effects) Chat.log(effect.getId());  
  }
}
```

## Methods

### get {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_get-String-}

```js
get(String id): NBTElementHelper<?>
```

Reads one component as a detached NBT tag. Prefer a typed getter when one is available.  
Use [has(String)](#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_has-String-) to distinguish a missing component from one that cannot be encoded.  
Without a connection, only built-in registries are available for encoding.

**Parameters:**
- `id`: component ID; the `minecraft:` namespace can be omitted

**Returns:** the encoded value, or `null` if absent, unknown, invalid, or not encodable

### getArmorTrim {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getArmorTrim-}

```js
getArmorTrim(): ArmorTrimHelper
```

Reads the material and pattern of the applied armor trim.

**Returns:** the trim, or `null` if the stack is untrimmed

### getAttributeModifiers {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getAttributeModifiers-}

```js
getAttributeModifiers(): List<ItemAttributeModifierHelper>
```

Lists the stack's configured attribute modifiers and the equipment slots they apply to.

**Returns:** the modifier entries, or `null` if the component is absent

### getBundleContents {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getBundleContents-}

```js
getBundleContents(): BundleContentsHelper
```

Reads the items, fullness, and selection of a bundle.

**Returns:** the bundle contents, or `null` if the component is absent

### getChanges {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getChanges-}

```js
getChanges(): NBTElementHelper.NBTCompoundHelper
```

Reads the stack's saved changes as a detached NBT compound. Added and replaced values use
their component IDs as keys. Removals have keys such as `"!minecraft:damage"`.  
Components that Minecraft does not save are omitted.

**Returns:** the saved changes, an empty compound when there are none, or `null` if a
        saved change cannot be encoded

### getConsumable {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getConsumable-}

```js
getConsumable(): ConsumableComponentHelper
```

Reads how this stack is consumed, including its use duration, animation, and configured effects.

**Returns:** the consumable component, or `null` if it is absent

### getContainerContents {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getContainerContents-}

```js
getContainerContents(): List<ItemStackHelper>
```

Reads items stored in a stack such as a shulker box. List indices are zero-based slots;  
gaps appear as empty stacks, and trailing empty slots may be omitted.

**Returns:** copies in slot order, or `null` if the component is absent

### getCustomData {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getCustomData-}

```js
getCustomData(): NBTElementHelper.NBTCompoundHelper
```

Reads arbitrary NBT attached through the `minecraft:custom_data` component.

**Returns:** a copy of the custom data, or `null` if the component is absent

### getCustomModelData {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getCustomModelData-}

```js
getCustomModelData(): CustomModelDataHelper
```

Reads custom model data that resource packs can use to change the item's appearance.

**Returns:** the custom model data, or `null` if the component is absent

### getDamage {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getDamage-}

```js
getDamage(): Integer
```

Reads the damage component before Minecraft clamps it to the stack's maximum damage.

**Returns:** damage taken, or `null` if the component is absent

### getDyedColor {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getDyedColor-}

```js
getDyedColor(): Integer
```

Reads the dye color applied to a stack, for example dyed leather armor.

**Returns:** the 24-bit RGB color, or `null` if the component is absent

### getEquippable {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getEquippable-}

```js
getEquippable(): EquippableComponentHelper
```

Reads the component that specifies how the stack can be equipped.

**Returns:** the equippable component, or `null` if it is absent

### getFireworkExplosion {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getFireworkExplosion-}

```js
getFireworkExplosion(): FireworkExplosionHelper
```

Reads the explosion stored on a firework star.

**Returns:** the explosion, or `null` if the component is absent

### getFireworks {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getFireworks-}

```js
getFireworks(): FireworksHelper
```

Reads a firework rocket's flight duration and explosions.

**Returns:** the rocket data, or `null` if the component is absent

### getFood {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getFood-}

```js
getFood(): FoodComponentHelper
```

Reads the nutrition of this stack, including any changes to the item's defaults.

**Returns:** the food properties, or `null` if the component is absent

### getIds {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getIds-}

```js
getIds(): List<String>
```

Lists the components present on this stack.

**Returns:** registered component IDs in sorted order

### getLodestoneTracker {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getLodestoneTracker-}

```js
getLodestoneTracker(): LodestoneTrackerHelper
```

Reads the lodestone tracker stored on the stack, such as a compass linked to a lodestone.  
A tracker can be present even after Minecraft has cleared its target.

**Returns:** the tracker, or `null` if the component is absent

### getMapId {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getMapId-}

```js
getMapId(): Integer
```

Reads the numeric ID that identifies a filled map's saved data.

**Returns:** the map ID, which can be zero, or `null` if the stack has no map ID

### getMaxDamage {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getMaxDamage-}

```js
getMaxDamage(): Integer
```

Reads the stack's maximum damage, including a value inherited from the item.

**Returns:** maximum damage, or `null` if the component is absent

### getPotionContents {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getPotionContents-}

```js
getPotionContents(): PotionContentsHelper
```

Reads the potion data stored on a stack, such as a potion or tipped arrow.

**Returns:** the potion contents, or `null` if the component is absent

### getStoredEnchantments {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getStoredEnchantments-}

```js
getStoredEnchantments(): List<EnchantmentHelper>
```

Reads enchantments stored for transfer to another item, such as those in an enchanted book.  
Use [ItemStackHelper.getEnchantments()](ItemStackHelper.md#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackHelper_getEnchantments-) for enchantments applied to the stack itself.

**Returns:** the stored enchantments with levels, or `null` if the component is absent

### getTool {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getTool-}

```js
getTool(): ToolComponentHelper
```

Reads this stack's mining rules and durability cost per block.

**Returns:** the tool component, or `null` if it is absent

### getUseCooldown {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getUseCooldown-}

```js
getUseCooldown(): UseCooldownHelper
```

Reads the cooldown the item is configured to start on use. For the player's current
cooldown progress, use [ItemStackHelper.getCooldownProgress()](ItemStackHelper.md#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackHelper_getCooldownProgress-).

**Returns:** the cooldown settings, or `null` if the component is absent

### getUseRemainder {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getUseRemainder-}

```js
getUseRemainder(): ItemStackHelper
```

Reads the configured remainder after item use, such as the glass bottle left by a potion.

**Returns:** a copy of the remainder stack, or `null` if the component is absent

### getWritableBookContents {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getWritableBookContents-}

```js
getWritableBookContents(): WritableBookContentsHelper
```

Reads the plain-text pages stored in a book and quill.

**Returns:** the writable book contents, or `null` if the component is absent

### getWrittenBookContents {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_getWrittenBookContents-}

```js
getWrittenBookContents(): WrittenBookContentsHelper
```

Reads a signed book's metadata and formatted pages.

**Returns:** the written book contents, or `null` if the component is absent

### has {#com.jsmacrosce.jsmacros.client.api.helper.inventory.ItemStackComponentsHelper_has-String-}

```js
has(String id): boolean
```

Checks whether a component is present, even if it does not have an NBT representation.

**Parameters:**
- `id`: component ID; the `minecraft:` namespace can be omitted

**Returns:** whether the component is present; false for unknown or invalid IDs
