Class NBTTag

java.lang.Object
xyz.gmitch215.socketmc.util.NBTTag
All Implemented Interfaces:
Serializable

public final class NBTTag extends Object implements Serializable
Represents a NBT tag.
See Also:
  • Constructor Details

    • NBTTag

      public NBTTag()
      Constructs a new, empty NBTTag.
    • NBTTag

      public NBTTag(@NotNull @NotNull Map<String,Object> tag)
      Constructs a new NBTTag.
      Parameters:
      tag - The tag.
  • Method Details

    • getTag

      @NotNull public @NotNull Map<String,Object> getTag()
      Gets an immutable copy of the full tag.
      Returns:
      The tag.
    • get

      Gets an object from the tag. If the object does not exist, null is returned.

      This supports dot notation, for example, "key.subkey.subsubkey".

      Parameters:
      key - The key to get.
      Returns:
      The object, or null if it does not exist.
    • set

      public void set(@NotNull @NotNull String key, @Nullable @Nullable Object value)
      Sets an object in the tag.
      Parameters:
      key - The key to set.
      value - The value to set. If null, will remove the key.
    • remove

      public void remove(@NotNull @NotNull String key)
      Removes an object from the tag.
      Parameters:
      key - The key to remove.
    • get

      Gets an object from the tag. If the object does not exist, the default value is returned.

      This supports dot notation, for example, "key.subkey.subsubkey".

      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The object, or the default value if it does not exist.
    • getObject

      @NotNull public <T> T getObject(@NotNull @NotNull String key, @NotNull @NotNull Class<T> clazz)
      Gets an object from the tag. If the object does not exist, null is returned.
      Type Parameters:
      T - The type of the object.
      Parameters:
      key - The key to get.
      clazz - The class of the object to cast to.
      Returns:
      The object, or null if it does not exist.
    • getObject

      @NotNull public <T> T getObject(@NotNull @NotNull String key, @NotNull @NotNull Class<T> clazz, T def)
      Gets an object from the tag. If the object does not exist, the default value is returned.
      Type Parameters:
      T - The type of the object.
      Parameters:
      key - The key to get.
      clazz - The class of the object to cast to.
      def - The default value.
      Returns:
      The object, or the default value if it does not exist.
    • getSection

      Gets a section from the tag. If the object does not exist, an empty map is returned.
      Parameters:
      key - The key to get.
      Returns:
      The section, or an empty map if it does not exist.
    • createSection

      public void createSection(@NotNull @NotNull String key)
      Creates a section in the tag. If it exists, it will be overwritten.
      Parameters:
      key - The key to create.
    • getString

      @NotNull public @NotNull String getString(@NotNull @NotNull String key)
      Gets a string from the tag. If the object does not exist, an empty string is returned.
      Parameters:
      key - The key to get.
      Returns:
      The string, or an empty string if it does not exist.
    • getString

      Gets a string from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The string, or the default value if it does not exist.
    • getBoolean

      public boolean getBoolean(@NotNull @NotNull String key)
      Gets a boolean from the tag. If the object does not exist, false is returned.
      Parameters:
      key - The key to get.
      Returns:
      The boolean, or false if it does not exist.
    • getBoolean

      public boolean getBoolean(@NotNull @NotNull String key, boolean def)
      Gets a boolean from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The boolean, or the default value if it does not exist.
    • getByte

      public byte getByte(@NotNull @NotNull String key)
      Gets a byte from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The byte, or 0 if it does not exist.
    • getByte

      public byte getByte(@NotNull @NotNull String key, byte def)
      Gets a byte from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The byte, or the default value if it does not exist.
    • getShort

      public short getShort(@NotNull @NotNull String key)
      Gets a short from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The short, or 0 if it does not exist.
    • getShort

      public short getShort(@NotNull @NotNull String key, short def)
      Gets a short from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The short, or the default value if it does not exist.
    • getInt

      public int getInt(@NotNull @NotNull String key)
      Gets an integer from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The integer, or 0 if it does not exist.
    • getInt

      public int getInt(@NotNull @NotNull String key, int def)
      Gets an integer from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The integer, or the default value if it does not exist.
    • getLong

      public long getLong(@NotNull @NotNull String key)
      Gets a long from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The long, or 0 if it does not exist.
    • getLong

      public long getLong(@NotNull @NotNull String key, long def)
      Gets a long from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The long, or the default value if it does not exist.
    • getFloat

      public float getFloat(@NotNull @NotNull String key)
      Gets a float from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The float, or 0 if it does not exist.
    • getFloat

      public float getFloat(@NotNull @NotNull String key, float def)
      Gets a float from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The float, or the default value if it does not exist.
    • getDouble

      public double getDouble(@NotNull @NotNull String key)
      Gets a double from the tag. If the object does not exist, 0 is returned.
      Parameters:
      key - The key to get.
      Returns:
      The double, or 0 if it does not exist.
    • getDouble

      public double getDouble(@NotNull @NotNull String key, double def)
      Gets a double from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The double, or the default value if it does not exist.
    • getByteArray

      @NotNull public @org.jetbrains.annotations.NotNull byte[] getByteArray(@NotNull @NotNull String key)
      Gets a byte array from the tag. If the object does not exist, an empty byte array is returned.
      Parameters:
      key - The key to get.
      Returns:
      The byte array, or an empty byte array if it does not exist.
    • getByteArray

      @NotNull public @org.jetbrains.annotations.NotNull byte[] getByteArray(@NotNull @NotNull String key, byte[] def)
      Gets a byte array from the tag. If the object does not exist, the default value is returned.
      Parameters:
      key - The key to get.
      def - The default value.
      Returns:
      The byte array, or the default value if it does not exist.
    • toTag

      @NotNull public @NotNull String toTag()
      Converts the tag to a NBT string.
      Returns:
      The tag as a string.
    • fromTag

      @Nullable public static @Nullable NBTTag fromTag(@Nullable @Nullable String string)
      Converts a NBT string to a NBTTag.
      Parameters:
      string - The string to convert.
      Returns:
      The converted tag.