# HeldItemChange

`com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange`

**Since:** 1.2.7

**Cancellable:** No. Calling `event.cancel()` throws `UnsupportedOperationException`.

Fires when polling detects a change to the local player's main- or off-hand stack, at most once
per client tick. It does not fire for an empty-to-empty transition. If only damage changes,  
[EventItemDamage](../inventory/EventItemDamage.md) fires first.  
```js
JsMacros.on("HeldItemChange", JavaWrapper.methodToJava(function (event) {
  const hand = event.offHand ? "off hand" : "main hand";  
  const now = event.item.isEmpty() ? "empty" : `${event.item.getItemId()} x${event.item.getCount()}`;  
  const before = event.oldItem.isEmpty() ? "empty" : `${event.oldItem.getItemId()} x${event.oldItem.getCount()}`;  
  Chat.log(`${hand}: ${before} to ${now}`);  
}))
```

## Constructors

### new EventHeldItemChange {#com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange_constructor-ItemStack-ItemStack-boolean-}

```js
new EventHeldItemChange(ItemStack item, ItemStack oldItem, boolean offHand)
```

Wraps the supplied current and previous raw stacks and records which hand they describe.  
This constructor does not detect or dispatch a hand change. Each argument is retained as
the corresponding helper's raw value; a `null` stack therefore produces a non-null
helper wrapping `null`.

**Parameters:**
- `item`: current raw stack; may be `null`
- `oldItem`: previous raw stack; may be `null`
- `offHand`: `true` for the off hand, `false` for the main hand

## Fields

### item {#com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange_item}

```js
item: ItemStackHelper
```

The current stack in the changed hand. An empty hand is represented by an empty
[ItemStackHelper](../../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/inventory/ItemStackHelper.md), not `null`.

### offHand {#com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange_offHand}

```js
offHand: boolean
```

Whether the changed stack is in the off hand; `false` indicates the main hand.

### oldItem {#com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange_oldItem}

```js
oldItem: ItemStackHelper
```

The previous stack in the changed hand; an empty hand is represented by an empty
[ItemStackHelper](../../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/inventory/ItemStackHelper.md), not `null`.

## Methods

### toString {#com.jsmacrosce.jsmacros.client.api.event.impl.player.EventHeldItemChange_toString-}

```js
toString(): String
```

Formats the event name and current stack helper as `%s:{"item": %s}`; the previous
stack and hand indicator are omitted. Formatting dereferences the current raw stack, so a
current stack wrapped from `null` causes a [NullPointerException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/NullPointerException.html).

**Returns:** the formatted event name and current stack representation
