Skip to content

Nbt Module

npm versionDownloadsInstall sizenpmBuild Status

Provide function to read NBT binary format to json.

Usage

Read and Write NBT

You can simply deserialize/serialize nbt.

ts
import { serialize, deserialize } from "@xmcl/nbt";
const fileData: Buffer;
// compressed = undefined will not perform compress algorithm
// compressed = true will use gzip algorithm
const compressed: true | "gzip" | "deflate" | undefined;
const readed: any = await deserialize(fileData, { compressed });
// The deserialize return object contain NBTPrototype property which define its nbt type
// After you do the modification on it, you can serialize it back to NBT
const buf: Buffer = await serialize(readed, { compressed });

You can use class with annotation (decorator) to serialize/deserialize the type consistently.

Suppose you are reading the servers.dat. You can have:

ts
import { serialize, deserialize, TagType } from "@xmcl/nbt";

class ServerInfo {
    @TagType(TagType.String)
    icon: string = "";
    @TagType(TagType.String)
    ip: string = "";
    @TagType(TagType.String)
    name: string = "";
    @TagType(TagType.Byte)
    acceptTextures: number = 0;
}

class Servers {
    @TagType([ServerInfo])
    servers: ServerInfo[] = []
}

// read
// explict tell the function to deserialize into the type Servers
const servers = await deserialize(data, { type: Servers });
const infos: ServerInfo[] = servers.servers;

// write
const servers: Servers;
const binary = await serialize(servers);

🧾 Classes

🤝 Interfaces

🗃️ Namespaces

🏭 Functions

TagType

ts
TagType(type: TagType | Schema | Constructor<T>): Function

Annotate the type of a field

Parameters

  • type: TagType | Schema | Constructor<T>

Return Type

  • Function

Defined in: packages/nbt/index.ts:62, packages/nbt/index.ts:17, packages/nbt/index.ts:138

deserialize

ts
deserialize(fileData: Uint8Array, option: DeserializationOption<T>= {}): Promise<T>

Deserialize the nbt binary into json

Parameters

  • fileData: Uint8Array The nbt binary
  • option: DeserializationOption<T>

Return Type

  • Promise<T>

Defined in: packages/nbt/index.ts:410

deserializeSync

ts
deserializeSync(fileData: Uint8Array, option: DeserializationOption<T>= {}): T

Deserialize the nbt binary into json

Parameters

  • fileData: Uint8Array The nbt binary
  • option: DeserializationOption<T>

Return Type

  • T

Defined in: packages/nbt/index.ts:435

getPrototypeOf

ts
getPrototypeOf(object: object | Function): NBTPrototype | undefined

Get NBT schema for this object or a class.

If the param is a object, any modifications on this prototype will only affact this object. If the param is a class, any modifications on this prototype will affact all object under this class

Parameters

  • object: object | Function The object or class

Return Type

  • NBTPrototype | undefined

Defined in: packages/nbt/index.ts:118

serialize

ts
serialize(object: object, option: SerializationOption= {}): Promise<Uint8Array>

Serialzie an nbt typed json object into NBT binary

Parameters

  • object: object The json
  • option: SerializationOption

Return Type

  • Promise<Uint8Array>

Defined in: packages/nbt/index.ts:401

serializeSync

ts
serializeSync(object: object, option: SerializationOption= {}): Uint8Array

Serialzie an nbt typed json object into NBT binary

Parameters

  • object: object The json
  • option: SerializationOption

Return Type

  • Uint8Array

Defined in: packages/nbt/index.ts:425

setPrototypeOf

ts
setPrototypeOf(object: object | Function, nbtPrototype: NBTPrototype): void

Set and change the NBT prototype of this object or class

Parameters

  • object: object | Function A object or a class function
  • nbtPrototype: NBTPrototype The nbt prototype

Return Type

  • void

Defined in: packages/nbt/index.ts:128

🏷️ Variables

kNBTConstructor

ts
kNBTConstructor: typeof kNBTConstructor = ...

Defined in: packages/nbt/index.ts:15

kNBTPrototype

ts
kNBTPrototype: typeof kNBTPrototype = ...

Defined in: packages/nbt/index.ts:14

⏩ Type Aliases

CompoundSchema

ts
CompoundSchema: Object

Defined in: packages/nbt/index.ts:174

ListSchema

ts
ListSchema: [TagType | Schema]

Defined in: packages/nbt/index.ts:173

Schema

ts
Schema: ListSchema | CompoundSchema | Constructor<any>

Defined in: packages/nbt/index.ts:172

TagType

ts
TagType: TagTypePrimitive | typeof List | typeof Compound

Defined in: packages/nbt/index.ts:62, packages/nbt/index.ts:17, packages/nbt/index.ts:138

TagTypePrimitive

ts
TagTypePrimitive: typeof End | typeof Byte | typeof Short | typeof Int | typeof Long | typeof Float | typeof Double | typeof ByteArray | typeof String | typeof IntArray | typeof LongArray

Defined in: packages/nbt/index.ts:22

Released under the MIT License.