Class NbtList
- Namespace
- fNbt
- Assembly
- fNbt.dll
A tag containing a list of unnamed tags, all of the same kind.
public sealed class NbtList : NbtTag, ICloneable, IList<NbtTag>, ICollection<NbtTag>, IEnumerable<NbtTag>, IList, ICollection, IEnumerable
- Inheritance
-
NbtList
- Implements
- Inherited Members
Constructors
NbtList()
public NbtList()
NbtList(IEnumerable<NbtTag>)
Creates an unnamed NbtList with the given contents, and inferred ListType. If given tag array is empty, NbtTagType remains Unknown.
public NbtList(IEnumerable<NbtTag> tags)
Parameters
tagsIEnumerable<NbtTag>Collection of tags to insert into the list. All tags are expected to be of the same type. ListType is inferred from the first tag. List may be empty, but may not be
null.
Exceptions
- ArgumentNullException
tagsisnull.- ArgumentException
If given tags are of mixed types.
NbtList(IEnumerable<NbtTag>, NbtTagType)
public NbtList(IEnumerable<NbtTag> tags, NbtTagType givenListType)
Parameters
tagsIEnumerable<NbtTag>Collection of tags to insert into the list. All tags are expected to be of the same type (matching givenListType). List may be empty, but may not be
null.givenListTypeNbtTagTypeType of the list elements. May be Unknown (to infer type from the first element of tags).
Exceptions
- ArgumentNullException
tagsisnull.- ArgumentOutOfRangeException
givenListTypeis not a valid tag type.- ArgumentException
If given tags do not match
givenListTypeor are of mixed types; or a tag is named, already has a Parent, or appears more than once.
NbtList(string?)
public NbtList(string? tagName)
Parameters
tagNamestringName to assign to this tag. May be
null.
NbtList(string?, IEnumerable<NbtTag>)
Creates an NbtList with the given name and contents, and inferred ListType. If given tag array is empty, NbtTagType remains Unknown.
public NbtList(string? tagName, IEnumerable<NbtTag> tags)
Parameters
tagNamestringName to assign to this tag. May be
null.tagsIEnumerable<NbtTag>Collection of tags to insert into the list. All tags are expected to be of the same type. ListType is inferred from the first tag. List may be empty, but may not be
null.
Exceptions
- ArgumentNullException
tagsisnull.- ArgumentException
If given tags are of mixed types.
NbtList(string?, IEnumerable<NbtTag>?, NbtTagType)
public NbtList(string? tagName, IEnumerable<NbtTag>? tags, NbtTagType givenListType)
Parameters
tagNamestringName to assign to this tag. May be
null.tagsIEnumerable<NbtTag>Collection of tags to insert into the list. All tags are expected to be of the same type (matching givenListType). May be empty or
null.givenListTypeNbtTagTypeType of the list elements. May be Unknown (to infer type from the first element of tags).
Exceptions
- ArgumentOutOfRangeException
givenListTypeis not a valid tag type.- ArgumentException
If given tags do not match
givenListTypeor are of mixed types; or a tag is named, already has a Parent, or appears more than once.
NbtList(string?, NbtTagType)
public NbtList(string? tagName, NbtTagType givenListType)
Parameters
tagNamestringName to assign to this tag. May be
null.givenListTypeNbtTagTypeType of the list elements. If givenListType is Unknown, ListType will be inferred from the first tag added to this NbtList.
Exceptions
- ArgumentOutOfRangeException
givenListTypeis not a valid tag type.
NbtList(NbtList)
Creates a deep copy of given NbtList.
public NbtList(NbtList other)
Parameters
Exceptions
- ArgumentNullException
otherisnull.- NbtFormatException
otheris nested deeper than 512 levels.
NbtList(NbtTagType)
Creates an unnamed NbtList with empty contents and an explicitly specified ListType. If ListType is Unknown, it will be inferred from the type of the first added tag. Otherwise, all tags added to this list are expected to be of the given type.
public NbtList(NbtTagType givenListType)
Parameters
givenListTypeNbtTagTypeType of the list elements. May be Unknown.
Exceptions
- ArgumentOutOfRangeException
givenListTypeis not a recognized tag type.
Properties
Count
Gets the number of tags contained in the NbtList.
public int Count { get; }
Property Value
this[int]
Gets or sets the tag at the specified index.
public override NbtTag this[int tagIndex] { get; set; }
Parameters
tagIndexintThe zero-based index of the tag to get or set.
Property Value
- NbtTag
The tag at the specified index.
Exceptions
- ArgumentOutOfRangeException
tagIndexis not a valid index in the NbtList.- ArgumentNullException
valueisnull.- ArgumentException
Given tag's type does not match the declared ListType; or it already has a Parent; or it is this list or one of its ancestors; or it is named.
ListType
Gets or sets the tag type of this list. All tags in this NbtTag must be of the same type. The type of an empty list is a constraint on what may be added, not part of its value: Unknown (a new list) and End (a loaded or parsed one) both let the first tag added set the type, and a declared type is enforced. Files store the declared type, or End for Unknown, readers accept any type for an empty list, SNBT carries none, and NbtComparer treats every empty list as equal to every other.
public NbtTagType ListType { get; set; }
Property Value
Exceptions
- ArgumentException
If the given NbtTagType does not match the type of existing list items (for non-empty lists).
- ArgumentOutOfRangeException
If the given NbtTagType is not a recognized tag type.
TagType
Type of this tag (List).
public override NbtTagType TagType { get; }
Property Value
Methods
Add(NbtTag)
Adds a tag to this NbtList.
public void Add(NbtTag newTag)
Parameters
Exceptions
- ArgumentNullException
newTagisnull.- ArgumentException
If
newTagdoes not match the declared ListType; or it already has a Parent; or it is this list or one of its ancestors; or it is named.
AddRange(IEnumerable<NbtTag>)
Adds all tags from the specified collection to the end of this NbtList.
public void AddRange(IEnumerable<NbtTag> newTags)
Parameters
newTagsIEnumerable<NbtTag>The collection whose elements should be added to this NbtList.
Exceptions
- ArgumentNullException
newTagsisnull.- ArgumentException
If given tags do not match the declared ListType or are of mixed types; or a tag is named, already has a Parent, is this list or one of its ancestors, or appears more than once.
Clear()
Removes all tags from this NbtList.
public void Clear()
Contains(NbtTag)
Determines whether this NbtList contains a specific tag.
public bool Contains(NbtTag item)
Parameters
Returns
CopyTo(NbtTag[], int)
Copies the tags of this NbtList to an array, starting at a particular array index.
public void CopyTo(NbtTag[] array, int arrayIndex)
Parameters
arrayNbtTag[]The one-dimensional array that is the destination of the tag copied from NbtList. The array must have zero-based indexing.
arrayIndexintThe zero-based index in array at which copying begins.
Exceptions
- ArgumentNullException
arrayisnull.- ArgumentOutOfRangeException
arrayIndex is less than 0.
- ArgumentException
Given array is multidimensional; arrayIndex is equal to or greater than the length of array; the number of tags in this NbtList is greater than the available space from arrayIndex to the end of the destination array; or type NbtTag cannot be cast automatically to the type of the destination array.
CreateMixed(params NbtTag[])
Creates an unnamed list from tags of any types, stored the way Minecraft stores a list of mixed types. Tags of one type other than compound make an ordinary list. Any other input makes a list of compounds in which every tag that is not a plain compound sits under an empty key, the wrapper compounds Minecraft 1.21.5 and later write to disk; a compound that already has that shape is wrapped again, so UnwrapMixed() gives every tag back as it was. A wrapped tag gets the empty name and its wrapper as Parent.
public static NbtList CreateMixed(params NbtTag[] elements)
Parameters
elementsNbtTag[]Tags to hold. Each must be unnamed and have no Parent.
Returns
- NbtList
The new list.
Exceptions
- ArgumentNullException
elementsor a tag in it isnull.- ArgumentException
A tag is named, already has a Parent, or appears more than once.
GetEnumerator()
Returns an enumerator that iterates through all tags in this NbtList.
public IEnumerator<NbtTag> GetEnumerator()
Returns
- IEnumerator<NbtTag>
An IEnumerator<NbtTag> that can be used to iterate through the list.
Get<T>(int)
Gets the tag at the specified index, cast to the requested type.
public T Get<T>(int tagIndex) where T : NbtTag
Parameters
tagIndexintThe zero-based index of the tag to get.
Returns
- T
The tag at the specified index.
Type Parameters
TType to cast the result to. Must derive from NbtTag.
Exceptions
- ArgumentOutOfRangeException
tagIndexis not a valid index in the NbtList.- InvalidCastException
If tag could not be cast to the desired tag.
IndexOf(NbtTag?)
Determines the index of a specific tag in this NbtList
public int IndexOf(NbtTag? tag)
Parameters
Returns
- int
The index of tag if found in the list; otherwise, -1.
Insert(int, NbtTag)
Inserts an item to this NbtList at the specified index.
public void Insert(int tagIndex, NbtTag newTag)
Parameters
tagIndexintThe zero-based index at which newTag should be inserted.
newTagNbtTagThe tag to insert into this NbtList.
Exceptions
- ArgumentOutOfRangeException
tagIndexis not a valid index in this NbtList.- ArgumentNullException
newTagisnull.- ArgumentException
newTagdoes not match the declared ListType; or it already has a Parent; or it is this list or one of its ancestors; or it is named.
Remove(NbtTag)
Removes the first occurrence of a specific NbtTag from this NbtList. Looks for exact object matches, not name matches.
public bool Remove(NbtTag tag)
Parameters
Returns
- bool
true if tag was successfully removed from this NbtList; otherwise, false. This method also returns false if tag is not found.
Exceptions
- ArgumentNullException
tagisnull.
RemoveAt(int)
Removes a tag at the specified index from this NbtList.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the NbtList.
ToArray()
Copies all tags in this NbtList to an array.
public NbtTag[] ToArray()
Returns
- NbtTag[]
Array of NbtTags.
ToArray<T>()
Copies all tags in this NbtList to an array, and casts it to the desired type.
public T[] ToArray<T>() where T : NbtTag
Returns
- T[]
Array of NbtTags cast to the desired type.
Type Parameters
Exceptions
- InvalidCastException
If contents of this list cannot be cast to the given type.
UnwrapMixed()
Returns the elements the way Minecraft reads them. In a list of compounds, a
compound holding one tag under an empty key stands for that tag, the wrapper form in
which Minecraft 1.21.5 and later store a list of mixed types; every other list returns
its elements as they are. One level of wrapping is removed. The tags are this list's
own, so Clone() one before adding it elsewhere; an unwrapped value
and its clone carry the empty name the wrapper gave them, which a list refuses until
Name is set to null.
public NbtTag[] UnwrapMixed()
Returns
- NbtTag[]
The elements, unwrapped.