Class ListBuilder
java.lang.Object
de.pauleff.builder.NBTBuilder
de.pauleff.builder.ListBuilder
Fluent builder for creating type-safe NBT lists. All elements must share the same NBT type,
preventing common structural errors when building Minecraft NBT data.
Build complex inventory structures with confidence:
Tag_List inventory = NBTBuilder.list("Inventory", NBTTags.Tag_Compound)
.addCompound("item1")
.addString("id", "minecraft:diamond_sword")
.addInt("Count", 1)
.end()
.build();
- Author:
- Paul Ferlitz
- See Also:
-
Field Summary
Fields inherited from class de.pauleff.builder.NBTBuilder
name, parent -
Constructor Summary
ConstructorsConstructorDescriptionListBuilder(String name, NBTTags listType, NBTBuilder parent) Creates a new list builder with strict type enforcement. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddBuiltTag(Tag<?> tag) Adds a pre-built tag to this list.Adds a byte element to the list.addCompound(String name) Begins building a compound element within this list.Adds a double element to the list.Adds a float element to the list.Adds an integer element to the list.Begins building a nested list element within this list.Adds a long element to the list.Adds a short element to the list.Adds a string element to the list.build()Completes construction and returns the built list.end()Finishes this nested list and returns 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_ListgetList()Provides access to the underlying list for parent builders.Returns the NBT type this list accepts for all elements.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
-
ListBuilder
Creates a new list builder with strict type enforcement.- Parameters:
name- the list tag namelistType- the NBT type all elements must matchparent- the parent builder for nesting support, or null for root lists- Throws:
IllegalArgumentException- if listType is null
-
-
Method Details
-
addString
Adds a string element to the list.- Parameters:
name- the element namevalue- the string value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept strings
-
addInt
Adds an integer element to the list.- Parameters:
name- the element namevalue- the integer value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept integers
-
addDouble
Adds a double element to the list.- Parameters:
name- the element namevalue- the double value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept doubles
-
addFloat
Adds a float element to the list.- Parameters:
name- the element namevalue- the float value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept floats
-
addByte
Adds a byte element to the list.- Parameters:
name- the element namevalue- the byte value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept bytes
-
addShort
Adds a short element to the list.- Parameters:
name- the element namevalue- the short value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept shorts
-
addLong
Adds a long element to the list.- Parameters:
name- the element namevalue- the long value- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if this list doesn't accept longs
-
addCompound
Begins building a compound element within this list.- Parameters:
name- the compound element name- Returns:
- a
CompoundBuilderfor defining the compound's contents - Throws:
IllegalArgumentException- if this list doesn't accept compounds
-
addList
Begins building a nested list element within this list.- Parameters:
name- the nested list element namenestedListType- the NBT type for elements in the nested list- Returns:
- a new
ListBuilderfor defining the nested list's contents - Throws:
IllegalArgumentException- if this list doesn't accept nested lists
-
addBuiltTag
Adds a pre-built tag to this list. Used internally by nested builders.- Parameters:
tag- the completed tag to add- Throws:
IllegalArgumentException- if tag is null or type doesn't match
-
build
Completes construction and returns the built list.- Specified by:
buildin classNBTBuilder- Returns:
- the completed
IListTag - Throws:
IllegalStateException- if this is a nested builder (callend()first)
-
end
Finishes this nested list and returns to the parent builder. Automatically integrates the completed list 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
NBTBuilder - Throws:
IllegalStateException- if this is a root builder (callbuild()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():- Lists nested within compounds where you need to continue building the compound
- When you want compile-time type safety instead of runtime casting
- Building complex structures with mixed container types
Example usage:
NBTBuilder.compound("Player") .addList("inventory", NBTTags.Tag_String) .addString("item1", "sword") .addString("item2", "potion") .endCompound() // ← Type-safe return to Player compound .addInt("level", 50); // ← 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():- Nested lists (lists within lists) where you need to continue building the parent list
- When you want compile-time type safety for complex list structures
- Building multi-dimensional array-like structures
Example usage:
NBTBuilder.list("matrix", NBTTags.Tag_List) .addList("row1", NBTTags.Tag_Int) .addInt("col1", 1) .addInt("col2", 2) .endList() // ← Type-safe return to matrix list .addList("row2", NBTTags.Tag_Int) // ← Can continue adding rows .addInt("col1", 3) .addInt("col2", 4) .endList();- Returns:
- the parent as a ListBuilder
- Throws:
IllegalStateException- if parent is not a ListBuilder- Since:
- 1.5.0
-
getListType
Returns the NBT type this list accepts for all elements.- Returns:
- the enforced element type
-
getList
Provides access to the underlying list for parent builders.- Returns:
- the list being constructed
-