Skip to content

Struct MinecraftBuffer

Namespace: Void.Minecraft.Buffers
Assembly: Void.Minecraft.dll

public ref struct MinecraftBuffer

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Constructors

MinecraftBuffer()

public MinecraftBuffer()

MinecraftBuffer(Span<byte>)

public MinecraftBuffer(Span<byte> memory)

Parameters

memory Span<byte>

MinecraftBuffer(ReadOnlySpan<byte>)

public MinecraftBuffer(ReadOnlySpan<byte> span)

Parameters

span ReadOnlySpan<byte>

MinecraftBuffer(ReadOnlySequence<byte>)

public MinecraftBuffer(ReadOnlySequence<byte> sequence)

Parameters

sequence ReadOnlySequence<byte>

MinecraftBuffer(MemoryStream)

public MinecraftBuffer(MemoryStream memoryStream)

Parameters

memoryStream MemoryStream

Properties

HasData

public readonly bool HasData { get; }

Property Value

bool

Length

public readonly long Length { get; }

Property Value

long

Position

public readonly long Position { get; }

Property Value

long

Methods

CopyAsBufferSpan(bool)

public BufferSpan CopyAsBufferSpan(bool read = false)

Parameters

read bool

Returns

BufferSpan

Dump()

public string Dump()

Returns

string

DumpBytes()

public ReadOnlySpan<byte> DumpBytes()

Returns

ReadOnlySpan<byte>

DumpHex()

public string DumpHex()

Returns

string

GetVarIntSize(int)

public static int GetVarIntSize(int value)

Parameters

value int

Returns

int

Read(long)

public ReadOnlySpan<byte> Read(long length)

Parameters

length long

Returns

ReadOnlySpan<byte>

ReadBoolean()

public bool ReadBoolean()

Returns

bool

ReadComponent(bool)

public Component ReadComponent(bool asNbt = true)

Parameters

asNbt bool

Returns

Component

ReadDouble()

public double ReadDouble()

Returns

double

ReadFloat()

public float ReadFloat()

Returns

float

ReadInt()

public int ReadInt()

Returns

int

ReadJsonString()

public JsonNode ReadJsonString()

Returns

JsonNode

ReadLong()

public long ReadLong()

Returns

long

ReadProperty()

public Property ReadProperty()

Returns

Property

ReadPropertyArray()

public Property[] ReadPropertyArray()

Returns

Property[]

ReadShort()

public short ReadShort()

Returns

short

ReadString(int)

Reads a Minecraft protocol string encoded as a VarInt byte length followed by UTF-8 bytes.

public string ReadString(int maxLength = 32767)

Parameters

maxLength int

Maximum allowed character count for the decoded string.

Returns

string

The decoded UTF-8 string. The returned value is never null.

Examples

var text = buffer.ReadString(16);

Remarks

This method validates the decoded character length, not the encoded byte length. If maxLength is less than or equal to 0, length validation is skipped.

The buffer read position advances by the size of the string length prefix and payload bytes.

Exceptions

InvalidOperationException

The length prefix is not a valid Minecraft VarInt value.

IndexOutOfRangeException

The decoded string length exceeds maxLength.

ReadOnlyException

Propagated from the underlying buffer implementation in unsupported scenarios.

See Also

MinecraftBuffer.ReadVarInt()

ReadTag(bool)

public NbtTag ReadTag(bool readName = true)

Parameters

readName bool

Returns

NbtTag

ReadToEnd()

public ReadOnlySpan<byte> ReadToEnd()

Returns

ReadOnlySpan<byte>

ReadUnsignedByte()

public byte ReadUnsignedByte()

Returns

byte

ReadUnsignedShort()

public ushort ReadUnsignedShort()

Returns

ushort

ReadUuid()

public Uuid ReadUuid()

Returns

Uuid

ReadUuidAsIntArray()

public Uuid ReadUuidAsIntArray()

Returns

Uuid

ReadVarInt()

public int ReadVarInt()

Returns

int

ReadVarLong()

public long ReadVarLong()

Returns

long

ReadVarShort()

public int ReadVarShort()

Returns

int

Reset()

public void Reset()

Seek(long)

public void Seek(long offset)

Parameters

offset long

Seek(long, SeekOrigin)

public void Seek(long offset, SeekOrigin origin)

Parameters

offset long

origin SeekOrigin

Write(scoped ReadOnlySpan<byte>)

public void Write(scoped ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

Write(Stream)

public void Write(Stream stream)

Parameters

stream Stream

WriteBoolean(bool)

public void WriteBoolean(bool value)

Parameters

value bool

WriteComponent(Component, bool, bool)

public void WriteComponent(Component value, bool asNbt = true, bool writeNbtName = false)

Parameters

value Component

asNbt bool

writeNbtName bool

WriteDouble(double)

public void WriteDouble(double value)

Parameters

value double

WriteFloat(float)

public void WriteFloat(float value)

Parameters

value float

WriteInt(int)

public void WriteInt(int value)

Parameters

value int

WriteJsonString(JsonNode, JsonSerializerOptions?)

public void WriteJsonString(JsonNode node, JsonSerializerOptions? jsonSerializerOptions = null)

Parameters

node JsonNode

jsonSerializerOptions JsonSerializerOptions?

WriteLong(long)

public void WriteLong(long value)

Parameters

value long

WriteProperty(Property)

public void WriteProperty(Property value)

Parameters

value Property

WritePropertyArray(Property[]?)

Writes a profile property array using Minecraft’s length-prefixed format.

public void WritePropertyArray(Property[]? value)

Parameters

value Property[]?

The properties to serialize. When null, an empty array is written.

Examples

buffer.WritePropertyArray(profile.Properties);

Remarks

This method writes a VarInt element count first, then serializes each by calling .

Passing null is equivalent to passing [], so the written count is 0.

Exceptions

InvalidDataException

Propagated from when a property is marked as signed but has a missing or whitespace signature.

See Also

MinecraftBuffer.ReadPropertyArray()

WriteShort(short)

public void WriteShort(short value)

Parameters

value short

WriteString(ReadOnlySpan<char>)

Writes a Minecraft protocol string as UTF-8 bytes prefixed by its byte length encoded as VarInt.

public void WriteString(ReadOnlySpan<char> value)

Parameters

value ReadOnlySpan<char>

The characters to encode and write.

Examples

buffer.WriteString("minecraft:stone");

Remarks

The method writes the UTF-8 byte count first, then writes the encoded bytes. An empty span writes a zero length prefix and no payload bytes.

This method mutates the buffer by advancing the current position and appending or overwriting data depending on the backing storage.

Exceptions

ReadOnlyException

The underlying backing buffer is read-only.

InternalBufferOverflowException

The target writable span does not have enough capacity for the encoded payload.

See Also

MinecraftBuffer.WriteVarInt(int)

WriteTag(NbtTag, bool)

public void WriteTag(NbtTag value, bool writeName = true)

Parameters

value NbtTag

writeName bool

WriteUnsignedByte(byte)

public void WriteUnsignedByte(byte value)

Parameters

value byte

WriteUnsignedShort(ushort)

public void WriteUnsignedShort(ushort value)

Parameters

value ushort

WriteUuid(Uuid)

public void WriteUuid(Uuid value)

Parameters

value Uuid

WriteUuidAsIntArray(Uuid)

public void WriteUuidAsIntArray(Uuid value)

Parameters

value Uuid

WriteVarInt(int)

public void WriteVarInt(int value)

Parameters

value int

WriteVarLong(long)

public void WriteVarLong(long value)

Parameters

value long

WriteVarShort(int)

public void WriteVarShort(int value)

Parameters

value int