Skip to content

NBT Manipulation

Meredith Espinosa edited this page Jan 21, 2020 · 2 revisions

This page serves as documentation for NBT manipulation in tweakers. All sample code is in JavaScript.

NBT Types

NBT has a set list of types for its tags. These types are labeled as int values in the actual tag, but LibCD presents them as strings for better readability. The tag types are as follows, with the number and the string name:

  1. "byte"
  2. "short"
  3. "int"
  4. "long"
  5. "float"
  6. "double"
  7. "byte array"
  8. "string"
  9. "list"
  10. "compound"
  11. "int array"
  12. "long array"

There are two other special tag values: 0 for an end-marker tag (only used in binary storage format) and 99 for accepting any type of number when reading. Neither of these are currently important in the context of LibCD.

Creating tags

LibCD adds a helper to create new NBT tags from a scripting environment. This can be accessed as var Nbt = libcd.import("libcd.util.Nbt");. It provides the following functions for creating new tags:

  • newCompound() - Creates and returns a new empty compound tag.
  • newList() - Creates and returns a new empty list tag.
  • listOf(...items) - Passed either an array of items or any number of items separated by commas, creates and returns a new list tag filled to match the array or vararg passed in.
  • listOf(items) - Passed a java List, creates and returns a new list tag filled to match the java List passed in.

Manipulating tags

The primary tags you can manipulate are Compound and List tags. All other types of tags hold primitives, so must be set or gotten inside a compound or list.

Compound tag

Compound tags are key-value storage of NBT tags. They have the following methods:

  • getKeys() - Returns an array of all the key names stored in this tag.
  • getType(key) - Returns the string name of the tag type stored at this key.
  • hasTag(key) - Returns true if the compound has a tag with this key name.
  • hasTag(key, type) - Returns true if the compound has a tag with this key name, and the tag is of the type specified.
  • getTag(key) - Returns the tag stored at this key value.
  • getByte(key) - Returns the byte stored at this key value, or 0.
  • putByte(key, value) - Stores the specified byte value at this key.
  • `getBoolean(key) - Returns the boolean stored at this key value, or false.
  • putBoolean(key, value) - Stores the specified boolean value at this key.
  • getShort(key) - Returns the short stored at this key value, or 0.
  • putShort(key, value) - Stores the specified short value at this key.
  • getInt(key) - Returns the int stored at this key value, or 0.
  • putInt(key, value) - Stores the specified int value at this key.
  • getLong(key) - Returns the long stored at this key value, or 0.
  • putLong(key, value) - Stores the specified long value at this key.
  • hasUuid(key)- Returns true if there is a UUID stored at this key value.
  • getUuid(key) - Returns the string form of the UUID at this key.
  • putUuid(key, value) - Stores the specified UUID value at this key.
  • getFloat(key) - Returns the float at this key value, or 0.
  • putFloat(key, value) - Stores the specified float value at this key.
  • getDouble(key) - Returns the double at this key value, or 0.
  • putDouble(key, value) - Stores the specified double value at this key.
  • getByteArray(key) - Returns the byte array at this key value, or [].
  • putByteArray(key, value) - Stores the specified byte array value at this key.
  • getString(key) - Returns the string at this key value, or "".
  • putString(key, value) - Stores the specified string value at this key.
  • getListType(key) - Returns the type of the list at the specified key, or "" if it's not a list.
  • getList(key, type) - Returns the list with the specified type at this key, or an empty list.
  • putList(key, value) - Stores the passed NBT list (see below) at this key.
  • getCompound(key) - Returns the compound tag at this key value, or an empty compound tag.
  • putCompound(key, value) - Stores the passed compound tag at this key.
  • getIntArray(key) - Returns the int array at this key value, or [].
  • putIntArray(key, value) - Stores the specified int array value at this key.
  • getLongArray(key) - Returns the long array at this key value, or [].
  • putLongArray(key, value) - Stores the specified long array value at this key.
  • remove(key) - Removes the tag with the specified key from the compound.
  • clear() - Removes all tags in the compound.
  • isEmpty() - Returns true if there are no tags in this compound.
  • toString() - Returns a stringified form of this NBT.

List tag

List tags are array storage of NBT tags. They can only store one type of tag at a time, and are flexible in size. A list's type is set when the first element is added to it after the list is cleared. Lists have the following methods:

  • getListType() - Returns the string name of the current type of the list.
  • getSize() - Returns the number of elements in the list.
  • get(index) - Returns the object at the specified index in the list.
  • set(index, value) - Sets the object at the specified index to the specified value. Will only succeed if the object matches the list's type. Returns true if the object was successfully set, or false otherwise.
  • add(value) - Adds the specified value to the end of the list. Will only succeed if the value matches the list's type. Returns true if the object was successfully added, or false otherwise.
  • add(index, value) - Adds the specified value to the specific index in the list, bumping everything after it one index to the right. Will only succeed if the value matches the list's type. Returns true if the object was successfully added, or false otherwise.
  • remove(index) - Removes the object at the specified index from the list. Shifts all items at higher indexes one to the left. If this is the last item in the list, the list's type will be reset. Returns the removed object.
  • clear() - Removes all objects from the list, and resets the list's type.
  • isEmpty() - Returns true if there are no items in the list.
Clone this wiki locally