binary-nbt
v0.5.7
Published
A serializer and deserializer for Minecraft's NBT archives with a lossless but ergonomic output format
Downloads
5
Readme
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, called
binary-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 a
level.dat
file. Other libraries such as
nbt
use custom objects, e.g.
{value: <...>, type: TAG_COMPOUND}
.This is possible through the use of ES6
Symbol
s -
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_LONG
s are deserialized as Long
s from the
long
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 the
mcfunction-langserver
project. It
is published automatically - see
the publishing strategy
API
Deserialization
Basic usage:
function deserializeNBTUncompressed(buffer: Buffer): any;
const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");
const contents = fs.readFileSync("some_nbt.nbt");
console.log(deserializeNBTUncompressed(contents));
Or in Typescript:
const { deserializeNBTUncompressed } = require("binary-nbt");
const fs = require("fs");
const contents = fs.readFileSync("level.dat");
const level: Level = deserializeNBTUncompressed(contents);
Command line
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
Comparison with alternatives
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 Promise
s, vanilla
JavaScript classes (e.g. Number
, 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.
nbt
:
- Has a very desirable name on
npm
; - 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
{value: <actual value>, type: <type>}
object; TAG_LONGS
s are encoded as an[upper, upper]
number array, without any convenience APIs;- Doesn't have
Typescript
type definitions available; - Doesn't use
Promise
s (this can be circumvented usingutil.promisify
);
prismarine-nbt
- Supports little endian NBT;
- Uses same format as
nbt
for longs and other values; - Has weak API documentation and no
Typescript
type definitions; - Doesn't use
Promise
s;
nbt-ts
- Uses
BigInt
s for longs, so only supports Node 10; - Custom classes are used for numbers, so the value must be accessed using the
value
property;
Excluded packages
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 forTAG_LONG_ARRAY
mc-schematic
,minecraft-schematic
: Not packages for arbritrary NBT data - provide a APIs for schematics.nibbit
,nibbit-nolong
: No support for longs or long arrays.nbtviewer
: An NBT cli, not a packagemcpe-anvil
: Just usesprismarine-nbt
internally.@nbxx/nb-table
,nbtemplates
andnbtc
: Unrelated to Minecraft NBT
Changelog
The changelog contains a list of all the changes between versions