Class NBTFactory
java.lang.Object
de.pauleff.api.NBTFactory
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates an empty byte tag.createByte(String name) Creates a named byte tag.createByte(String name, byte value) Creates a byte tag with name and value.static ITag<byte[]> Creates an empty byte array tag.static ITag<byte[]> createByteArray(String name) Creates a named byte array tag.static ITag<byte[]> createByteArray(String name, byte[] value) Creates a byte array tag with name and value.static ICompoundTagCreates an empty compound tag.static ICompoundTagcreateCompound(String name) Creates a named compound tag.static ICompoundTagcreateCompound(String name, ArrayList<ITag<?>> data) Creates a compound tag with initial child tags.Creates an empty double tag.createDouble(String name) Creates a named double tag.createDouble(String name, double value) Creates a double tag with name and value.Creates an empty float tag.createFloat(String name) Creates a named float tag.createFloat(String name, float value) Creates a float tag with name and value.Creates an empty integer tag.Creates a named integer tag.Creates an integer tag with name and value.static ITag<int[]> Creates an empty integer array tag.static ITag<int[]> createIntArray(String name) Creates a named integer array tag.static ITag<int[]> createIntArray(String name, int[] value) Creates an integer array tag with name and value.static IListTagcreateList(int listTypeID) Creates an empty list tag for the specified element type.static IListTagcreateList(String name, int listTypeID) Creates a named list tag for the specified element type.Creates an empty long tag.createLong(String name) Creates a named long tag.createLong(String name, long value) Creates a long tag with name and value.static ITag<long[]> Creates an empty long array tag.static ITag<long[]> createLongArray(String name) Creates a named long array tag.static ITag<long[]> createLongArray(String name, long[] value) Creates a long array tag with name and value.Creates an empty short tag.createShort(String name) Creates a named short tag.createShort(String name, short value) Creates a short tag with name and value.static ICompoundTagcreateSimpleCompound(String rootName, String... keyValuePairs) Creates a simple NBT structure with key-value string pairs.Creates an empty string tag.createString(String name) Creates a named string tag.createString(String name, String value) Creates a string tag with name and value.static ICompoundTagparseCompoundFromSNBT(String snbt) Parses SNBT text into a compound tag.static ITag<?> parseFromSNBT(String snbt) Parses SNBT (Stringified NBT) text into an NBT tag structure.static IListTagparseListFromSNBT(String snbt) Parses SNBT text into a list tag.static StringConverts an NBT tag to SNBT (Stringified NBT) format.static voidwriteToSNBTFile(ITag<?> tag, File file) Writes an NBT tag to a text file in SNBT format.
-
Constructor Details
-
NBTFactory
public NBTFactory()
-
-
Method Details
-
createCompound
Creates an empty compound tag.- Returns:
- New
ICompoundTaginstance
-
createCompound
Creates a named compound tag.- Parameters:
name- The tag name- Returns:
- New
ICompoundTagwith the specified name
-
createCompound
Creates a compound tag with initial child tags.- Parameters:
name- The tag namedata- Initial child tags to add- Returns:
- New
ICompoundTagcontaining the provided children
-
createList
-
createList
-
createString
-
createString
-
createString
-
createInt
-
createInt
-
createInt
-
createDouble
-
createDouble
-
createDouble
-
createFloat
-
createFloat
-
createFloat
-
createByte
-
createByte
-
createByte
-
createShort
-
createShort
-
createShort
-
createLong
-
createLong
-
createLong
-
createByteArray
-
createByteArray
-
createByteArray
-
createIntArray
-
createIntArray
-
createIntArray
-
createLongArray
-
createLongArray
-
createLongArray
-
createSimpleCompound
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 tagkeyValuePairs- Alternating keys and values (key1, value1, key2, value2, ...)- Returns:
- A
ICompoundTagcontaining the key-value pairs - Throws:
IllegalArgumentException- If an odd number of arguments is provided
-
parseFromSNBT
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
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
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
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
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 writefile- The output file- Throws:
IOException- If writing to the file fails
-