Class NBTBuilder

java.lang.Object
de.pauleff.builder.NBTBuilder
Direct Known Subclasses:
CompoundBuilder, ListBuilder

public abstract class NBTBuilder extends Object
Fluent factory for creating NBT tag structures with type safety and readable syntax. Provides static factory methods and builder patterns for constructing complex NBT hierarchies.

Build NBT structures using method chaining and automatic type validation:


 Tag_Compound player = NBTBuilder.compound("Player")
     .addString("Name", "Steve")
     .addInt("Level", 50)
     .addList("Inventory", NBTTags.Tag_Compound)
         .addCompound("sword")
             .addString("id", "minecraft:diamond_sword")
             .addInt("Count", 1)
         .end()
     .end()
     .build();
 

Remember to call build() on root builders and end() on nested ones.

Author:
Paul Ferlitz
See Also:
  • Field Details

  • Constructor Details

    • NBTBuilder

      protected NBTBuilder(String name, NBTBuilder parent)
      Constructs a builder with parent-child relationship for nested structures.
      Parameters:
      name - tag name
      parent - parent builder, null for root builders
  • Method Details

    • compound

      public static CompoundBuilder compound(String name)
      Creates a fluent builder for compound tags containing key-value pairs.
      Parameters:
      name - compound tag name
      Returns:
      new CompoundBuilder
    • list

      public static ListBuilder list(String name, NBTTags listType)
      Creates a fluent builder for homogeneous list tags.
      Parameters:
      name - list tag name
      listType - type constraint for all list elements
      Returns:
      new ListBuilder
    • string

      public static ITag<String> string(String name, String value)
      Creates a string tag with immediate value assignment.
      Parameters:
      name - tag name
      value - string content
      Returns:
      Tag_String as ITag
    • integer

      public static ITag<Integer> integer(String name, int value)
      Creates an integer tag with immediate value assignment.
      Parameters:
      name - tag name
      value - integer content
      Returns:
      Tag_Int as ITag
    • doubleTag

      public static ITag<Double> doubleTag(String name, double value)
      Creates a double-precision tag with immediate value assignment.
      Parameters:
      name - tag name
      value - double content
      Returns:
      Tag_Double as ITag
    • floatTag

      public static ITag<Float> floatTag(String name, float value)
      Creates a single-precision tag with immediate value assignment.
      Parameters:
      name - tag name
      value - float content
      Returns:
      Tag_Float as ITag
    • byteTag

      public static ITag<Byte> byteTag(String name, byte value)
      Creates a byte tag with immediate value assignment.
      Parameters:
      name - tag name
      value - byte content
      Returns:
      Tag_Byte as ITag
    • shortTag

      public static ITag<Short> shortTag(String name, short value)
      Creates a short integer tag with immediate value assignment.
      Parameters:
      name - tag name
      value - short content
      Returns:
      Tag_Short as ITag
    • longTag

      public static ITag<Long> longTag(String name, long value)
      Creates a long integer tag with immediate value assignment.
      Parameters:
      name - tag name
      value - long content
      Returns:
      Tag_Long as ITag
    • validateName

      protected static void validateName(String name)
      Ensures tag names meet NBT requirements.
      Parameters:
      name - candidate tag name
      Throws:
      IllegalArgumentException - if name is null or empty
    • fromFile

      public static CompoundBuilder fromFile(File file) throws IOException
      Creates a compound builder pre-loaded with data from an existing NBT file. The builder can then be used to modify the structure before saving.
      Parameters:
      file - The File to load NBT data from
      Returns:
      A CompoundBuilder containing the loaded data
      Throws:
      IOException - If the file cannot be read
    • build

      public abstract ITag<?> build()
      Finalizes construction and returns the completed NBT tag.
      Returns:
      immutable tag structure
    • end

      public abstract NBTBuilder end()
      Closes this nested builder and returns control to its parent.
      Returns:
      parent builder for continued chaining
      Throws:
      IllegalStateException - if called on root builder
    • getName

      public String getName()
      Returns the name that will be assigned to the built tag.
      Returns:
      tag name
    • getParent

      public NBTBuilder getParent()
      Returns the parent builder in the construction hierarchy.
      Returns:
      parent builder, null for root builders
    • buildAndSave

      public void buildAndSave(File file) throws IOException
      Builds the NBT structure and writes it directly to a file. Only available for root builders (builders without parents).
      Parameters:
      file - The File to write to
      Throws:
      IOException - If writing fails
      IllegalStateException - If this is not a root builder
    • buildAndSave

      public void buildAndSave(File file, Compression_Types compression) throws IOException
      Builds the NBT structure and writes it to a file with specified compression. Only available for root builders (builders without parents).
      Parameters:
      file - The File to write to
      compression - The Compression_Types to use
      Throws:
      IOException - If writing fails
      IllegalStateException - If this is not a root builder