NBTDecoder
Source: decoder.ts:453
Binary decoding helper class
Constructors
new NBTDecoder(
buffer: Buffer,
needDecompress: boolean,
): NBTDecoder
Parameters
buffer(Buffer)needDecompress(boolean, default: "false")
Returns
NBTDecoder
Properties
buffer: Buffer
offset: number
Default: 0
Methods
decode(isNetwork: boolean): any
Read an NBT
Note that Network NBT slice some root data so I have to specified it through isNetwork param
Parameters
isNetwork(boolean, default: "true")
Returns
any
read<
T,
>(
size: number,
readFunc: (buffer: Buffer) => (offset?: number) => T,
): T
Read shortcut
Read size bytes, use readFunc, then add size to internal offset
Type Parameters
T
Parameters
size(number)readFunc((buffer: Buffer) => (offset?: number) => T)
Returns
T
readAngle(): number
Read an angle.
An angle is a rotation angle in steps of 1/256 of a full turn
Returns
number
readArray<
T,
>(
length: number,
readFunc: (decoder: BinaryDecoder, ind: number) => T,
): T[]
Read an array with known length
Type Parameters
T
Parameters
length(number)readFunc((decoder: BinaryDecoder, ind: number) => T)
Returns
T[]— an array
readBoolean(): boolean
Read 1 byte
Returns
boolean— a boolean
readByte(): number
Read 1 byte
Returns
number— signed 1-byte integer
readChatType(
): { chat: { parameters: number[]; style: any; translationKey: string }; narration: { parameters: number[]; style: any; translationKey: string } }
Read Chat Type
Returns
{ chat: { parameters: number[]; style: any; translationKey: string }; narration: { parameters: number[]; style: any; translationKey: string } }
readChatTypeDecoration(
): { parameters: number[]; style: any; translationKey: string }
Read Chat Type Decoration
The chat type decorations look like:
| Name | Type | Description |
|---|---|---|
| Translation Key | String | |
| Parameters | Prefixed Array of VarInt Enum | 0: sender, 1: target, 2: content |
| Style | NBT |
Returns
{ parameters: number[]; style: any; translationKey: string }
readCompound(): Record<string, any>
Read n bits
A Compound entry would look like this:
[1 byte of Type][2 byte of field name length][N bits of field length][N bits of data]
Returns
Record<string, any>— signed n-bit integer
readCompoundList(type?: number): any[]
Read a list / array of tag
A list / array packet should look like this: [1 byte of type, if not provided][4 bytes of length][N bits of data]
Parameters
type(number, optional)
Returns
any[]
readCompoundString(): string
Read a Compound string
A string packet would look like this:
[2 bytes as length][N bits of data]
Returns
string— a string
readCompoundValue(tag: number): any
Read compound value base on provided tag
Parameters
tag(number)
Returns
any
readDouble(): number
Read 8 bytes
Returns
number— signed 8-byte decimal
readFixedPoint(x: number, n: number): number
Resolve a fixed point to a double
A fixed point is a certain number of bits represent the signed integer part (number to the left of the decimal point) and the rest represent the fractional part (to the right).
Parameters
x(number) — number to resolen(number) — n fraction bits
Returns
number
readFloat(): number
Read 4 bytes
Returns
number— signed 4-bytes decimal
readIdOrX(readX: (decoder: BinaryDecoder) => any): any
Represents a data record of type X, either inline, or by reference to a registry implied by context
Parameters
readX((decoder: BinaryDecoder) => any)
Returns
any
readIdSet(
): { ids: number[] | null; tagName: string | null; type: number }
Represents a set of IDs in a certain registry (implied by context), either directly (enumerated IDs) or indirectly (tag name).
| Field Name | Field Type | Meaning |
|---|---|---|
| Type | VarInt | Value used to determine the data that follows. It can be either: 0 - Represents a named set of IDs defined by a tag. Anything else - Represents an ad-hoc set of IDs enumerated inline. |
| Tag Name | Optional Identifier | The registry tag defining the ID set. Only present if Type is 0. |
| IDs | Optional Array of VarInt | An array of registry IDs. Only present if Type is not 0. The size of the array is equal to Type - 1. |
Returns
{ ids: number[] | null; tagName: string | null; type: number }
readInt(): number
Read 4 bytes
Returns
number— signed 4-byte integer
readLong(): bigint
Read 8 bytes
Returns
bigint— signed 8-byte integer
readLpVec3(): Vec3
Read LpVec3
Read this article for more information: https://minecraft.wiki/w/Java_Edition_protocol/Data_types#LpVec3
Returns
readNBT(): any
Read a NBT
Returns
any
readPosition(): { x: number; y: number; z: number }
Read a Position
A position packet is a 64-bit value, split into three signed integer parts:
x: 26 MSBs
z: 26 middle bits
y: 12 LSBsReturns
{ x: number; y: number; z: number }— a Position
readPrefixedArray<
T,
>(
readFunc: (decoder: BinaryDecoder, ind: number) => T,
): T[]
Read a length-prefixed array
An array packet would look like this: [VarInt as length][N bytes of data]
Type Parameters
T
Parameters
readFunc((decoder: BinaryDecoder, ind: number) => T)
Returns
T[]— a length-prefixed array
readPrefixedOptional<
T,
>(
readFunc: (decoder: BinaryDecoder) => T,
): T | null
Read a Prefixed Optional field
A Prefixed Optional would look like this: [Boolean to indicate whenether the following field is presented][N bytes of data if the boolean before is true, or else empty]
Type Parameters
T
Parameters
readFunc((decoder: BinaryDecoder) => T)
Returns
T | null
readRawArray(
length: number,
size: number,
): Buffer<ArrayBufferLike>
Read an array with known length and return Buffer
Parameters
length(number)size(number) — size of the item, in byte
Returns
Buffer<ArrayBufferLike>— an array
readRawPrefixedArray(size: number): Buffer<ArrayBufferLike>
Read a length-prefixed array and return the raw buffer
An array packet would look like this: [VarInt as length][N bytes of data]
Parameters
size(number) — size of the item, in byte
Returns
Buffer<ArrayBufferLike>— a length-prefixed array
readShort(): number
Read 2 bytes
Returns
number— signed 2-byte integer
readString(length?: number): string
Read a string
A string packet would look like this:
[VarInt as length, if not provided][N bits of data]
Parameters
length(number, optional)
Returns
string— a string
readTeleportFlag(
): { pitch: boolean; rotateVelocity: boolean; velX: boolean; velY: boolean; velZ: boolean; x: boolean; y: boolean; yaw: boolean; z: boolean }
Read a Teleport Flag packet
A Teleport Flag packet, which is bit mask represented as an int, would look like this:
| Hex Mask | Field |
|---|---|
| 0x0001 | Is X relative |
| 0x0002 | Is Y relative |
| 0x0004 | Is Z relative |
| 0x0008 | Is Yaw relative |
| 0x0010 | Is Pitch relative |
| 0x0020 | Is Velocity X relative |
| 0x0040 | Is Velocity Y relative |
| 0x0080 | Is Velocity Z relative |
| 0x0100 | Rotate velocity |
Returns
{ pitch: boolean; rotateVelocity: boolean; velX: boolean; velY: boolean; velZ: boolean; x: boolean; y: boolean; yaw: boolean; z: boolean }
readUByte(): number
Read 1 byte
Returns
number— unsigned 1-byte integer
readUShort(): number
Read 2 bytes
Returns
number— unsigned 2-byte integer
readUUID(): string
Read a 16-byte UUID
Returns
string
readVarInt(): number
Read n bytes of integer
A VarInt packet would look like this:
[7 bits of data][1 bit indicate if the VarInt ended here]
Returns
number— signed n-bit integer
readVarLong(): bigint
Read n bytes of integer
Similar to VarInt, A VarLong packet would look like this:
[7 bits of data][1 bit indicate if the VarLong ended here]
Returns
bigint— signed n-bit integer