Skip to content

NbtCompound

Namespace Mc.Scripting.Nbt · Kind class (sealed)

public sealed class NbtCompound : NbtTag, IEnumerable, IEnumerable<KeyValuePair<string, NbtTag>>

A compound: named tags (keys are unique; the enumeration order is the insertion order).

Member Description
NbtCompound()
public NbtCompound()
Member Description
int Count { get; } Number of entries.
IEnumerable<string> Keys { get; } The keys.
override NbtType Type { get; }
public int Count { get; }

Number of entries.

public IEnumerable<string> Keys { get; }

The keys.

public override NbtType Type { get; }
Member Description
NbtTag this[string key] { get; set; } Gets or sets an entry.
public NbtTag this[string key] { get; set; }

Gets or sets an entry.

Parameters

Name Type Description
key string The key.

Throws

  • System.Collections.Generic.KeyNotFoundException: Reading a key that does not exist.
Member Description
void Clear() Removes every entry.
override NbtTag Clone()
bool Contains(string key) Whether a key exists.
T? Get<T>(string key) where T : NbtTag Gets an entry of a given type.
bool GetBool(string key, bool fallback = false) Reads a boolean (a non-zero numeric entry).
NbtCompound? GetCompound(string key) Reads a compound entry.
double GetDouble(string key, double fallback = 0) Reads a numeric entry as a double.
IEnumerator<KeyValuePair<string, NbtTag>> GetEnumerator()
int GetInt(string key, int fallback = 0) Reads a numeric entry as an int.
NbtList? GetList(string key) Reads a list entry.
long GetLong(string key, long fallback = 0) Reads a numeric entry as a long (any integer or float type; floats are truncated).
string GetString(string key, string fallback = "") Reads a string entry.
NbtCompound Merge(NbtCompound other) Deep-merges another compound into this one: nested compounds merge, everything else is copied (like /data merge).
NbtCompound Put(string key, NbtTag tag) Adds or replaces an entry.
NbtCompound PutBool(string key, bool value) Sets a byte entry (the game’s boolean).
NbtCompound PutByte(string key, sbyte value) Sets a byte entry.
NbtCompound PutDouble(string key, double value) Sets a double entry.
NbtCompound PutFloat(string key, float value) Sets a float entry.
NbtCompound PutInt(string key, int value) Sets an int entry.
NbtCompound PutLong(string key, long value) Sets a long entry.
NbtCompound PutShort(string key, short value) Sets a short entry.
NbtCompound PutString(string key, string value) Sets a string entry.
bool Remove(string key) Removes an entry.
bool TryGet(string key, out NbtTag tag) Gets an entry.
public void Clear()

Removes every entry.

public override NbtTag Clone()

Returns NbtTag

public bool Contains(string key)

Whether a key exists.

Parameters

Name Type Description
key string The key.

Returns bool: True when present.

public T? Get<T>(string key) where T : NbtTag

Gets an entry of a given type.

Type parameters

Parameters

Name Type Description
key string The key.

Returns T?: The tag, or null when the key is missing or has another type.

public bool GetBool(string key, bool fallback = false)

Reads a boolean (a non-zero numeric entry).

Parameters

Name Type Description
key string The key.
fallback bool = false What to return when the key is missing.

Returns bool: The value.

public NbtCompound? GetCompound(string key)

Reads a compound entry.

Parameters

Name Type Description
key string The key.

Returns NbtCompound?: The compound, or null.

public double GetDouble(string key, double fallback = 0)

Reads a numeric entry as a double.

Parameters

Name Type Description
key string The key.
fallback double = 0 What to return when the key is missing or not numeric.

Returns double: The value.

public IEnumerator<KeyValuePair<string, NbtTag>> GetEnumerator()

Returns IEnumerator<KeyValuePair<string, NbtTag>>

public int GetInt(string key, int fallback = 0)

Reads a numeric entry as an int.

Parameters

Name Type Description
key string The key.
fallback int = 0 What to return when the key is missing or not numeric.

Returns int: The value.

public NbtList? GetList(string key)

Reads a list entry.

Parameters

Name Type Description
key string The key.

Returns NbtList?: The list, or null.

public long GetLong(string key, long fallback = 0)

Reads a numeric entry as a long (any integer or float type; floats are truncated).

Parameters

Name Type Description
key string The key.
fallback long = 0 What to return when the key is missing or not numeric.

Returns long: The value.

public string GetString(string key, string fallback = "")

Reads a string entry.

Parameters

Name Type Description
key string The key.
fallback string = "" What to return when the key is missing or not a string.

Returns string: The value.

public NbtCompound Merge(NbtCompound other)

Deep-merges another compound into this one: nested compounds merge, everything else is copied (like /data merge).

Parameters

Name Type Description
other NbtCompound The compound to merge in.

Returns NbtCompound: This compound.

public NbtCompound Put(string key, NbtTag tag)

Adds or replaces an entry.

Parameters

Name Type Description
key string The key.
tag NbtTag The tag.

Returns NbtCompound: This compound, for chaining.

public NbtCompound PutBool(string key, bool value)

Sets a byte entry (the game’s boolean).

Parameters

Name Type Description
key string The key.
value bool The value.

Returns NbtCompound: This compound.

public NbtCompound PutByte(string key, sbyte value)

Sets a byte entry.

Parameters

Name Type Description
key string The key.
value sbyte The value.

Returns NbtCompound: This compound.

public NbtCompound PutDouble(string key, double value)

Sets a double entry.

Parameters

Name Type Description
key string The key.
value double The value.

Returns NbtCompound: This compound.

public NbtCompound PutFloat(string key, float value)

Sets a float entry.

Parameters

Name Type Description
key string The key.
value float The value.

Returns NbtCompound: This compound.

public NbtCompound PutInt(string key, int value)

Sets an int entry.

Parameters

Name Type Description
key string The key.
value int The value.

Returns NbtCompound: This compound.

public NbtCompound PutLong(string key, long value)

Sets a long entry.

Parameters

Name Type Description
key string The key.
value long The value.

Returns NbtCompound: This compound.

public NbtCompound PutShort(string key, short value)

Sets a short entry.

Parameters

Name Type Description
key string The key.
value short The value.

Returns NbtCompound: This compound.

public NbtCompound PutString(string key, string value)

Sets a string entry.

Parameters

Name Type Description
key string The key.
value string The value.

Returns NbtCompound: This compound.

public bool Remove(string key)

Removes an entry.

Parameters

Name Type Description
key string The key.

Returns bool: True when it existed.

public bool TryGet(string key, out NbtTag tag)

Gets an entry.

Parameters

Name Type Description
key string The key.
tag out NbtTag The tag when present.

Returns bool: Whether the key exists.

NbtList · NbtTag · NbtType