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+ import kotlin.reflect.KClass
6+
7+ /* *
8+ * Wraps a configuration file in YAML format to provide convenient access and modifications via a configuration class.
9+ * Automatically manages the creation, loading, and saving of the configuration through a `SpongeConfigManager`.
10+ *
11+ * @param T The type of the configuration class.
12+ * @param clazz The class of the configuration.
13+ * @param configFolder The folder where the configuration file is located.
14+ * @param fileName The name of the configuration file in YAML format.
15+ *
16+ * Use in your main: val myConfig = surfConfigApi.createYmlConfig<MyConfigClass>(configFolderPath)
17+ */
18+ class YmlConfigWrapper <T : Any >(
19+ private val clazz : KClass <T >,
20+ private val configFolder : Path ,
21+ private val fileName : String
22+ ) {
23+ /* *
24+ * Manages the configuration of type [T] through a Sponge-based configuration manager.
25+ * Handles loading, saving, and reloading of the configuration using the specified file and folder structure.
26+ * The configuration manager ensures that operations such as edits or reloads are applied to the configuration instance.
27+ *
28+ * @see SpongeConfigManager
29+ */
30+ private val configManager: SpongeConfigManager <T >
31+
32+ init {
33+ surfConfigApi.createSpongeYmlConfig(clazz.java, configFolder, fileName)
34+ configManager = surfConfigApi.getSpongeConfigManagerForConfig(clazz.java)
35+ reload()
36+ }
37+
38+ /* *
39+ * Reloads the configuration by delegating the operation to the underlying configuration manager.
40+ *
41+ * This method ensures that the configuration is reloaded from the associated file,
42+ * updating the current in-memory representation of the configuration. If a critical error
43+ * occurs during the reload process, it propagates the exception thrown by the configuration manager.
44+ *
45+ * Recommended to be used when changes to the underlying configuration file have been made and
46+ * need to be reflected within the application.
47+ *
48+ * @throws RuntimeException if a critical error occurs during the reload operation.
49+ */
50+ fun reload () {
51+ configManager.reloadFromFile()
52+ }
53+
54+ /* *
55+ * Edits the configuration by applying the given block of modifications.
56+ * Optionally saves the changes to the configuration file.
57+ *
58+ * @param save Indicates whether the updated configuration should be saved to the file.
59+ * Defaults to `true`, meaning the changes will be persisted.
60+ * @param block A lambda function defining the modifications to be applied to the configuration object.
61+ */
62+ fun edit (save : Boolean = true, block : T .() -> Unit ) {
63+ val config = config
64+ config.block()
65+
66+ if (save) {
67+ configManager.save()
68+ }
69+ }
70+
71+ /* *
72+ * Provides access to the configuration object managed by the `SpongeConfigManager`.
73+ * This property retrieves the current state of the configuration.
74+ *
75+ * @return The configuration data of type [T].
76+ */
77+ val config: T get() = configManager.config
78+ }
79+
80+ /* *
81+ * Creates a YAML configuration wrapper for a specified configuration class.
82+ *
83+ * @param C The type of the configuration class.
84+ * @param configFolder The folder where the configuration file will be stored.
85+ * @param fileName The name of the configuration file, defaulting to "config.yml".
86+ * @return A `YmlConfigWrapper` instance for managing the YAML configuration of type [C].
87+ */
88+ inline fun <reified C : Any > SurfConfigApi.createYmlConfig (
89+ configFolder : Path ,
90+ fileName : String = "config.yml"
91+ ): YmlConfigWrapper <C > {
92+ return YmlConfigWrapper (C ::class , configFolder, fileName)
93+ }
0 commit comments