Class NBTFileFactory

java.lang.Object
de.pauleff.api.NBTFileFactory

public class NBTFileFactory extends Object
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 Details

    • NBTFileFactory

      public NBTFileFactory()
  • Method Details

    • createReader

      public static INBTReader createReader(File nbtFile)
      Creates a reader that automatically detects compression format.
      Parameters:
      nbtFile - The File to read NBT data from
      Returns:
      New INBTReader ready to parse the file
    • createReader

      public static INBTReader createReader(DataInputStream dis)
      Creates a reader from an existing data stream.
      Parameters:
      dis - The DataInputStream containing NBT data
      Returns:
      New INBTReader ready to parse the stream
    • createWriter

      public static INBTWriter createWriter(File nbtFile) throws FileNotFoundException
      Creates a writer that preserves the original file's compression format. File must exist to detect compression type.
      Parameters:
      nbtFile - The File to write NBT data to
      Returns:
      New INBTWriter ready to write data
      Throws:
      FileNotFoundException - If the target file doesn't exist for compression detection
    • createWriter

      public static INBTWriter createWriter(File nbtFile, Compression_Types compression)
      Creates a writer with explicit compression settings. Creates or overwrites the target file with the specified compression.
      Parameters:
      nbtFile - The File to write NBT data to
      compression - The Compression_Types to apply
      Returns:
      New INBTWriter ready to write compressed data
    • readNBTFile

      public static ICompoundTag readNBTFile(File nbtFile) throws IOException
      Reads an NBT file completely and returns the root compound tag. Automatically handles compression detection and resource cleanup.
      Parameters:
      nbtFile - The File to read
      Returns:
      The root ICompoundTag containing all NBT data
      Throws:
      IOException - If the file cannot be read or parsed
    • writeNBTFile

      public static void writeNBTFile(File nbtFile, ICompoundTag root) throws IOException
      Writes an NBT compound tag to file with automatic compression detection. If the file exists, preserves its original compression format.
      Parameters:
      nbtFile - The File to write to
      root - The ICompoundTag to 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 - The File to write to
      root - The ICompoundTag to write
      compression - The Compression_Types to use
      Throws:
      IOException - If the file cannot be written
    • copyNBTFile

      public static void copyNBTFile(File source, File destination) throws IOException
      Copies an NBT file to a new location, preserving compression format.
      Parameters:
      source - The source File to copy from
      destination - The destination File to copy to
      Throws:
      IOException - If the copy operation fails
    • isValidNBTFile

      public static boolean isValidNBTFile(File nbtFile)
      Checks if a file appears to be a valid NBT file by attempting to read its header.
      Parameters:
      nbtFile - The File to validate
      Returns:
      true if the file appears to be valid NBT, false otherwise