eznbt
Easy Minecraft NBT reading and writing
Prerequisites
Make sure to use a node version that supports BigInts, they're used for the Long NBT Tag
Usage
Pretty much all you need to know is that PascalCase members of eznbt
are classes that can be used for both reading
and writing. Its constructors accept an object containing either a value
or buffer
property, from which the class
is then constructed. The instance will contain a value
and buffer
property.
For ease of use lowercase members of eznbt
can be used to construct classes like this: int(5)
(instead of
new Int({ value: 5 })
)
Writing stuff
const NBTString string rootcompound = NBT /* NBT files are usually implicitly in a compound. If this is the case you should use * a named RootCompound. You name it by providing a [Symbol.for('NBTRootTagName')] string. */const myCompound = myCompoundbuffer // ready to send to your client/server
Reading stuff
const Compound = NBT const myBuffer = ... // a buffer you received. it is an nbt compound tag (as always)const myCompound = buffer: myBuffer myCompoundvalue // yields the compound as an object. children are NBT Tag instancesmyCompoundjson // yields the compound as an object. children are JS types (number, string, bigint, etc.)
Reading and writing lists
Lists are special because they have one set type, which you will first have to pass to the List
or list
function.
const List list NBTString = NBT const myList = 'hello' 'world!' // creates a string-list // When the type of the list is unknown, it can only be used to read stuff:const value = new buffer: myListbuffer
Compound shorthands
The compound tag comes with a few handy shorthands
const int compound = NBT const myCompound =
Compound vs RootCompound
Since generally all NBT files are implicitly in a compound tag, you should probably always use RootCompound for the outside object, and Compound otherwise
// RootCompound.json vs Compound.json // RootCompound.json will put all values as a property of the root compound's namejson/** { * 'hello!': { * a: 1, * b: 2 * } * } */ // Compound doesn't have a name so it won't be namedjson/** { * a: 1, * b: 2 * } */
Typescript
This package has been made using Typescript and ships with type declarations.