A serializer and deserializer for Minecraft's NBT archives with a lossless but ergonomic output format
npm install binary-nbt 

A serializer and deserializer for Minecraft's NBT
archives with a lossless but ergonomic output format.
This also contains a command line tool to convert an NBT file to JSON, calledbinary-nbt
Lossless here means that a value can be deserialized from NBT and then
serialized back into exactly the same NBT value (i.e. the types from the NBT can
be recovered). Ergonomic means that the deserialized value can be treated as a
plain JavaScript object. E.g. level.Data.allowCommands works as expected for alevel.dat file. Other libraries such asnbt use custom objects, e.g.{value: <...>, type: TAG_COMPOUND}.This is possible through the use of ES6Symbols -
every deserialized object has NBTTypeSymbol applied to it, which is used in
serialisation (with sensible defaults for when this is not provided). This
approach
also works for primitives.
The primary disadvantage of this approach is that the produced value cannot be
safely serialised into any form other than NBT without losing the data required
to reconstruct the NBT. However, if such serialisation is required, NBT can be
used as the format!
TAG_LONGs are deserialized as Longs from thelong package.
This library currently only supports NodeJS 8 and 10. Other javascript
environments may be supported, should the need arise. Open an issue if you are
interested. We also only support big endian NBT, so the pocket edition nbt is
not yet supported
This package is part of themcfunction-langserver project. It
is published automatically - see
the publishing strategy
Basic usage:
``ts`
function deserializeNBTUncompressed(buffer: Buffer): any;
`js
const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");
const contents = fs.readFileSync("some_nbt.nbt");
console.log(deserializeNBTUncompressed(contents));
`
Or in Typescript:
`ts
const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");
const contents = fs.readFileSync("level.dat");
const level: Level = deserializeNBTUncompressed(contents);
`
The command line tool converts NBT values to JSON, whether compressed or not:
`
$ binary-nbt --help
binary-nbt
Usage:
$ binary-nbt [...files]
Commands:
[...files] Convert files from NBT into JSON, outputting them to stdout.
If a directory is passed, it is recursively walked for files
For more info, run any command with the --help flag:
$ binary-nbt --help
Options:
-o, --out [dir] Place the resulting files into this directory instead
-e, --extension [ext] Extension to output the JSON files in (default: .json)
-h, --help Display this message
Examples:
binary-nbt some_dir file.nbt file.nbt.gz --output .
binary-nbt some_dir -o result
`
This library is yet another NBT library amongst the wide selection of existing
JavaScript NBT libraries. A comparison with a selection of the others is below.
The primary advantages of this library are the use of Promises, vanillaNumber
JavaScript classes (e.g. , String) which can be treated as primitives
in most cases, and Typescript type definitions. We also have full
API documentation generated
using TypeDoc.
A brief rundown of the differences between other packages and this one are
below. For example, if they don't support little endian NBT, no attention is
brought to that fact as it is not a difference from this package. These packages
are described in the order they appear in when searching for
nbt on npm.
- Has a very desirable name on npm;{value:
- Data can be safely serialised and deserialized into formats other than NBT;
- Supports the browser
- Has good API documentation, although
the following two points aren't well signposted;
- Every value is wrapped in an unwieldy
object;TAG_LONGS
- s are encoded as an [upper, upper] number array, without anyTypescript
convenience APIs;
- Doesn't have type definitions available;Promise
- Doesn't use s (this can be circumvented using util.promisify);
- Supports little endian NBT;
- Uses same format as nbt for longs and other values;Typescript
- Has weak API documentation and no type definitions;Promise
- Doesn't use s;
- Uses BigInts for longs, so only supports Node 10;value
- Custom classes are used for numbers, so the value must be accessed using the
property;
Some packages are not up-to-date with the latest version of NBT or are not NBT
packages and so are excluded. These are:
- node-nbt,
mcnbt,
nbt-js,
minecraft-nbt: No support for
TAG_LONG_ARRAYmc-schematic
- ,minecraft-schematic
:nibbit
Not packages for arbritrary NBT data - provide a APIs for schematics.
- ,nibbit-nolong
: No supportnbtviewer
for longs or long arrays.
- : An NBT cli, not amcpe-anvil
package
- : Just usesprismarine-nbt
internally.@nbxx/nb-table
- ,nbtemplates
andnbtc`: Unrelated to Minecraft NBT
The
changelog
contains a list of all the changes between versions