Skip to content

Class NbtTag

Namespace: Void.Minecraft.Nbt
Assembly: Void.Minecraft.dll

Provides the common API for tags in the Named Binary Tag data model.

public abstract record NbtTag : IEquatable<NbtTag>

Inheritance

objectNbtTag

Derived

NbtByte, NbtByteArray, NbtCompound, NbtDouble, NbtEnd, NbtFloat, NbtInt, NbtIntArray, NbtList, NbtLong, NbtLongArray, NbtShort, NbtString, NbtTag<T>

Implements

IEquatable<NbtTag>

Inherited Members

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

Properties

Name

Gets or sets the optional binary tag name.

public string? Name { get; set; }

Property Value

string?

Type

Gets the NBT payload type.

public NbtTagType Type { get; }

Property Value

NbtTagType

Methods

AsJsonNode()

Serializes this tag to the library’s typed JSON representation.

public JsonNode AsJsonNode()

Returns

JsonNode

The JSON node.

AsStream(NbtFormatOptions, bool)

Serializes this tag to a seekable in-memory binary stream.

public MemoryStream AsStream(NbtFormatOptions formatOptions = NbtFormatOptions.BigEndian, bool writeName = true)

Parameters

formatOptions NbtFormatOptions

The byte-order and integer-encoding options.

writeName bool

Whether to include the tag-name field.

Returns

MemoryStream

A new stream positioned at zero.

Remarks

When writeName is false, is temporarily cleared and restored after serialization.

Parse(string)

Parses an SNBT compound or list.

public static NbtTag Parse(string data)

Parameters

data string

The SNBT text.

Returns

NbtTag

The parsed compound or list tag.

Exceptions

FormatException

The trimmed input does not begin with { or [, or contains invalid SNBT.

Parse(ReadOnlyMemory<byte>, out NbtTag, bool, NbtFormatOptions)

Parses one binary NBT tag.

public static long Parse(ReadOnlyMemory<byte> data, out NbtTag result, bool readName = true, NbtFormatOptions formatOptions = NbtFormatOptions.BigEndian)

Parameters

data ReadOnlyMemory<byte>

Array-backed binary NBT data.

result NbtTag

The parsed tag.

readName bool

Whether the root tag includes a name field.

formatOptions NbtFormatOptions

The byte-order and integer-encoding options.

Returns

long

The stream position after the parsed tag.

Exceptions

ArgumentException

data is not backed by an accessible array.

Parse<T>(ReadOnlyMemory<byte>, out T, bool, NbtFormatOptions)

Parses one binary NBT tag and requires a specific tag type.

public static long Parse<T>(ReadOnlyMemory<byte> data, out T result, bool readName = true, NbtFormatOptions formatOptions = NbtFormatOptions.BigEndian) where T : NbtTag

Parameters

data ReadOnlyMemory<byte>

Array-backed binary NBT data.

result T

The parsed tag.

readName bool

Whether the root tag includes a name field.

formatOptions NbtFormatOptions

The byte-order and integer-encoding options.

Returns

long

The stream position after the parsed tag.

Type Parameters

T

The required tag type.

Exceptions

ArgumentException

data is not backed by an accessible array.

InvalidCastException

The parsed tag is not assignable to T.

ReadFrom<TBuffer>(ref TBuffer, bool)

Reads one binary NBT tag from the current buffer position.

public static NbtTag ReadFrom<TBuffer>(ref TBuffer buffer, bool readName = true) where TBuffer : struct, IMinecraftBuffer<TBuffer>, allows ref struct

Parameters

buffer TBuffer

The buffer to consume.

readName bool

Whether the root tag includes a name field.

Returns

NbtTag

The parsed tag.

Type Parameters

TBuffer

The Minecraft buffer type.

Remarks

The implementation reads the remaining buffer data, then advances the original buffer only by the parsed tag length.

ToSnbt()

Serializes this tag as stringified NBT for derived tag implementations.

protected string ToSnbt()

Returns

string

The SNBT representation.

ToString()

Serializes this tag as stringified NBT.

public override string ToString()

Returns

string

The SNBT representation.

Operators

implicit operator NbtTag(Tag)

Converts a SharpNBT tag to its Void NBT representation.

public static implicit operator NbtTag(Tag tag)

Parameters

tag Tag

The SharpNBT tag.

Returns

NbtTag

The corresponding Void tag.

Exceptions

NotSupportedException

The concrete SharpNBT tag type is unsupported.

implicit operator Tag(NbtTag)

Converts a Void NBT tag to its SharpNBT representation.

public static implicit operator Tag(NbtTag tag)

Parameters

tag NbtTag

The Void tag.

Returns

Tag

The corresponding SharpNBT tag.

Exceptions

NotSupportedException

The concrete Void tag type is unsupported.