Skip to content

Config Storage

Emil edited this page Sep 27, 2026 · 2 revisions

JsonConfigStore<T> stores one JSON file. Your mod supplies the config class, defaults, validation, file path, and the point at which changes are saved.

import dev.elpu7.elib.config.JsonConfigStore;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class ExampleConfig {
    boolean enabled = true;
    double speed = 1.0;

    static void sanitize(ExampleConfig config) {
        if (!Double.isFinite(config.speed)) {
            config.speed = 1.0;
        }
        config.speed = Math.clamp(config.speed, 0.1, 5.0);
    }
}

final class ExampleConfigManager {
    private static final Logger LOGGER = LoggerFactory.getLogger("examplemod");
    private static JsonConfigStore<ExampleConfig> store;

    static void initialize(Path configDirectory) {
        store = new JsonConfigStore<>(
            configDirectory.resolve("examplemod.json"),
            ExampleConfig.class,
            ExampleConfig::new,
            ExampleConfig::sanitize,
            LOGGER
        );
        store.load();
    }

    static ExampleConfig get() {
        return store.get();
    }

    static boolean save() {
        return store.save();
    }
}

Pass your loader's config directory to initialize during client startup. load() creates a missing file with defaults. If JSON is malformed, Elib backs up the file with a .broken-<timestamp> suffix before saving defaults. If the file cannot be read for another I/O reason, Elib uses defaults in memory without overwriting it.

get() returns the active, mutable object. Update it and call save() when your mod decides to persist a change. save() returns false if the write fails. The sanitizer must modify the supplied object in place; it cannot replace the object.

JsonConfigStore has no built-in reset button. To reset, restore your fields to their defaults, save, and then optionally call ElibConfigNotifications.reset() after a successful save. See UI-Helpers.

For a complete working example, see PieChart's config manager.

Clone this wiki locally