Class ConfigLoader

java.lang.Object
net.blockhost.commons.config.ConfigLoader

public final class ConfigLoader extends Object

Utility class for loading and saving configuration files using ConfigLib.

This class provides a simplified API for working with ConfigLib's YAML configurations, with sensible defaults and common configuration patterns. All methods require explicit properties, which can be obtained via defaultPropertiesBuilder().

Example usage:

// Define your configuration class
@Configuration
public class MyConfig {
    private String host = "localhost";
    private int port = 3306;
}

// Load or create configuration
Path configPath = dataFolder.resolve("config.yml");
var properties = ConfigLoader.defaultPropertiesBuilder().build();
MyConfig config = ConfigLoader.loadOrCreate(configPath, MyConfig.class, properties);

// Load with env var resolution (e.g. MYPLUGIN_HOST overrides host)
var envProperties = ConfigLoader.defaultPropertiesBuilder("MYPLUGIN_").build();
MyConfig config = ConfigLoader.load(configPath, MyConfig.class, envProperties);
See Also:
  • Configuration
  • YamlConfigurations
  • Method Summary

    Modifier and Type
    Method
    Description
    static de.exlll.configlib.YamlConfigurationProperties.Builder<?>
    Creates a default properties builder with common settings.
    static de.exlll.configlib.YamlConfigurationProperties.Builder<?>
    Creates a default properties builder with environment variable resolution.
    static <T> T
    load(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)
    Loads a configuration from an existing file.
    static <T> T
    loadOrCreate(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)
    Loads a configuration from the specified path, or creates it with default values if it doesn't exist.
    static <T> void
    save(Path path, Class<T> configClass, T config, de.exlll.configlib.YamlConfigurationProperties properties)
    Saves a configuration to the specified path.
    static <T> T
    updateIfChanged(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)
    Updates a configuration file with custom properties, only writing if changed.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • loadOrCreate

      public static <T> T loadOrCreate(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)

      Loads a configuration from the specified path, or creates it with default values if it doesn't exist.

      This method uses the update strategy, which:

      • Creates the file with defaults if it doesn't exist
      • Adds any new fields that are in the class but not in the file
      • Preserves existing values in the file
      • Removes fields that are no longer in the class
    • save

      public static <T> void save(Path path, Class<T> configClass, T config, de.exlll.configlib.YamlConfigurationProperties properties)

      Saves a configuration to the specified path.

      This will overwrite any existing file at the path.

    • load

      public static <T> T load(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)

      Loads a configuration from an existing file.

      Unlike loadOrCreate(Path, Class, YamlConfigurationProperties), this method requires the file to exist and will not create it if missing.

    • defaultPropertiesBuilder

      public static de.exlll.configlib.YamlConfigurationProperties.Builder<?> defaultPropertiesBuilder()
      Creates a default properties builder with common settings.
    • defaultPropertiesBuilder

      public static de.exlll.configlib.YamlConfigurationProperties.Builder<?> defaultPropertiesBuilder(String envPrefix)
      Creates a default properties builder with environment variable resolution.
      Parameters:
      envPrefix - the environment variable prefix (e.g. "MYPLUGIN_")
      Returns:
      a builder configured with env var resolution
    • updateIfChanged

      public static <T> T updateIfChanged(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties)

      Updates a configuration file with custom properties, only writing if changed.

      This method:

      1. Loads the existing config (with Java defaults for any new fields)
      2. Serializes it back to YAML (which includes new fields with defaults)
      3. Compares with the original file content
      4. Only writes if the content actually changed

      This ensures new fields are added to the file and obsolete fields are removed, but avoids unnecessary disk writes and timestamp updates when nothing changed.

      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to the configuration file
      configClass - the configuration class
      properties - custom ConfigLib properties
      Returns:
      the loaded configuration