Class ConfigLoader
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:
-
Method Summary
Modifier and TypeMethodDescriptionstatic de.exlll.configlib.YamlConfigurationProperties.Builder<?> Creates a default properties builder with common settings.static de.exlll.configlib.YamlConfigurationProperties.Builder<?> defaultPropertiesBuilder(String envPrefix) Creates a default properties builder with environment variable resolution.static <T> TLoads a configuration from an existing file.static <T> TloadOrCreate(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> voidsave(Path path, Class<T> configClass, T config, de.exlll.configlib.YamlConfigurationProperties properties) Saves a configuration to the specified path.static <T> TupdateIfChanged(Path path, Class<T> configClass, de.exlll.configlib.YamlConfigurationProperties properties) Updates a configuration file with custom properties, only writing if changed.
-
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
updatestrategy, 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
-
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:
- Loads the existing config (with Java defaults for any new fields)
- Serializes it back to YAML (which includes new fields with defaults)
- Compares with the original file content
- 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 fileconfigClass- the configuration classproperties- custom ConfigLib properties- Returns:
- the loaded configuration
-