Skip to content

NBTElementHelper ​

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

Since1.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"); }

// an item's data, which is a compound
const nbt = player.getMainHand().getNBT();  
if (nbt !== null) {
  // ask the helper type before reaching into it
  if (nbt instanceof NBTHelpers.Compound) {
    for (const key of nbt.getKeys()) {
      Chat.log(`${key}: ${nbt.asString(key)}`);  
    }
  }

  // and the game's own pretty-printed form, as styled text
  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 ​

Methods ​

asCompoundHelper ​

asCompoundHelper(): NBTElementHelper.NBTCompoundHelperSince1.5.1#
Returns this helper as a compound helper; the tag must be a compound or the cast fails.
Returns: this helper as a compound view

asListHelper ​

asListHelper(): NBTElementHelper.NBTListHelperSince1.5.1#
Returns this helper as a list/array helper; the tag must be a list or array or the cast fails. 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

asNumberHelper ​

asNumberHelper(): NBTElementHelper.NBTNumberHelperSince1.5.1#
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

asString ​

asString(): StringSince1.5.1#
Returns the string value for string tags, or the tag's textual representation otherwise.
Returns: the string value or textual representation

asText ​

asText(): TextHelperSince2.0.0#
Returns Minecraft's pretty-printed representation of this tag as styled text.
Returns: the pretty-printed tag as styled text

getType ​

getType(): intSince1.5.1#
Returns this tag's Minecraft NBT type id; see the class documentation for the mapping.
Returns: this tag's NBT type id

isCompound ​

isCompound(): booleanSince1.5.1#
Returns whether this tag is a compound.
Returns: true if this element is a compound

isList ​

isList(): booleanSince1.5.1#
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().
Returns: true if this element is a list or an array

isNull ​

isNull(): booleanSince1.5.1#
Returns whether this tag is the NBT end marker (type id 0).
Returns: true if this is the end marker rather than a value

isNumber ​

isNumber(): booleanSince1.5.1#
Returns whether this tag is numeric (byte, short, int, long, float, or double).
Returns: true if this element is a number

isString ​

isString(): booleanSince1.5.1#
Returns whether this tag is a string.
Returns: true if this element is a string

resolve ​

resolve(String nbtPath): List<NBTElementHelper<?>>Since1.9.0#
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.
const player = Player.getPlayer();
if (player === null) { throw new Error("not in a world"); }
const nbt = player.getMainHand().getNBT();
if (nbt !== null) {
  // a key at the top level
  const damage = nbt.resolve("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
static resolve(Tag element): NBTElementHelper<?>Since1.5.1#
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

toString ​

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 ​

static wrap(Tag element): NBTElementHelper<?>Since1.9.1#
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

wrapCompound ​

static wrapCompound(CompoundTag compound): NBTElementHelper.NBTCompoundHelperSince1.9.0#
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