Class ListTag<T extends Tag>

java.lang.Object
com.viaversion.nbt.tag.ListTag<T>
All Implemented Interfaces:
Tag, Iterable<T>
Direct Known Subclasses:
MixedListTag

public class ListTag<T extends Tag> extends Object implements Tag, Iterable<T>
A tag containing a list of tags.
See Also:
  • Field Details

  • Constructor Details

    • ListTag

      @Deprecated public ListTag()
      Deprecated.
      use MixedListTag if you need to mix element types
      Creates an empty list tag and no defined type.
    • ListTag

      public ListTag(Class<T> type)
      Creates an empty list tag and type.
      Parameters:
      type - Tag type of the list.
    • ListTag

      public ListTag(List<T> value)
      Creates a list tag and value. The list tag's type will be set to that of the first tag being added, or null if the given list is empty.
      Parameters:
      value - The value of the tag.
      Throws:
      IllegalArgumentException - If all tags in the list are not of the same type.
  • Method Details

    • of

      public static ListTag<?> of(List<Tag> value)
      Creates a list tag, possibly of mixed types.
      Parameters:
      value - list of tags, can be mixed
      Returns:
      a new list tag
    • read

      public static ListTag<?> read(DataInput in, TagLimiter tagLimiter, int nestingLevel) throws IOException
      Throws:
      IOException
    • getValue

      public List<T> getValue()
      Description copied from interface: Tag
      Returns the value of this tag.
      Specified by:
      getValue in interface Tag
      Returns:
      value of this tag
    • asRawString

      public String asRawString()
      Description copied from interface: Tag
      Returns the raw string representation of the value of this tag. For SNBT, use SNBT.serialize(Tag).
      Specified by:
      asRawString in interface Tag
      Returns:
      raw string representation of the value of this tag
    • setValue

      public void setValue(List<T> value)
      Sets the value of this tag. The list tag's type will be set to that of the first tag being added, or null if the given list is empty.
      Parameters:
      value - New value of this tag.
      Throws:
      IllegalArgumentException - If all tags in the list are not of the same type.
    • getElementType

      @Nullable public @Nullable Class<? extends Tag> getElementType()
      Gets the element type of the ListTag.
      Returns:
      The ListTag's element type, or null if the list does not yet have a defined type.
    • add

      public boolean add(T tag) throws IllegalArgumentException
      Adds a tag to this list tag. If the list does not yet have a type, it will be set to the type of the tag being added.
      Parameters:
      tag - Tag to add. Should not be null.
      Returns:
      If the list was changed as a result.
      Throws:
      IllegalArgumentException - If the tag's type differs from the list tag's type.
    • remove

      public boolean remove(T tag)
      Removes a tag from this list tag.
      Parameters:
      tag - Tag to remove.
      Returns:
      If the list contained the tag.
    • get

      public T get(int index)
      Gets the tag at the given index of this list tag.
      Parameters:
      index - Index of the tag.
      Returns:
      The tag at the given index.
    • set

      public T set(int index, T tag)
      Sets the tag at the given index of this list tag.
      Parameters:
      index - Index of the tag.
      tag - New tag to set.
      Returns:
      The old tag at the given index.
    • remove

      public T remove(int index)
      Removes the tag at the given index of this list tag.
      Parameters:
      index - Index of the tag.
      Returns:
      The removed tag at the given index.
    • size

      public int size()
      Gets the number of tags in this list tag.
      Returns:
      The size of this list tag.
    • isEmpty

      public boolean isEmpty()
    • stream

      public Stream<T> stream()
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T extends Tag>
    • write

      public void write(DataOutput out) throws IOException
      Description copied from interface: Tag
      Writes this tag to an output stream.
      Specified by:
      write in interface Tag
      Parameters:
      out - data output to write to
      Throws:
      IOException - if an I/O error occurs
    • copy

      public ListTag<T> copy()
      Description copied from interface: Tag
      Returns a copy of this tag.
      Specified by:
      copy in interface Tag
      Returns:
      a copy of this tag
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getTagId

      public int getTagId()
      Description copied from interface: Tag
      Returns the NBT tag id of this tag type, used in I/O.
      Specified by:
      getTagId in interface Tag
      Returns:
      ID of the tag this class represents
    • toString

      public String toString()
      Overrides:
      toString in class Object