Skip to content

Configuration

vansencool edited this page Jul 7, 2024 · 2 revisions

The ConfigUtils class provides utility methods for handling plugin configurations. It includes methods for loading, saving, and reloading configurations, both synchronously and asynchronously, as well as setting and retrieving configuration values.

Methods:

load: Loads the configuration file asynchronously. Parameters: String fileName

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    // Use configUtils here
});

save: Saves the configuration file synchronously.

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.save();
});

saveAsync: Saves the configuration file asynchronously.

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.saveAsync().thenRun(() -> {
        // Config saved
    });
});

reload: Reloads the configuration file synchronously.

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.reload();
});

reloadAsync: Reloads the configuration file asynchronously.

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.reloadAsync().thenRun(() -> {
        // Config reloaded
    });
});

get: Retrieves a value from the configuration. Parameters: String path

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    String value = configUtils.get("some.path");
    System.out.println(value);
});

set: Sets a value in the configuration. Parameters: String path, Object value

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.set("some.path", "newValue");
    configUtils.save();
});

setIf: Sets a value in the configuration if a condition is met. Parameters: String path, Object value, Predicate condition

Example:

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.setIf("some.path", "newValue", currentValue -> currentValue == null);
    configUtils.save();
});

Builder Class The Builder class provides a way to construct ConfigUtils instances with additional convenience methods.

Methods

of: Creates a Builder instance asynchronously. Parameters: String fileName

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    // Use builder here
});

save: Saves the configuration file synchronously.

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.save();
});

saveAsync: Saves the configuration file asynchronously.

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.saveAsync().thenRun(() -> {
        // Config saved
    });
});

reload: Reloads the configuration file synchronously.

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.reload();
});

reloadAsync: Reloads the configuration file asynchronously.

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.reloadAsync().thenRun(() -> {
        // Config reloaded
    });
});

get: Retrieves a value from the configuration. Parameters: String path

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    String value = builder.get("some.path");
    System.out.println(value);
});

set: Sets a value in the configuration. Parameters: String path, Object value

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.set("some.path", "newValue");
    builder.save();
});

setIf: Sets a value in the configuration if a condition is met. Parameters: String path, Object value, Predicate condition

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.setIf("some.path", "newValue", currentValue -> currentValue == null);
    builder.save();
});

build: Builds and returns the ConfigUtils instance.

Example:

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    ConfigUtils configUtils = builder.build();
});

Examples:

Example 1: Loading and Getting a Configuration Value

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    String value = configUtils.get("some.path");
    System.out.println(value);
});

Example 2: Setting and Saving a Configuration Value

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.set("some.path", "newValue");
    configUtils.save();
});

Example 3: Reloading a Configuration

ConfigUtils.load("config.yml").thenAccept(configUtils -> {
    configUtils.reload();
});

Example 4: Using the Builder Class

ConfigUtils.Builder.of("config.yml").thenAccept(builder -> {
    builder.set("some.path", "newValue")
           .save()
           .build();
});
Clone this wiki locally