# NBTElementHelper

`com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper`

**Since:** 1.5.1

Wraps an NBT tag and provides type checks, typed helper views, and path lookup. The numeric
type ids are the game's tag ids: 0 is the end marker, 1–6 are numeric tags, 7 is a byte array,  
8 a string, 9 a list, 10 a compound, 11 an int array, and 12 a long array.  

A tag helper wraps the original tag rather than copying it; compound, list, and numeric tags
have specialized helper subclasses.  
```js
const player = Player.getPlayer();  
if (player === null) { throw new Error("not in a world"); }

const nbt = player.getMainHand().getComponents().getCustomData();  
if (nbt !== null) {
  Chat.log(nbt.asText().getString());  
}
```

Available in scripts as `NBTHelpers.Element`.

## Type Parameters

- `T`: the Minecraft NBT tag type wrapped by this helper

## Nested types

- [NBTCompoundHelper](NBTElementHelper.NBTCompoundHelper.md)
- [NBTListHelper](NBTElementHelper.NBTListHelper.md)
- [NBTNumberHelper](NBTElementHelper.NBTNumberHelper.md)

## Methods

### asCompoundHelper {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_asCompoundHelper-}

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

Returns this helper as a compound helper; the tag must be a compound or the cast fails.

**Returns:** this helper as a compound view

**Since:** 1.5.1

### asListHelper {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_asListHelper-}

```js
asListHelper(): NBTElementHelper.NBTListHelper
```

Returns this helper as a list/array helper; the tag must be a list or array or the cast
fails. [NBTListHelper.getHeldType()](NBTElementHelper.NBTListHelper.md#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper.NBTListHelper_getHeldType-) returns the array component id, but returns
`TAG_COMPOUND` for ordinary lists regardless of their elements.

**Returns:** this helper as a list/array view

**Since:** 1.5.1

### asNumberHelper {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_asNumberHelper-}

```js
asNumberHelper(): NBTElementHelper.NBTNumberHelper
```

Returns this helper as a numeric helper; the tag must be numeric or the cast fails. This
returns the same helper instance, not a copy.

**Returns:** this helper as a numeric view

**Since:** 1.5.1

### asString {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_asString-}

```js
asString(): String
```

Returns the string value for string tags, or the tag's textual representation otherwise.

**Returns:** the string value or textual representation

**Since:** 1.5.1

### asText {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_asText-}

```js
asText(): TextHelper
```

Returns Minecraft's pretty-printed representation of this tag as styled text.

**Returns:** the pretty-printed tag as styled text

**Since:** 2.1.0

### getType {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_getType-}

```js
getType(): int
```

Returns this tag's Minecraft NBT type id; see the class documentation for the mapping.

**Returns:** this tag's NBT type id

**Since:** 1.5.1

### isCompound {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_isCompound-}

```js
isCompound(): boolean
```

Returns whether this tag is a compound.

**Returns:** `true` if this element is a compound

**Since:** 1.5.1

### isList {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_isList-}

```js
isList(): boolean
```

Returns whether this tag is a list or an array tag. An int array of length four can be
interpreted as a UUID by [NBTListHelper.asUUID()](NBTElementHelper.NBTListHelper.md#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper.NBTListHelper_asUUID-).

**Returns:** `true` if this element is a list or an array

**Since:** 1.5.1

### isNull {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_isNull-}

```js
isNull(): boolean
```

Returns whether this tag is the NBT end marker (type id 0).

**Returns:** `true` if this is the end marker rather than a value

**Since:** 1.5.1

### isNumber {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_isNumber-}

```js
isNumber(): boolean
```

Returns whether this tag is numeric (byte, short, int, long, float, or double).

**Returns:** `true` if this element is a number

**Since:** 1.5.1

### isString {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_isString-}

```js
isString(): boolean
```

Returns whether this tag is a string.

**Returns:** `true` if this element is a string

**Since:** 1.5.1

### resolve {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_resolve-String-}

```js
resolve(String nbtPath): List<NBTElementHelper<?>>
```

Resolves a Minecraft NBT path against this tag.  

Returns all matching tags, or `null` if evaluation yields no matches or raises a
syntax exception. Invalid syntax during parsing throws CommandSyntaxException.  
Parsed paths are cached, with at most 24 entries.  
```js
const player = Player.getPlayer();  
if (player === null) { throw new Error("not in a world"); }
const nbt = player.getMainHand().getComponents().getChanges();  
if (nbt !== null) {
  // a key at the top level
  const damage = nbt.resolve('"minecraft:damage"');  
  if (damage !== null) {
    for (const element of damage) {
      Chat.log(`Damage is ${element.asString()}`);  
    }
  }
}
```

**Parameters:**
- `nbtPath`: the path to resolve, in the game's nbt path syntax

**Returns:** the matched elements, or `null` if evaluation yields no matches or raises a
syntax exception

**Since:** 1.9.0

### resolve {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_resolve-Tag-}

```js
static resolve(Tag element): NBTElementHelper<?>
```

Wraps a tag without copying it, using a specialized helper for numeric, list/array, and
compound tags; other tags use the base helper. Returns `null` for `null` input.

**Parameters:**
- `element`: the element to wrap, or `null`

**Returns:** a helper of the matching subclass, or `null` if `element` was
`null`

**Since:** 1.5.1

### toString {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_toString-}

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

Returns a diagnostic string containing the wrapped tag's `toString()` representation.

**Returns:** this helper's diagnostic representation of the wrapped NBT tag

### wrap {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_wrap-Tag-}

```js
static wrap(Tag element): NBTElementHelper<?>
```

Wraps a tag without copying it, using the helper for its type; returns `null` for
`null` input.

**Parameters:**
- `element`: the element to wrap, or `null`

**Returns:** a helper of the matching subclass, or `null` if `element` was
`null`

**Since:** 1.9.1

### wrapCompound {#com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper_wrapCompound-CompoundTag-}

```js
static wrapCompound(CompoundTag compound): NBTElementHelper.NBTCompoundHelper
```

Wraps the given compound tag without copying it; returns `null` for `null`
input.

**Parameters:**
- `compound`: the compound to wrap, or `null`

**Returns:** a helper for it, or `null` if `compound` was `null`

**Since:** 1.9.0
