Class YAMLUpdater

java.lang.Object
me.croabeast.file.YAMLUpdater

public final class YAMLUpdater extends Object
A utility class for updating YAML configuration files while preserving comments and formatting. This class ensures that existing configurations remain intact while merging new keys and values.

Designed for scenarios where configuration files need to be updated dynamically without losing manually added comments, spacing, or structure.

Features:

  • Keeps original file comments and formatting.
  • Only adds missing keys from a default configuration.
  • Ensures a non-destructive update process.
  • Method Details

    • update

      public void update() throws IOException
      Updates the YAML configuration file by merging missing keys while preserving comments and formatting.

      This method ensures that:

      • Existing keys and values are retained.
      • New keys from the default configuration are added.
      • Comments and formatting remain intact.
      • Ignored keys are properly skipped.
      Throws:
      IOException - If an I/O error occurs while reading or writing the file.
    • of

      public static <T> YAMLUpdater of(T loader, String resourcePath, File file, List<String> ignored) throws IOException
      Creates a new YAMLUpdater instance using the specified resource path and ignored keys.

      This method allows easy instantiation of the updater with a loader, resource path, and file location. It also supports ignoring specific keys to prevent them from being added or modified.

      Type Parameters:
      T - The type of the loader (e.g., a plugin instance or class reference).
      Parameters:
      loader - The class loader or plugin instance used to access the resource file.
      resourcePath - The path to the default configuration resource inside the JAR.
      file - The target YAML configuration file to update.
      ignored - A list of keys that should be ignored during the update process.
      Returns:
      A new instance of YAMLUpdater configured with the given parameters.
      Throws:
      IOException - If an error occurs while reading the resource or target file.
    • of

      public static <T> YAMLUpdater of(T loader, String resourcePath, File file, String... ignored) throws IOException
      Creates a new YAMLUpdater instance with ignored keys provided as varargs.

      This is a convenience method that converts the ignored keys array into a list before calling of(Object, String, File, List).

      Type Parameters:
      T - The type of the loader (e.g., a plugin instance or class reference).
      Parameters:
      loader - The class loader or plugin instance used to access the resource file.
      resourcePath - The path to the default configuration resource inside the JAR.
      file - The target YAML configuration file to update.
      ignored - An array of keys that should be ignored during the update process.
      Returns:
      A new instance of YAMLUpdater configured with the given parameters.
      Throws:
      IOException - If an error occurs while reading the resource or target file.