@alan404/nbt
v0.0.2
Published
Typescript library for serializing and deserializing NBT formats
Downloads
7
Readme
nbt-ts
NBT serialize and deserialize library for TypeScript
Installation
npm i @alan404/nbt
pnpm add @alan404/nbt
yarn add @alan404/nbt
NBTTag
NBT tags are represented as Enums - { type: string; data: any; }
import { NBTTag } from "@alan404/nbt";
let tag = {
type: "Compound",
data: {
name: {
type: "String",
data: "Sophia",
},
age: {
type: "Byte",
data: 19,
},
},
};
// Same thing but with enum factory
let tag = NBTTag.Compound({
name: NBTTag.String("Sophia"),
age: NBTTag.Byte(19),
});
Manual Serialization / Deserialization
This library uses the native javascript ArrayBuffer API.
import { NBTCompound, serializeNBT, deserializeNBT } from "@alan404/nbt";
let buffer: ArrayBuffer = serializeNBT(tag);
let anotherTag: NBTCompound = deserializeNBT(buffer);
NBTDocument
This library also provides some support for serde-like functionality:
import { NBTDocument, Field } from "@alan404/nbt";
class Person extends NBTDocument {
@Field("String")
name: string = "";
@Field("Byte")
age: number = 0;
}
// or Person.deserialize(ArrayBuffer)
let person = Person.deserializeFromTag(tag);
let buf: ArrayBuffer = person.serialize();
let tag = person.serializeToTag();
Supports more complicated scenarios:
import { NBTRoot, NBTDocument, Field } from "@alan404/nbt";
class Server extends NBTDocument {
@Field("String")
name: string;
@Field("String")
ip: string;
@Field("String")
icon: boolean;
}
class ServersDat extends NBTDocument {
[NBTRoot] = "";
@Field("List", ["Compound", Server])
servers: Server[];
}
The [NBTRoot]
symbol property allows you to serialize and deserialize root NBT tags (nbt format is implicitly inside a NBTCompound), set to false
to mark as not root (default) or a string to give it a name.