Class CompoundBuilder
java.lang.Object
de.pauleff.builder.NBTBuilder
de.pauleff.builder.CompoundBuilder
Fluent builder for creating structured NBT compound tags with seamless nesting support.
Chain method calls to build complex hierarchical data structures effortlessly.
Build player data with nested inventory:
Tag_Compound player = NBTBuilder.compound("Player")
.addString("Name", "Steve")
.addInt("Level", 50)
.addList("Inventory", NBTTags.Tag_Compound)
.addCompound("item1")
.addString("id", "minecraft:diamond_sword")
.end()
.end()
.build();
- Author:
- Paul Ferlitz
- See Also:
-
Field Summary
Fields inherited from class de.pauleff.builder.NBTBuilder
name, parent -
Constructor Summary
ConstructorsConstructorDescriptionCompoundBuilder(String name, NBTBuilder parent) Creates a new compound builder ready for tag assembly. -
Method Summary
Modifier and TypeMethodDescriptionStores a byte value with the specified name.addCompound(String name) Creates a nested compound within this one and switches to nested-building mode.Stores a double-precision value with the specified name.Stores a single-precision float with the specified name.Stores an integer value with the specified name.Creates a new list within this compound and switches to list-building mode.Stores a long integer with the specified name.Stores a short integer with the specified name.Stores a string value with the specified name.Incorporates an existing tag into this compound.build()Finalizes construction and returns the completed compound tag.end()Completes this nested compound and returns control to the parent builder.Type-safe method to return to a CompoundBuilder parent.endList()Type-safe method to return to a ListBuilder parent.protected Tag_CompoundProvides access to the underlying compound for framework integration.Methods inherited from class de.pauleff.builder.NBTBuilder
buildAndSave, buildAndSave, byteTag, compound, doubleTag, floatTag, fromFile, getName, getParent, integer, list, longTag, shortTag, string, validateName
-
Constructor Details
-
CompoundBuilder
Creates a new compound builder ready for tag assembly.- Parameters:
name- the compound's identifierparent- the parent builder for nesting support, or null for root compounds
-
-
Method Details
-
addString
Stores a string value with the specified name.- Parameters:
name- the tag identifiervalue- the string content- Returns:
- this builder for chaining
-
addInt
Stores an integer value with the specified name.- Parameters:
name- the tag identifiervalue- the numeric content- Returns:
- this builder for chaining
-
addDouble
Stores a double-precision value with the specified name.- Parameters:
name- the tag identifiervalue- the floating-point content- Returns:
- this builder for chaining
-
addFloat
Stores a single-precision float with the specified name.- Parameters:
name- the tag identifiervalue- the floating-point content- Returns:
- this builder for chaining
-
addByte
Stores a byte value with the specified name.- Parameters:
name- the tag identifiervalue- the byte content- Returns:
- this builder for chaining
-
addShort
Stores a short integer with the specified name.- Parameters:
name- the tag identifiervalue- the numeric content- Returns:
- this builder for chaining
-
addLong
Stores a long integer with the specified name.- Parameters:
name- the tag identifiervalue- the numeric content- Returns:
- this builder for chaining
-
addTag
Incorporates an existing tag into this compound.- Parameters:
tag- the pre-built tag to include- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if tag is null
-
addList
Creates a new list within this compound and switches to list-building mode.- Parameters:
name- the list identifierlistType- the element type this list will contain- Returns:
- a
ListBuilderfor adding list elements - See Also:
-
addCompound
Creates a nested compound within this one and switches to nested-building mode.- Parameters:
name- the nested compound identifier- Returns:
- a new
CompoundBuilderfor the nested structure
-
build
Finalizes construction and returns the completed compound tag. Only available for root-level compounds.- Specified by:
buildin classNBTBuilder- Returns:
- the fully constructed
ICompoundTag - Throws:
IllegalStateException- if called on a nested builder (useend()first)
-
end
Completes this nested compound and returns control to the parent builder. Automatically integrates the completed compound into its parent structure.When to use
end():- Terminal operations - when finishing the current branch without further chaining
- Simple single-level nesting where parent type is obvious
- Variable storage patterns where builders are stored in variables
- Legacy code compatibility
Consider using type-safe alternatives:
endCompound()- when continuing to build on a compound parentendList()- when continuing to build on a list parent
- Specified by:
endin classNBTBuilder- Returns:
- the parent
NBTBuilderto continue building - Throws:
IllegalStateException- if called on a root builder (usebuild()instead)- Since:
- 1.3.0
-
endCompound
Type-safe method to return to a CompoundBuilder parent. Provides compile-time safety for fluent method chaining when you know the parent is a compound.When to use
endCompound():- Complex fluent chaining where you need to continue adding to a compound parent
- When you want compile-time type safety instead of runtime casting
- Building deeply nested structures with multiple compound levels
Example usage:
NBTBuilder.compound("Player") .addCompound("Stats") .addInt("level", 50) .endCompound() // ← Type-safe return to Player compound .addString("name", "Steve"); // ← Can continue building Player- Returns:
- the parent as a CompoundBuilder
- Throws:
IllegalStateException- if parent is not a CompoundBuilder- Since:
- 1.5.0
-
endList
Type-safe method to return to a ListBuilder parent. Provides compile-time safety for fluent method chaining when you know the parent is a list.When to use
endList():- Adding multiple compound elements to a list with continued chaining
- When you want compile-time type safety for list operations
- Building arrays of complex objects
Example usage:
NBTBuilder.list("inventory", NBTTags.Tag_Compound) .addCompound("item1") .addString("type", "sword") .endList() // ← Type-safe return to inventory list .addCompound("item2") // ← Can continue adding to list .addString("type", "potion") .endList();- Returns:
- the parent as a ListBuilder
- Throws:
IllegalStateException- if parent is not a ListBuilder- Since:
- 1.5.0
-
getCompound
Provides access to the underlying compound for framework integration.- Returns:
- the compound tag under construction
-