Class NBTBuilder
java.lang.Object
de.pauleff.builder.NBTBuilder
- Direct Known Subclasses:
CompoundBuilder,ListBuilder
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 Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNBTBuilder(String name, NBTBuilder parent) Constructs a builder with parent-child relationship for nested structures. -
Method Summary
Modifier and TypeMethodDescriptionabstract ITag<?> build()Finalizes construction and returns the completed NBT tag.voidbuildAndSave(File file) Builds the NBT structure and writes it directly to a file.voidbuildAndSave(File file, Compression_Types compression) Builds the NBT structure and writes it to a file with specified compression.Creates a byte tag with immediate value assignment.static CompoundBuilderCreates a fluent builder for compound tags containing key-value pairs.Creates a double-precision tag with immediate value assignment.abstract NBTBuilderend()Closes this nested builder and returns control to its parent.Creates a single-precision tag with immediate value assignment.static CompoundBuilderCreates a compound builder pre-loaded with data from an existing NBT file.getName()Returns the name that will be assigned to the built tag.Returns the parent builder in the construction hierarchy.Creates an integer tag with immediate value assignment.static ListBuilderCreates a fluent builder for homogeneous list tags.Creates a long integer tag with immediate value assignment.Creates a short integer tag with immediate value assignment.Creates a string tag with immediate value assignment.protected static voidvalidateName(String name) Ensures tag names meet NBT requirements.
-
Field Details
-
name
-
parent
-
-
Constructor Details
-
NBTBuilder
Constructs a builder with parent-child relationship for nested structures.- Parameters:
name- tag nameparent- parent builder, null for root builders
-
-
Method Details
-
compound
Creates a fluent builder for compound tags containing key-value pairs.- Parameters:
name- compound tag name- Returns:
- new
CompoundBuilder
-
list
Creates a fluent builder for homogeneous list tags.- Parameters:
name- list tag namelistType- type constraint for all list elements- Returns:
- new
ListBuilder
-
string
-
integer
-
doubleTag
Creates a double-precision tag with immediate value assignment.- Parameters:
name- tag namevalue- double content- Returns:
Tag_DoubleasITag
-
floatTag
-
byteTag
-
shortTag
-
longTag
-
validateName
Ensures tag names meet NBT requirements.- Parameters:
name- candidate tag name- Throws:
IllegalArgumentException- if name is null or empty
-
fromFile
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- TheFileto load NBT data from- Returns:
- A
CompoundBuildercontaining the loaded data - Throws:
IOException- If the file cannot be read
-
build
Finalizes construction and returns the completed NBT tag.- Returns:
- immutable tag structure
-
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
-
getParent
Returns the parent builder in the construction hierarchy.- Returns:
- parent builder, null for root builders
-
buildAndSave
Builds the NBT structure and writes it directly to a file. Only available for root builders (builders without parents).- Parameters:
file- TheFileto write to- Throws:
IOException- If writing failsIllegalStateException- If this is not a root builder
-
buildAndSave
Builds the NBT structure and writes it to a file with specified compression. Only available for root builders (builders without parents).- Parameters:
file- TheFileto write tocompression- TheCompression_Typesto use- Throws:
IOException- If writing failsIllegalStateException- If this is not a root builder
-