Package net.blockhost.commons.config


@NullMarked package net.blockhost.commons.config

Quick Start

Basic Configuration

Define your configuration class using ConfigLib annotations:

@Configuration
public class MyPluginConfig {
    @Comment("The server host address")
    private String host = "localhost";

    @Comment("The server port")
    private int port = 3306;

    @Comment("Database settings")
    private DatabaseSettings database = new DatabaseSettings();
}

Load your configuration:

Path configPath = plugin.getDataFolder().toPath().resolve("config.yml");
var properties = ConfigLoader.defaultPropertiesBuilder().build();
MyPluginConfig config = ConfigLoader.loadOrCreate(configPath, MyPluginConfig.class, properties);

Versioned Configuration with Migration

For configurations that may change over time, use VersionAwareConfiguration:

@Configuration
public class MyPluginConfig extends VersionAwareConfiguration {
    private static final int CURRENT_VERSION = 2;

    private String hostname = "localhost";
    private int timeout = 30;

    public MyPluginConfig() {
        super(CURRENT_VERSION);
    }
}

Set up migrations and load:

ConfigMigrator migrator = ConfigMigrator.builder()
    .createBackups(true)
    .register(Migration.of(2, "Rename host to hostname", ctx -> {
        ctx.rename("host", "hostname");
    }))
    .build();

MyPluginConfig config = migrator.migrateAndLoad(configPath, MyPluginConfig.class, 2);

Package Structure

See Also: