ConfigRef

class ConfigRef<T : Any>

A live, reload-safe reference to a configuration instance of type T.

ConfigRef solves the stale-reference problem: when a config is reloaded, every class that holds a ConfigRef automatically sees the new values without re-injection or manual wiring.

Three ways to access the current config:

class DuelManager(private val config: ConfigRef<DuelsConfig>) {

// 1. Invoke operator — concise
fun getPort() = config().server.port

// 2. Property access — readable
fun getHost() = config.current.server.host

// 3. Kotlin delegate — zero-boilerplate
private val cfg: DuelsConfig by config
fun getMode() = cfg.game.defaultMode
}

Selecting sub-sections as delegates:

class QueueManager(config: ConfigRef<DuelsConfig>) {
private val messages by config.selecting { it.messages }
private val cooldowns by config.selecting { it.commandCooldowns }

fun onJoin(player: Player) {
player.sendMessage(messages.prefix + messages.queueJoin)
// messages is always the latest value — never stale
}
}

Reacting to changes:

config.onChange { old, new ->
if (old.game.maxDurationMinutes != new.game.maxDurationMinutes) {
restartActiveGames()
}
}

Automatic file watching:

val config = YamlConfigManager.ref<DuelsConfig>(file)
.withAutoReload() // starts file watcher
.onChange { old, new -> logger.info("Config reloaded") }

Since

1.0

Parameters

T

The configuration data class type.

See also

Properties

Link copied to clipboard
val current: T

Returns the current configuration instance.

Functions

Link copied to clipboard
operator fun getValue(thisRef: Any?, property: KProperty<*>): T

Enables Kotlin property delegation to the current config.

Link copied to clipboard
operator fun invoke(): T

Returns the current configuration instance.

Link copied to clipboard
fun onChange(listener: (old: T, new: T) -> Unit): ConfigRef<T>

Registers a listener that is called whenever the config changes.

Link copied to clipboard
fun reload(): T

Reloads the configuration from disk.

Link copied to clipboard
fun save()

Saves the current config state back to the file.

Link copied to clipboard
fun <R> selecting(selector: (T) -> R): ReadOnlyProperty<Any?, R>

Creates a Kotlin property delegate that extracts a sub-section or derived value from the config.

Link copied to clipboard

Stops the automatic file watcher if one is active.

Link copied to clipboard
fun withAutoReload(debounceMs: Long = 500): ConfigRef<T>

Starts a file watcher that automatically reloads the config when the file is modified on disk.