Skip to content

Class CompoundTag

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

Top-level tag that acts as a container for other named tags.

public class CompoundTag : Tag, IEquatable<Tag>, ICloneable, IDictionary<string, Tag>, ICollection<KeyValuePair<string, Tag>>, IEnumerable<KeyValuePair<string, Tag>>, ICollection<Tag>, IEnumerable<Tag>, IEnumerable

Inheritance

objectTagCompoundTag

Implements

IEquatable<Tag>, ICloneable, IDictionary<string, Tag>, ICollection<KeyValuePair<string, Tag>>, IEnumerable<KeyValuePair<string, Tag>>, ICollection<Tag>, IEnumerable<Tag>, IEnumerable

Inherited Members

Tag.NoName, Tag.Type, Tag.Parent, Tag.Name, Tag.PrettyPrinted(StringBuilder, int, string), Tag.PrettyName, Tag.WriteJson(Utf8JsonWriter, bool), Tag.WriteJson(Stream, JsonWriterOptions?), Tag.WriteJsonAsync(Stream, JsonWriterOptions?), Tag.ToJson(JsonWriterOptions?), Tag.ToJsonString(bool, string), Tag.Equals(Tag?), Tag.Equals(object?), Tag.GetHashCode(), Tag.Clone(), Tag.Stringify(bool), Tag.StringifyName, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks

This along with the class define the structure of the NBT format. Children are not order-dependent, nor is order guaranteed. The closing does not require to be explicitly added, it will be added automatically during serialization.

Constructors

CompoundTag(string?)

Creates a new instance of the class.

public CompoundTag(string? name)

Parameters

name string?

The name of the tag, or null if tag has no name.

CompoundTag(string?, IEnumerable<Tag>)

Creates a new instance of the class.

public CompoundTag(string? name, IEnumerable<Tag> values)

Parameters

name string?

The name of the tag, or null if tag has no name.

values IEnumerable<Tag>

A collection objects that are children of this object.

Properties

Count

Removes all items from the .

public int Count { get; }

Property Value

int

Exceptions

NotSupportedException

The is read-only.

Keys

Gets an containing the keys of the .

public ICollection<string> Keys { get; }

Property Value

ICollection<string>

Values

Gets an containing the values in the .

public ICollection<Tag> Values { get; }

Property Value

ICollection<Tag>

this[string]

Gets or sets the element with the specified key.

public Tag this[string name] { get; set; }

Property Value

Tag

Exceptions

ArgumentNullException

key is null.

KeyNotFoundException

The property is retrieved and key is not found.

NotSupportedException

The property is set and the is read-only.

Methods

Add(string, Tag)

Adds an element with the provided key and value to the .

public void Add(string key, Tag value)

Parameters

key string

The object to use as the key of the element to add.

value Tag

The object to use as the value of the element to add.

Exceptions

ArgumentNullException

key is null.

ArgumentException

An element with the same key already exists in the .

NotSupportedException

The is read-only.

Add(Tag)

Adds an item to the .

public void Add(Tag value)

Parameters

value Tag

Exceptions

NotSupportedException

The is read-only.

Clear()

Removes all items from the .

public void Clear()

Exceptions

NotSupportedException

The is read-only.

Contains(Tag)

Determines whether the contains a specific value.

public bool Contains(Tag tag)

Parameters

tag Tag

Returns

bool

true if item is found in the ; otherwise, false.

ContainsKey(string)

Determines whether the contains an element with the specified key.

public bool ContainsKey(string key)

Parameters

key string

The key to locate in the .

Returns

bool

true if the contains an element with the key; otherwise, false.

Exceptions

ArgumentNullException

key is null.

CopyTo(Tag[], int)

Copies the elements of the to an , starting at a particular index.

public void CopyTo(Tag[] array, int arrayIndex)

Parameters

array Tag[]

The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing.

arrayIndex int

The zero-based index in array at which copying begins.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is less than 0.

ArgumentException

The number of elements in the source is greater than the available space from arrayIndex to the end of the destination array.

Find<TTag>(string, bool)

Searches the children of this tag, returning the first child with the specified name.

public TTag? Find<TTag>(string name, bool recursive = false) where TTag : Tag

Parameters

name string

The name of the tag to search for.

recursive bool

true to recursively search children, otherwise false to only search direct descendants.

Returns

TTag?

The first tag found with name, otherwise null if none was found.

Type Parameters

TTag

Get<TTag>(string)

public TTag Get<TTag>(string name) where TTag : Tag

Parameters

name string

Returns

TTag

Type Parameters

TTag

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<Tag> GetEnumerator()

Returns

IEnumerator<Tag>

An enumerator that can be used to iterate through the collection.

PrettyPrinted(string?)

Retrieves a “pretty-printed” multiline string representing the complete tree structure of the tag.

public string PrettyPrinted(string? indent = " ")

Parameters

indent string?

The prefix that will be applied to each indent-level of nested nodes in the tree structure.

Returns

string

The pretty-printed string.

PrettyPrinted(StringBuilder, int, string)

Writes this tag as a formatted string to the given buffer.

protected override void PrettyPrinted(StringBuilder buffer, int level, string indent)

Parameters

buffer StringBuilder

A instance to write to.

level int

The current indent depth to write at.

indent string

The string to use for indents.

Remove(string)

Removes the element with the specified key from the .

public bool Remove(string key)

Parameters

key string

The key of the element to remove.

Returns

bool

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original .

Exceptions

ArgumentNullException

key is null.

NotSupportedException

The is read-only.

Remove(Tag)

Removes the first occurrence of a specific object from the .

public bool Remove(Tag item)

Parameters

item Tag

The object to remove from the .

Returns

bool

true if item was successfully removed from the ; otherwise, false. This method also returns false if item is not found in the original .

Exceptions

NotSupportedException

The is read-only.

Stringify(bool)

Gets the string representation of this NBT tag (SNBT).

public override string Stringify(bool named = true)

Parameters

named bool

Flag indicating if the name of the tag should be written.

Returns

string

This NBT tag in SNBT format.

See Also

https://minecraft.fandom.com/wiki/NBT_format#SNBT_format

Stringify(bool, bool)

Gets the string representation of this NBT tag (SNBT).

public string Stringify(bool topLevel, bool named)

Parameters

topLevel bool

Flag indicating if this is the top-level tag that should be wrapped in braces.

named bool

Flag indicating if the name of the NBT should be written.

Returns

string

This NBT tag in SNBT format.

See Also

https://minecraft.fandom.com/wiki/NBT_format#SNBT_format

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

TryGetValue(string, out Tag)

Gets the value associated with the specified key.

public bool TryGetValue(string key, out Tag value)

Parameters

key string

The key whose value to get.

value Tag

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the object that implements contains an element with the specified key; otherwise, false.

Exceptions

ArgumentNullException

key is null.

TryGetValue<TTag>(string, out TTag)

Gets the value associated with the specified key.

public bool TryGetValue<TTag>(string key, out TTag value) where TTag : Tag

Parameters

key string

The key whose value to get.

value TTag

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the object that implements contains an element with the specified key; otherwise, false.

Type Parameters

TTag

Exceptions

ArgumentNullException

key is null.

WriteJson(Utf8JsonWriter, bool)

Uses the provided writer to write the NBT tag in JSON format.

protected override void WriteJson(Utf8JsonWriter writer, bool named = true)

Parameters

writer Utf8JsonWriter

A JSON writer instance.

named bool

Flag indicating if this object’s name should be written as a property name, or false when it is a child of , in which case it should be written as a JSON array element.