Class NBTFactory

java.lang.Object
de.pauleff.api.NBTFactory

public class NBTFactory extends Object
Factory for creating NBT tags through the public API. Provides convenient static methods for building NBT structures without dealing with constructors directly. Use this when you need to create individual tags programmatically.

Example usage:


 // Create a player data structure
 ICompoundTag player = NBTFactory.createCompound("Player");
 player.addString("Name", "Steve")
       .addInt("Level", 42)
       .addDouble("Health", 20.0);

 // Create inventory items
 IListTag inventory = NBTFactory.createList("Inventory", NBTTags.Tag_Compound.getId());
 
Author:
Paul Ferlitz
See Also:
  • Constructor Details

    • NBTFactory

      public NBTFactory()
  • Method Details

    • createCompound

      public static ICompoundTag createCompound()
      Creates an empty compound tag.
      Returns:
      New ICompoundTag instance
    • createCompound

      public static ICompoundTag createCompound(String name)
      Creates a named compound tag.
      Parameters:
      name - The tag name
      Returns:
      New ICompoundTag with the specified name
    • createCompound

      public static ICompoundTag createCompound(String name, ArrayList<ITag<?>> data)
      Creates a compound tag with initial child tags.
      Parameters:
      name - The tag name
      data - Initial child tags to add
      Returns:
      New ICompoundTag containing the provided children
    • createList

      public static IListTag createList(int listTypeID)
      Creates an empty list tag for the specified element type.
      Parameters:
      listTypeID - The NBT type ID of elements this list will contain
      Returns:
      New IListTag instance
    • createList

      public static IListTag createList(String name, int listTypeID)
      Creates a named list tag for the specified element type.
      Parameters:
      name - The tag name
      listTypeID - The NBT type ID of elements this list will contain
      Returns:
      New IListTag with the specified name and type
    • createString

      public static ITag<String> createString()
      Creates an empty string tag.
      Returns:
      New string ITag
    • createString

      public static ITag<String> createString(String name)
      Creates a named string tag.
      Parameters:
      name - The tag name
      Returns:
      New string ITag with the specified name
    • createString

      public static ITag<String> createString(String name, String value)
      Creates a string tag with name and value.
      Parameters:
      name - The tag name
      value - The string value
      Returns:
      New string ITag with the specified name and value
    • createInt

      public static ITag<Integer> createInt()
      Creates an empty integer tag.
      Returns:
      New integer ITag
    • createInt

      public static ITag<Integer> createInt(String name)
      Creates a named integer tag.
      Parameters:
      name - The tag name
      Returns:
      New integer ITag with the specified name
    • createInt

      public static ITag<Integer> createInt(String name, int value)
      Creates an integer tag with name and value.
      Parameters:
      name - The tag name
      value - The integer value
      Returns:
      New integer ITag with the specified name and value
    • createDouble

      public static ITag<Double> createDouble()
      Creates an empty double tag.
      Returns:
      New double ITag
    • createDouble

      public static ITag<Double> createDouble(String name)
      Creates a named double tag.
      Parameters:
      name - The tag name
      Returns:
      New double ITag with the specified name
    • createDouble

      public static ITag<Double> createDouble(String name, double value)
      Creates a double tag with name and value.
      Parameters:
      name - The tag name
      value - The double value
      Returns:
      New double ITag with the specified name and value
    • createFloat

      public static ITag<Float> createFloat()
      Creates an empty float tag.
      Returns:
      New float ITag
    • createFloat

      public static ITag<Float> createFloat(String name)
      Creates a named float tag.
      Parameters:
      name - The tag name
      Returns:
      New float ITag with the specified name
    • createFloat

      public static ITag<Float> createFloat(String name, float value)
      Creates a float tag with name and value.
      Parameters:
      name - The tag name
      value - The float value
      Returns:
      New float ITag with the specified name and value
    • createByte

      public static ITag<Byte> createByte()
      Creates an empty byte tag.
      Returns:
      New byte ITag
    • createByte

      public static ITag<Byte> createByte(String name)
      Creates a named byte tag.
      Parameters:
      name - The tag name
      Returns:
      New byte ITag with the specified name
    • createByte

      public static ITag<Byte> createByte(String name, byte value)
      Creates a byte tag with name and value.
      Parameters:
      name - The tag name
      value - The byte value
      Returns:
      New byte ITag with the specified name and value
    • createShort

      public static ITag<Short> createShort()
      Creates an empty short tag.
      Returns:
      New short ITag
    • createShort

      public static ITag<Short> createShort(String name)
      Creates a named short tag.
      Parameters:
      name - The tag name
      Returns:
      New short ITag with the specified name
    • createShort

      public static ITag<Short> createShort(String name, short value)
      Creates a short tag with name and value.
      Parameters:
      name - The tag name
      value - The short value
      Returns:
      New short ITag with the specified name and value
    • createLong

      public static ITag<Long> createLong()
      Creates an empty long tag.
      Returns:
      New long ITag
    • createLong

      public static ITag<Long> createLong(String name)
      Creates a named long tag.
      Parameters:
      name - The tag name
      Returns:
      New long ITag with the specified name
    • createLong

      public static ITag<Long> createLong(String name, long value)
      Creates a long tag with name and value.
      Parameters:
      name - The tag name
      value - The long value
      Returns:
      New long ITag with the specified name and value
    • createByteArray

      public static ITag<byte[]> createByteArray()
      Creates an empty byte array tag.
      Returns:
      New byte array ITag
    • createByteArray

      public static ITag<byte[]> createByteArray(String name)
      Creates a named byte array tag.
      Parameters:
      name - The tag name
      Returns:
      New byte array ITag with the specified name
    • createByteArray

      public static ITag<byte[]> createByteArray(String name, byte[] value)
      Creates a byte array tag with name and value.
      Parameters:
      name - The tag name
      value - The byte array value
      Returns:
      New byte array ITag with the specified name and value
    • createIntArray

      public static ITag<int[]> createIntArray()
      Creates an empty integer array tag.
      Returns:
      New integer array ITag
    • createIntArray

      public static ITag<int[]> createIntArray(String name)
      Creates a named integer array tag.
      Parameters:
      name - The tag name
      Returns:
      New integer array ITag with the specified name
    • createIntArray

      public static ITag<int[]> createIntArray(String name, int[] value)
      Creates an integer array tag with name and value.
      Parameters:
      name - The tag name
      value - The integer array value
      Returns:
      New integer array ITag with the specified name and value
    • createLongArray

      public static ITag<long[]> createLongArray()
      Creates an empty long array tag.
      Returns:
      New long array ITag
    • createLongArray

      public static ITag<long[]> createLongArray(String name)
      Creates a named long array tag.
      Parameters:
      name - The tag name
      Returns:
      New long array ITag with the specified name
    • createLongArray

      public static ITag<long[]> createLongArray(String name, long[] value)
      Creates a long array tag with name and value.
      Parameters:
      name - The tag name
      value - The long array value
      Returns:
      New long array ITag with the specified name and value
    • createSimpleCompound

      public static ICompoundTag createSimpleCompound(String rootName, String... keyValuePairs)
      Creates a simple NBT structure with key-value string pairs. Perfect for configuration files or simple data storage.
      Parameters:
      rootName - The name of the root compound tag
      keyValuePairs - Alternating keys and values (key1, value1, key2, value2, ...)
      Returns:
      A ICompoundTag containing the key-value pairs
      Throws:
      IllegalArgumentException - If an odd number of arguments is provided
    • parseFromSNBT

      public static ITag<?> parseFromSNBT(String snbt) throws SNBTException
      Parses SNBT (Stringified NBT) text into an NBT tag structure. Supports the complete SNBT syntax used in Minecraft commands.

      Example usage:

      
       // Parse a compound from a Minecraft command
       ITag<?> tag = NBTFactory.parseFromSNBT("{display:{Name:\"Custom Item\"}}");
      
       // Parse a simple value
       ITag<?> number = NBTFactory.parseFromSNBT("42b");
      
       // Parse an array
       ITag<?> array = NBTFactory.parseFromSNBT("[I; 1, 2, 3]");
       
      Parameters:
      snbt - The SNBT string to parse
      Returns:
      The parsed NBT tag structure
      Throws:
      SNBTException - If the SNBT syntax is invalid
    • parseCompoundFromSNBT

      public static ICompoundTag parseCompoundFromSNBT(String snbt) throws SNBTException
      Parses SNBT text into a compound tag. Convenience method that ensures the result is a compound tag.
      Parameters:
      snbt - The SNBT string to parse (must represent a compound)
      Returns:
      The parsed compound tag
      Throws:
      SNBTException - If the SNBT syntax is invalid or doesn't represent a compound
    • parseListFromSNBT

      public static IListTag parseListFromSNBT(String snbt) throws SNBTException
      Parses SNBT text into a list tag. Convenience method that ensures the result is a list tag.
      Parameters:
      snbt - The SNBT string to parse (must represent a list)
      Returns:
      The parsed list tag
      Throws:
      SNBTException - If the SNBT syntax is invalid or doesn't represent a list
    • toSNBT

      public static String toSNBT(ITag<?> tag)
      Converts an NBT tag to SNBT (Stringified NBT) format. Uses compact formatting suitable for Minecraft commands.

      Example usage:

      
       ICompoundTag player = NBTFactory.createCompound("Player")
           .addString("name", "Steve")
           .addInt("level", 42);
      
       String snbt = NBTFactory.toSNBT(player);
       // Result: {name:"Steve",level:42}
       
      Parameters:
      tag - The NBT tag to serialize
      Returns:
      The SNBT string representation
    • writeToSNBTFile

      public static void writeToSNBTFile(ITag<?> tag, File file) throws IOException
      Writes an NBT tag to a text file in SNBT format. Uses Minecraft-compatible formatting and adds a trailing newline.
      Parameters:
      tag - The NBT tag to write
      file - The output file
      Throws:
      IOException - If writing to the file fails