Class CompoundTag

java.lang.Object
com.viaversion.nbt.tag.CompoundTag
All Implemented Interfaces:
Tag, Iterable<Map.Entry<String,Tag>>

public final class CompoundTag extends Object implements Tag, Iterable<Map.Entry<String,Tag>>
A compound tag containing other tags.
  • Field Details

  • Constructor Details

    • CompoundTag

      public CompoundTag()
      Creates a tag.
    • CompoundTag

      public CompoundTag(Map<String,Tag> value)
      Creates a tag.
      Parameters:
      value - The value of the tag.
    • CompoundTag

      public CompoundTag(LinkedHashMap<String,Tag> value)
      Creates a tag without wrapping the map.
      Parameters:
      value - The value of the tag.
  • Method Details

    • read

      public static CompoundTag read(DataInput in, TagLimiter tagLimiter, int nestingLevel) throws IOException
      Throws:
      IOException
    • getValue

      public Map<String,Tag> 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(Map<String,Tag> value)
      Sets the value of this tag.
      Parameters:
      value - New value of this tag.
    • setValue

      public void setValue(LinkedHashMap<String,Tag> value)
      Sets the value of this tag without wrapping the map.
      Parameters:
      value - New value of this tag.
    • isEmpty

      public boolean isEmpty()
      Checks whether the compound tag is empty.
      Returns:
      Whether the compound tag is empty.
    • contains

      public boolean contains(String tagName)
      Checks whether the compound tag contains a tag.
      Parameters:
      tagName - Name of the tag to check for.
      Returns:
      Whether the compound tag contains a tag.
    • get

      @Nullable public @Nullable Tag get(String tagName)
      Returns a tag by name if present.

      This will have its generic removed and instead return a raw tag in the future.

      Parameters:
      tagName - key of the tag
      Returns:
      tag if present, else null
      See Also:
    • getUnchecked

      @Nullable public <T extends Tag> T getUnchecked(String tagName)
      Returns a tag by name if present.
      Type Parameters:
      T - type of the tag
      Parameters:
      tagName - key of the tag
      Returns:
      tag if present, else null
    • getStringTag

      @Nullable public @Nullable StringTag getStringTag(String tagName)
    • getCompoundTag

      @Nullable public @Nullable CompoundTag getCompoundTag(String tagName)
    • getListTag

      @Nullable public @Nullable ListTag<?> getListTag(String tagName)
    • getListTag

      @Nullable public <T extends Tag> @Nullable ListTag<T> getListTag(String tagName, Class<T> type)
    • getNumberListTag

      @Nullable public @Nullable ListTag<? extends NumberTag> getNumberListTag(String tagName)
    • getIntTag

      @Nullable public @Nullable IntTag getIntTag(String tagName)
    • getLongTag

      @Nullable public @Nullable LongTag getLongTag(String tagName)
    • getShortTag

      @Nullable public @Nullable ShortTag getShortTag(String tagName)
    • getByteTag

      @Nullable public @Nullable ByteTag getByteTag(String tagName)
    • getFloatTag

      @Nullable public @Nullable FloatTag getFloatTag(String tagName)
    • getDoubleTag

      @Nullable public @Nullable DoubleTag getDoubleTag(String tagName)
    • getNumberTag

      @Nullable public @Nullable NumberTag getNumberTag(String tagName)
    • getByteArrayTag

      @Nullable public @Nullable ByteArrayTag getByteArrayTag(String tagName)
    • getIntArrayTag

      @Nullable public @Nullable IntArrayTag getIntArrayTag(String tagName)
    • getLongArrayTag

      @Nullable public @Nullable LongArrayTag getLongArrayTag(String tagName)
    • getString

      @Nullable public @Nullable String getString(String tagName)
    • getString

      @Nullable public @Nullable String getString(String tagName, @Nullable @Nullable String def)
    • getInt

      public int getInt(String tagName)
    • getInt

      public int getInt(String tagName, int def)
    • getLong

      public long getLong(String tagName)
    • getLong

      public long getLong(String tagName, long def)
    • getShort

      public short getShort(String tagName)
    • getShort

      public short getShort(String tagName, short def)
    • getByte

      public byte getByte(String tagName)
    • getByte

      public byte getByte(String tagName, byte def)
    • getFloat

      public float getFloat(String tagName)
    • getFloat

      public float getFloat(String tagName, float def)
    • getDouble

      public double getDouble(String tagName)
    • getDouble

      public double getDouble(String tagName, double def)
    • getBoolean

      public boolean getBoolean(String tagName)
    • getBoolean

      public boolean getBoolean(String tagName, boolean def)
    • getIntStrict

      public int getIntStrict(String tagName)
      Returns the value of the int tag if the tag exists and is an IntTag, or 0. Unlike getInt(String), this does not get the int value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      int if present, or 0
      See Also:
    • getIntStrict

      public int getIntStrict(String tagName, int def)
      Returns the value of the int tag if the tag exists and is an IntTag, or def. Unlike getInt(String, int), this does not get the int value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      int if present, or def
      See Also:
    • getLongStrict

      public long getLongStrict(String tagName)
      Returns the value of the long tag if the tag exists and is a LongTag, or 0L. Unlike getLong(String), this does not get the long value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      long if present, or 0L
      See Also:
    • getLongStrict

      public long getLongStrict(String tagName, long def)
      Returns the value of the long tag if the tag exists and is a LongTag, or def. Unlike getLong(String, long), this does not get the long value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      long if present, or def
      See Also:
    • getShortStrict

      public short getShortStrict(String tagName)
      Returns the value of the short tag if the tag exists and is a ShortTag, or 0. Unlike getShort(String), this does not get the short value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      short if present, or 0
      See Also:
    • getShortStrict

      public short getShortStrict(String tagName, short def)
      Returns the value of the short tag if the tag exists and is a ShortTag, or def. Unlike getShort(String, short), this does not get the short value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      short if present, or def
      See Also:
    • getByteStrict

      public byte getByteStrict(String tagName)
      Returns the value of the byte tag if the tag exists and is a ByteTag, or 0. Unlike getByte(String), this does not get the byte value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      byte if present, or 0
      See Also:
    • getByteStrict

      public byte getByteStrict(String tagName, byte def)
      Returns the value of the byte tag if the tag exists and is a ByteTag, or def. Unlike getByte(String, byte), this does not get the byte value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      byte if present, or def
      See Also:
    • getFloatStrict

      public float getFloatStrict(String tagName)
      Returns the value of the float tag if the tag exists and is a FloatTag, or 0F. Unlike getFloat(String), this does not get the float value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      float if present, or 0F
      See Also:
    • getFloatStrict

      public float getFloatStrict(String tagName, float def)
      Returns the value of the float tag if the tag exists and is a FloatTag, or def. Unlike getFloat(String, float), this does not get the float value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      float if present, or def
      See Also:
    • getDoubleStrict

      public double getDoubleStrict(String tagName)
      Returns the value of the double tag if the tag exists and is a DoubleTag, or 0D. Unlike getDouble(String), this does not get the double value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      double if present, or 0D
      See Also:
    • getDoubleStrict

      public double getDoubleStrict(String tagName, double def)
      Returns the value of the double tag if the tag exists and is a DoubleTag, or def. Unlike getDouble(String, double), this does not get the double value from other number tags.
      Parameters:
      tagName - tag name
      Returns:
      double if present, or def
      See Also:
    • put

      @Nullable public @Nullable Tag put(String tagName, Tag tag)
      Puts the tag into this compound tag.

      This will have its generic removed and instead return a raw tag in the future.

      Parameters:
      tagName - Name of the tag.
      tag - Tag to put into this compound tag.
      Returns:
      The previous tag associated with its name, or null if there wasn't one.
    • putString

      public void putString(String tagName, String value)
    • putByte

      public void putByte(String tagName, byte value)
    • putInt

      public void putInt(String tagName, int value)
    • putShort

      public void putShort(String tagName, short value)
    • putLong

      public void putLong(String tagName, long value)
    • putFloat

      public void putFloat(String tagName, float value)
    • putDouble

      public void putDouble(String tagName, double value)
    • putBoolean

      public void putBoolean(String tagName, boolean value)
    • putAll

      public void putAll(CompoundTag compoundTag)
    • remove

      @Nullable public @Nullable Tag remove(String tagName)
      Removes a tag from this compound tag.

      This will have its generic removed and instead return a raw tag in the future.

      Parameters:
      tagName - Name of the tag to remove.
      Returns:
      The removed tag.
      See Also:
    • removeUnchecked

      @Nullable public <T extends Tag> T removeUnchecked(String tagName)
      Removes a tag from this compound tag.
      Type Parameters:
      T - Type of tag to remove.
      Parameters:
      tagName - Name of the tag to remove.
      Returns:
      The removed tag.
    • keySet

      public Set<String> keySet()
      Gets a set of keys in this compound tag.
      Returns:
      The compound tag's key set.
    • values

      public Collection<Tag> values()
      Gets a collection of tags in this compound tag.
      Returns:
      This compound tag's tags.
    • entrySet

      public Set<Map.Entry<String,Tag>> entrySet()
      Gets the entry set of this compound tag.
      Returns:
      The compound tag's entry set.
    • size

      public int size()
      Gets the number of tags in this compound tag.
      Returns:
      This compound tag's size.
    • clear

      public void clear()
      Clears all tags from this compound tag.
    • iterator

      public Iterator<Map.Entry<String,Tag>> iterator()
      Specified by:
      iterator in interface Iterable<Map.Entry<String,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
    • equals

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

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

      public CompoundTag copy()
      Description copied from interface: Tag
      Returns a copy of this tag.
      Specified by:
      copy in interface Tag
      Returns:
      a copy of this tag
    • 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