NBTElementHelper
com.jsmacrosce.jsmacros.client.api.helper.NBTElementHelper
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
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
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
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
asText
Returns Minecraft's pretty-printed representation of this tag as styled text.
Returns: the pretty-printed tag as styled text
getType
Returns this tag's Minecraft NBT type id; see the class documentation for the mapping.
Returns: this tag's NBT type id
isCompound
Returns whether this tag is a compound.
Returns:
true if this element is a compoundisList
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 arrayisNull
Returns whether this tag is the NBT end marker (type id 0).
Returns:
true if this is the end marker rather than a valueisNumber
Returns whether this tag is numeric (byte, short, int, long, float, or double).
Returns:
true if this element is a numberisString
Returns whether this tag is a string.
Returns:
true if this element is a stringresolve
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 exceptionWraps 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, ornull
Returns: a helper of the matching subclass, or
null if element was nulltoString
wrap
Wraps a tag without copying it, using the helper for its type; returns
null for null input.Parameters:
element: the element to wrap, ornull
Returns: a helper of the matching subclass, or
null if element was nullwrapCompound
Wraps the given compound tag without copying it; returns
null for null input.Parameters:
compound: the compound to wrap, ornull
Returns: a helper for it, or
null if compound was null