Class NBTFileFactory
java.lang.Object
de.pauleff.api.NBTFileFactory
Factory for creating NBT file I/O handlers with automatic format detection.
Simplifies the process of reading from and writing to NBT files by handling compression detection,
stream management, and format validation automatically.
Quick start:
// Read an NBT file
INBTReader reader = NBTFileFactory.createReader(new File("level.dat"));
ICompoundTag levelData = reader.read();
reader.close();
// Write back with same compression
INBTWriter writer = NBTFileFactory.createWriter(new File("level.dat"));
writer.write(levelData);
- Author:
- Paul Ferlitz
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyNBTFile(File source, File destination) Copies an NBT file to a new location, preserving compression format.static INBTReaderCreates a reader from an existing data stream.static INBTReadercreateReader(File nbtFile) Creates a reader that automatically detects compression format.static INBTWritercreateWriter(File nbtFile) Creates a writer that preserves the original file's compression format.static INBTWritercreateWriter(File nbtFile, Compression_Types compression) Creates a writer with explicit compression settings.static booleanisValidNBTFile(File nbtFile) Checks if a file appears to be a valid NBT file by attempting to read its header.static ICompoundTagreadNBTFile(File nbtFile) Reads an NBT file completely and returns the root compound tag.static voidwriteNBTFile(File nbtFile, ICompoundTag root) Writes an NBT compound tag to file with automatic compression detection.static voidwriteNBTFile(File nbtFile, ICompoundTag root, Compression_Types compression) Writes an NBT compound tag to a new file with specified compression.
-
Constructor Details
-
NBTFileFactory
public NBTFileFactory()
-
-
Method Details
-
createReader
Creates a reader that automatically detects compression format.- Parameters:
nbtFile- TheFileto read NBT data from- Returns:
- New
INBTReaderready to parse the file
-
createReader
Creates a reader from an existing data stream.- Parameters:
dis- TheDataInputStreamcontaining NBT data- Returns:
- New
INBTReaderready to parse the stream
-
createWriter
Creates a writer that preserves the original file's compression format. File must exist to detect compression type.- Parameters:
nbtFile- TheFileto write NBT data to- Returns:
- New
INBTWriterready to write data - Throws:
FileNotFoundException- If the target file doesn't exist for compression detection
-
createWriter
Creates a writer with explicit compression settings. Creates or overwrites the target file with the specified compression.- Parameters:
nbtFile- TheFileto write NBT data tocompression- TheCompression_Typesto apply- Returns:
- New
INBTWriterready to write compressed data
-
readNBTFile
Reads an NBT file completely and returns the root compound tag. Automatically handles compression detection and resource cleanup.- Parameters:
nbtFile- TheFileto read- Returns:
- The root
ICompoundTagcontaining all NBT data - Throws:
IOException- If the file cannot be read or parsed
-
writeNBTFile
Writes an NBT compound tag to file with automatic compression detection. If the file exists, preserves its original compression format.- Parameters:
nbtFile- TheFileto write toroot- TheICompoundTagto write- Throws:
IOException- If the file cannot be written
-
writeNBTFile
public static void writeNBTFile(File nbtFile, ICompoundTag root, Compression_Types compression) throws IOException Writes an NBT compound tag to a new file with specified compression.- Parameters:
nbtFile- TheFileto write toroot- TheICompoundTagto writecompression- TheCompression_Typesto use- Throws:
IOException- If the file cannot be written
-
copyNBTFile
Copies an NBT file to a new location, preserving compression format.- Parameters:
source- The sourceFileto copy fromdestination- The destinationFileto copy to- Throws:
IOException- If the copy operation fails
-
isValidNBTFile
-