Class BufWrapper<Plugins>

Type Parameters

Hierarchy

  • BufWrapper

Constructors

Properties

buffer: Buffer

The wrapped NodeJS buffer

buffers: Buffer[]

List of buffers, used for the oneConcat option

offset: number

Current offset (used for reading)

options?: BufWrapperOptions<Plugins>

Options that apply to the current BufWrapper instance

plugins: BufWrapperPlugins<Plugins>

Installed plugins so you can access them from this object

defaultPlugins: BufWrapperPluginsArgument = {}

Object containing plugins that will be automatically installed to new BufWrapper instances.

Methods

  • When the BufWrapperOptions#oneConcat is set to true you must call this method to concatenate all buffers into one. If the option is undefined or set to false, this method will throw an error.

    This method will also set the BufWrapper#buffer to the concatenated buffer.

    Returns

    The concatenated buffer.

    Returns Buffer

  • Read a boolean from the buffer

    Returns

    The boolean read from the buffer

    Example

    const buffer = Buffer.from([ 0x01 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readBoolean();
    console.log(decoded); // true

    Returns boolean

  • Read raw bytes from the buffer

    Returns

    The bytes read from the buffer

    Example

    const buffer = Buffer.from([ 0x01, 0x02, 0x03 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readBytes(3);
    console.log(decoded); // <Buffer 01 02 03>

    Parameters

    • length: number

      The number of bytes to read

    Returns Buffer

  • Read a double from the buffer

    Returns

    The double read from the buffer

    Example

    const buffer = Buffer.from([ 0x40, 4x45, 0x35, 0xc2, 0x8f, 0x5c, 0x28, 0xf6 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readShort();
    console.log(decoded); // 42.42

    Returns number

  • Read a float from the buffer

    Returns

    The float read from the buffer

    Example

    const buffer = Buffer.from([ 0x41, 0x45, 0x70, 0xa4 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readFloat();
    console.log(decoded); // 12.34000015258789

    Returns number

  • Read an integer from the buffer

    Returns

    The integer value read from the buffer

    Example

    const buffer = Buffer.from([0x00, 0x00, 0x00, 0x7b]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readInt();
    console.log(decoded); // 123

    Returns number

  • Read an array of ints from the buffer

    Returns

    The array read from the buffer

    Example

    const buffer = Buffer.from([ 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readIntArray();
    console.log(decoded); // [ 1, 2, 3 ]

    Returns number[]

  • Read a long from the buffer

    Returns

    The long value read from the buffer

    Example

    const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readLong();
    console.log(decoded); // 123456789

    Returns number

  • Read a float from the buffer

    Returns

    The float read from the buffer

    Example

    const buffer = Buffer.from([ 0x00, 0x2a ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readShort();
    console.log(decoded); // 42

    Returns number

  • Read a string from the buffer (will use the ut8 encoding)

    Returns

    The string value read from the buffer

    Example

    const buffer = Buffer.from([0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readString();
    console.log(decoded); // Hello World

    Returns string

  • Read an array of strings from the buffer

    Returns

    The array read from the buffer

    Example

    const buffer = Buffer.from([0x02, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x05, 0x57, 0x6f, 0x72, 0x6c, 0x64]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readStringArray();
    console.log(decoded); // ['Hello', 'World']

    Returns string[]

  • Read an UUID from the buffer

    Returns

    The UUID read from the buffer

    Example

    const buffer = Buffer.from([ 0xC0, 0x9B, 0x74, 0xB4, 0x8C, 0x14, 0x44, 0xCB, 0xB5, 0x67, 0x65, 0x76, 0xA2, 0xDA, 0xF1, 0xF9 ]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readUUID();
    console.log(decoded); // UUID <c09b74b4-8c14-44cb-b567-6576a2daf1f9>

    Returns UUID

  • Read a varint from the buffer

    Returns

    The varint value read from the buffer

    Example

    const buffer = Buffer.from([0xac, 0x02]);
    const buf = new BufWrapper(buffer);
    const decoded = buf.readVarInt();
    console.log(decoded); // 300

    Returns number

  • Write a boolean to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeBoolean(true);
    console.log(buf.buffer); // <Buffer 01>

    Parameters

    • value: boolean

      The value to write (boolean)

    Returns void

  • Write raw bytes to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeBytes([ 0x01, 0x02, 0x03 ]);
    console.log(buf.buffer); // <Buffer 01 02 03>

    Parameters

    • value: number[] | Buffer

      The value to write (a buffer or an array of bytes)

    Returns void

  • Write a double to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeDouble(42.42);
    console.log(buf.buffer); // <Buffer 40 45 35 c2 8f 5c 28 f6>

    Parameters

    • value: number

      The value to write (number)

    Returns void

  • Write a float to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeFloat(12.34);
    console.log(buf.buffer); // <Buffer 41 45 70 a4>

    Parameters

    • value: number

      The value to write (number)

    Returns void

  • Write an integer to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeInt(123);
    console.log(buf.buffer); // <Buffer 00 00 00 7b>

    Parameters

    • value: number

      The value to write (number)

    Returns void

  • Write an array of ints to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeIntArray([1, 2, 3]);
    console.log(buf.buffer); // <Buffer 03 00 00 00 01 00 00 00 02 00 00 00 03>

    Parameters

    • value: number[]

      The value to write (number[])

    Returns void

  • Write a long to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeLong(123456789);
    console.log(buf.buffer); // <Buffer 00 00 00 00 07 5b cd 15>

    Parameters

    • value: number | bigint

      The value to write (number)

    Returns void

  • Write a short to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeShort(42);
    console.log(buf.buffer); // <Buffer 00 2a>

    Parameters

    • value: number

      The value to write (number)

    Returns void

  • Write a string to the buffer (will use the ut8 encoding)

    Example

    const buf = new BufWrapper();
    buf.writeString('Hello World');
    console.log(buf.buffer); // <Buffer 0b 48 65 6c 6c 6f 20 57 6f 72 6c 64>

    Parameters

    • value: string

      The value to write (string)

    Returns void

  • Write an array of strings to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeStringArray(['Hello', 'World']);
    console.log(buf.buffer); // <Buffer 02 05 48 65 6c 6c 6f 05 57 6f 72 6c 64>

    Parameters

    • value: string[]

      The value to write (string[])

    Returns void

  • Concat the given buffers into the main buffer if BufWrapperOptions#oneConcat is false or undefined. Otherwise, it will push the buffer to the BufWrapper#buffers array.

    Parameters

    • Rest ...buffers: Buffer[]

    Returns void

  • Write an UUID to the buffer

    Example

    import { parseUUID } from '@minecraft-js/uuid';
    const uuid = parseUUID('c09b74b4-8c14-44cb-b567-6576a2daf1f9');

    const buf = new BufWrapper();
    buf.writeUUID(uuid);
    console.log(buf.buffer); // <Buffer C0 9B 74 B4 8C 14 44 CB B5 67 65 76 A2 DA F1 F9>

    Parameters

    • value: UUID

      The value to write (UUID instance)

    Returns void

  • Write a varint to the buffer

    Example

    const buf = new BufWrapper();
    buf.writeVarInt(300);
    console.log(buf.buffer); // <Buffer ac 02>

    Parameters

    • value: number

      The value to write (number)

    Returns void

Generated using TypeDoc