1+ package dev.slne.surf.surfapi.core.api.config
2+
3+ import dev.slne.surf.surfapi.core.api.config.manager.SpongeConfigManager
4+ import java.nio.file.Path
5+
6+ /* *
7+ * Base convenience wrapper around a [SpongeConfigManager] for a specific config type [C].
8+ *
9+ * This class is intended to be extended by companion objects of config data classes.
10+ * It provides a simple, type-safe API to:
11+ * - access the current config instance via [getConfig]
12+ * - persist changes via [save]
13+ * - reload the config from disk via [reloadFromFile]
14+ * - perform in-place mutations via [edit]
15+ *
16+ * The actual manager instance is provided by subclasses and typically created by
17+ * a central configuration API (e.g. [surfConfigApi]).
18+ *
19+ * @param C the type of the configuration data object.
20+ * @param configClass the Java class of [C], used by underlying config frameworks
21+ * to create and map configuration instances.
22+ */
23+ sealed class SpongeConfigClass <C >(configClass : Class <C >) {
24+ /* *
25+ * Folder where the configuration file is stored.
26+ *
27+ * Implementations should point this to a plugin- or module-specific config directory.
28+ */
29+ protected abstract val configFolder: Path
30+
31+ /* *
32+ * The name of the configuration file, including its extension
33+ * (for example `settings.yml` or `settings.json`).
34+ */
35+ protected abstract val fileName: String
36+
37+ /* *
38+ * The underlying configuration manager responsible for loading, saving,
39+ * and tracking the config instance of type [C].
40+ */
41+ abstract val manager: SpongeConfigManager <C >
42+
43+ /* *
44+ * Persists the current configuration to disk.
45+ *
46+ * Delegates to [SpongeConfigManager.save].
47+ */
48+ fun save () = manager.save()
49+
50+ /* *
51+ * Reloads the configuration from disk and replaces the current in-memory instance.
52+ *
53+ * Delegates to [SpongeConfigManager.reloadFromFile].
54+ * Use this when external changes to the config file should be picked up at runtime.
55+ */
56+ fun reloadFromFile () = manager.reloadFromFile()
57+
58+ /* *
59+ * Applies mutations to the current config instance in a safe way.
60+ *
61+ * The [block] receives the current config instance as receiver and can freely
62+ * modify its properties. After the block completes, the config will be saved
63+ * automatically if [save] is `true`.
64+ *
65+ * Example:
66+ * ```kotlin
67+ * MyConfig.edit {
68+ * someField = "new value"
69+ * }
70+ * ```
71+ *
72+ * @param save whether to persist the config to disk after applying [block]. Defaults to `true`.
73+ * @param block mutation block executed on the current config instance.
74+ */
75+ inline fun edit (save : Boolean = true, block : C .() -> Unit ) = manager.edit(save, block)
76+
77+ /* *
78+ * Returns the current in-memory configuration instance.
79+ *
80+ * The returned object is managed by the underlying [SpongeConfigManager] and
81+ * will be replaced when [reloadFromFile] is called.
82+ */
83+ fun getConfig (): C = manager.config
84+
85+ /* *
86+ * Dummy initializer to force class loading and companion object initialization.
87+ *
88+ * This is useful in plugin entry points (e.g. `onLoad`) to ensure that:
89+ * - the config manager is constructed
90+ * - the configuration is loaded before first access via [getConfig]
91+ *
92+ * This method is a no-op and can be safely called multiple times.
93+ */
94+ fun init () = Unit
95+ }
0 commit comments