Skip to content

Commit a84d791

Browse files
committed
docs: more docs
1 parent 8648532 commit a84d791

File tree

9 files changed

+269
-10
lines changed

9 files changed

+269
-10
lines changed

.idea/php.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/command/SurfCommandUtil.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,40 @@ package dev.slne.surf.surfapi.core.api.command
33
import dev.slne.surf.surfapi.core.api.command.builder.CommandExceptionBuilder
44
import dev.slne.surf.surfapi.core.api.command.exception.WrapperCommandExceptionComponent
55
import net.kyori.adventure.text.ComponentLike
6-
import org.jetbrains.annotations.ApiStatus
76

8-
@ApiStatus.NonExtendable
7+
/**
8+
* Utility object for command-related operations and error handling in the Surf API.
9+
* Provides methods to create and throw custom command exceptions with user-friendly error messages.
10+
*/
911
object SurfCommandUtil {
12+
13+
/**
14+
* Creates a custom exception with the given message.
15+
*
16+
* @param message The message to include in the exception, as a [ComponentLike].
17+
* @return An instance of [WrapperCommandExceptionComponent].
18+
*/
1019
@JvmStatic
1120
fun createException(message: ComponentLike) =
1221
WrapperCommandExceptionComponent(message.asComponent())
1322

23+
/**
24+
* Throws a custom command exception with the given message.
25+
*
26+
* @param message The message to include in the exception, as a [ComponentLike].
27+
* @throws WrapperCommandExceptionComponent Always throws the created exception.
28+
*/
1429
@JvmStatic
1530
fun failWithMessage(message: ComponentLike) {
1631
throw createException(message)
1732
}
1833

34+
/**
35+
* Throws a custom command exception using a provided [CommandExceptionBuilder].
36+
*
37+
* @param builder The builder used to construct the exception message.
38+
* @throws WrapperCommandExceptionComponent Always throws the created exception.
39+
*/
1940
@JvmStatic
2041
fun failWithBuilder(builder: CommandExceptionBuilder) {
2142
throw createException(builder.build())

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/command/exception/WrapperCommandExceptionComponent.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import dev.slne.surf.surfapi.core.api.messages.ComponentMessage
77
import net.kyori.adventure.text.Component
88
import java.io.Serial
99

10+
/**
11+
* A wrapper for [CommandSyntaxException] to integrate custom error messages
12+
* as [Component] objects within the Surf API.
13+
*
14+
* @param errorMessage The error message to associate with the exception.
15+
*/
1016
class WrapperCommandExceptionComponent(errorMessage: Component) : WrapperCommandSyntaxException(
1117
CommandSyntaxException(
1218
SimpleCommandExceptionType(

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/config/SurfConfigApi.kt

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,180 @@ import dev.slne.surf.surfapi.core.api.config.manager.SpongeConfigManager
55
import dev.slne.surf.surfapi.core.api.util.requiredService
66
import java.nio.file.Path
77

8+
/**
9+
* API for managing configuration files in the Surf API, supporting both Sponge and DazzlConf configurations.
10+
* Provides methods to create, retrieve, and reload configuration files in various formats (YAML, JSON).
11+
*/
812
interface SurfConfigApi {
913

14+
/**
15+
* Creates a DazzlConf configuration file.
16+
*
17+
* @param C The type of the configuration class.
18+
* @param configClass The class of the configuration.
19+
* @param configFolder The folder where the configuration file is stored.
20+
* @param configFileName The name of the configuration file. Must follow the YAML file name pattern.
21+
* @return An instance of the configuration class [C].
22+
*/
1023
@PreferUsingSpongeConfigOverDazzlConf
1124
fun <C> createDazzlConfig(
1225
configClass: Class<C>,
1326
configFolder: Path,
14-
configFileName: @YamlConfigFileNamePattern String
27+
configFileName: @YamlConfigFileNamePattern String,
1528
): C
1629

30+
/**
31+
* Retrieves a DazzlConf configuration.
32+
*
33+
* @param C The type of the configuration class.
34+
* @param configClass The class of the configuration.
35+
* @return An instance of the configuration class [C].
36+
*/
1737
@PreferUsingSpongeConfigOverDazzlConf
1838
fun <C> getDazzlConfig(configClass: Class<C>): C
1939

40+
/**
41+
* Reloads a DazzlConf configuration from the file.
42+
*
43+
* @param C The type of the configuration class.
44+
* @param configClass The class of the configuration.
45+
* @return The reloaded instance of the configuration class [C].
46+
*/
2047
@PreferUsingSpongeConfigOverDazzlConf
2148
fun <C> reloadDazzlConfig(configClass: Class<C>): C
2249

50+
/**
51+
* Creates a Sponge YAML configuration file.
52+
*
53+
* @param C The type of the configuration class.
54+
* @param configClass The class of the configuration.
55+
* @param configFolder The folder where the configuration file is stored.
56+
* @param configFileName The name of the configuration file. Must follow the YAML file name pattern.
57+
* @return An instance of the configuration class [C].
58+
*/
2359
fun <C> createSpongeYmlConfig(
2460
configClass: Class<C>,
2561
configFolder: Path,
26-
configFileName: @YamlConfigFileNamePattern String
62+
configFileName: @YamlConfigFileNamePattern String,
2763
): C
2864

65+
/**
66+
* Creates a Sponge JSON configuration file.
67+
*
68+
* @param C The type of the configuration class.
69+
* @param configClass The class of the configuration.
70+
* @param configFolder The folder where the configuration file is stored.
71+
* @param configFileName The name of the configuration file. Must follow the JSON file name pattern.
72+
* @return An instance of the configuration class [C].
73+
*/
2974
fun <C> createSpongeJsonConfig(
3075
configClass: Class<C>,
3176
configFolder: Path,
32-
configFileName: @JsonConfigFileNamePattern String
77+
configFileName: @JsonConfigFileNamePattern String,
3378
): C
3479

80+
/**
81+
* Retrieves a Sponge configuration.
82+
*
83+
* @param C The type of the configuration class.
84+
* @param configClass The class of the configuration.
85+
* @return An instance of the configuration class [C].
86+
*/
3587
fun <C> getSpongeConfig(configClass: Class<C>): C
3688

89+
/**
90+
* Reloads a Sponge configuration from the file.
91+
*
92+
* @param C The type of the configuration class.
93+
* @param configClass The class of the configuration.
94+
* @return The reloaded instance of the configuration class [C].
95+
*/
3796
fun <C> reloadSpongeConfig(configClass: Class<C>): C
3897

98+
/**
99+
* Retrieves the [SpongeConfigManager] for a specific configuration class.
100+
*
101+
* @param C The type of the configuration class.
102+
* @param configClass The class of the configuration.
103+
* @return An instance of [SpongeConfigManager] for the configuration class [C].
104+
*/
39105
fun <C> getSpongeConfigManagerForConfig(configClass: Class<C>): SpongeConfigManager<C>
40106

41107
companion object {
108+
/**
109+
* Retrieves the singleton instance of [SurfConfigApi].
110+
*/
42111
val instance = requiredService<SurfConfigApi>()
43112
}
44113
}
45114

115+
/**
116+
* Retrieves the singleton instance of [SurfConfigApi].
117+
*/
46118
val surfConfigApi get() = SurfConfigApi.instance
47119

120+
/**
121+
* Creates a DazzlConf configuration using a reified type.
122+
*
123+
* @param C The type of the configuration class.
124+
* @param configFolder The folder where the configuration file is stored.
125+
* @param configFileName The name of the configuration file. Must follow the YAML file name pattern.
126+
* @return An instance of the configuration class [C].
127+
*/
48128
@PreferUsingSpongeConfigOverDazzlConf
49129
inline fun <reified C> SurfConfigApi.createDazzlConfig(
50130
configFolder: Path,
51-
configFileName: @YamlConfigFileNamePattern String
131+
configFileName: @YamlConfigFileNamePattern String,
52132
) = createDazzlConfig(C::class.java, configFolder, configFileName)
53133

134+
/**
135+
* Retrieves a DazzlConf configuration using a reified type.
136+
*
137+
* @param C The type of the configuration class.
138+
* @return An instance of the configuration class [C].
139+
*/
54140
@PreferUsingSpongeConfigOverDazzlConf
55141
inline fun <reified C> SurfConfigApi.getDazzlConfig() = getDazzlConfig(C::class.java)
56142

143+
/**
144+
* Reloads a DazzlConf configuration using a reified type.
145+
*
146+
* @param C The type of the configuration class.
147+
* @return The reloaded instance of the configuration class [C].
148+
*/
57149
@PreferUsingSpongeConfigOverDazzlConf
58150
inline fun <reified C> SurfConfigApi.reloadDazzlConfig() = reloadDazzlConfig(C::class.java)
59151

152+
/**
153+
* Creates a Sponge YAML configuration using a reified type.
154+
*
155+
* @param C The type of the configuration class.
156+
* @param configFolder The folder where the configuration file is stored.
157+
* @param configFileName The name of the configuration file. Must follow the YAML file name pattern.
158+
* @return An instance of the configuration class [C].
159+
*/
60160
inline fun <reified C> SurfConfigApi.createSpongeYmlConfig(
61161
configFolder: Path,
62-
configFileName: @YamlConfigFileNamePattern String
162+
configFileName: @YamlConfigFileNamePattern String,
63163
) = createSpongeYmlConfig(C::class.java, configFolder, configFileName)
64164

165+
/**
166+
* Creates a Sponge JSON configuration using a reified type.
167+
*
168+
* @param C The type of the configuration class.
169+
* @param configFolder The folder where the configuration file is stored.
170+
* @param configFileName The name of the configuration file. Must follow the JSON file name pattern.
171+
* @return An instance of the configuration class [C].
172+
*/
65173
inline fun <reified C> SurfConfigApi.createSpongeJsonConfig(
66174
configFolder: Path,
67-
configFileName: @JsonConfigFileNamePattern String
175+
configFileName: @JsonConfigFileNamePattern String,
68176
) = createSpongeJsonConfig(C::class.java, configFolder, configFileName)
69177

178+
/**
179+
* Retrieves a Sponge configuration using a reified type.
180+
*
181+
* @param C The type of the configuration class.
182+
* @return An instance of the configuration class [C].
183+
*/
70184
inline fun <reified C> SurfConfigApi.getSpongeConfig() = getSpongeConfig(C::class.java)

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/config/common.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@ private const val YAML_CONFIG_FILE_NAME_PATTERN = "^[a-zA-Z0-9_-]+\\.(yml|yaml)$
99
@Language("RegExp")
1010
private const val JSON_CONFIG_FILE_NAME_PATTERN = "^[a-zA-Z0-9_-]+\\.(json)$"
1111

12+
/**
13+
* Annotation to specify that a file name must follow a YAML configuration file name pattern.
14+
* The file name must match the regular expression: `^[a-zA-Z0-9_-]+\\.(yml|yaml)$`.
15+
*/
1216
@Pattern(YAML_CONFIG_FILE_NAME_PATTERN)
1317
@Retention(AnnotationRetention.RUNTIME)
1418
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE, AnnotationTarget.TYPE_PARAMETER)
1519
@MustBeDocumented
1620
annotation class YamlConfigFileNamePattern
1721

22+
/**
23+
* Annotation to specify that a file name must follow a JSON configuration file name pattern.
24+
* The file name must match the regular expression: `^[a-zA-Z0-9_-]+\\.(json)$`.
25+
*/
1826
@Pattern(JSON_CONFIG_FILE_NAME_PATTERN)
1927
@Retention(AnnotationRetention.RUNTIME)
2028
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE, AnnotationTarget.TYPE_PARAMETER)

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/config/manager/DazzlConfConfigManager.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ import java.util.concurrent.TimeUnit
2323
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
2424
annotation class PreferUsingSpongeConfigOverDazzlConf
2525

26+
/**
27+
* Manages configurations using the DazzlConf library, including loading, saving, and reloading configurations.
28+
* Provides resilience against syntax or invalid data errors and defaults to a valid configuration when such errors occur.
29+
*
30+
* @param C The type of the configuration class.
31+
* @property config The current configuration instance, or `null` if not yet loaded.
32+
*/
2633
@PreferUsingSpongeConfigOverDazzlConf
2734
class DazzlConfConfigManager<C> private constructor(private val helper: ConfigurationHelper<C>) {
2835
@Volatile
2936
var config: C? = null
3037
private set
3138

39+
/**
40+
* Reloads the configuration from the file.
41+
* If a syntax or validation error occurs, a default configuration is used.
42+
*
43+
* @return The reloaded configuration instance.
44+
* @throws RuntimeException if an I/O error or other critical issue occurs.
45+
*/
3246
fun reloadConfig(): C {
3347
try {
3448
config = helper.reloadConfigData()
@@ -65,6 +79,11 @@ class DazzlConfConfigManager<C> private constructor(private val helper: Configur
6579
return config ?: error("Config is null after reload")
6680
}
6781

82+
/**
83+
* Retrieves the current configuration, reloading it if not already loaded.
84+
*
85+
* @return The configuration instance.
86+
*/
6887
fun getOrCreateConfig(): C {
6988
val config = config ?: reloadConfig()
7089
return config
@@ -73,6 +92,15 @@ class DazzlConfConfigManager<C> private constructor(private val helper: Configur
7392
companion object {
7493
private val log = logger()
7594

95+
/**
96+
* Creates a new instance of [DazzlConfConfigManager] for managing a YAML configuration.
97+
*
98+
* @param C The type of the configuration class.
99+
* @param configClass The class of the configuration.
100+
* @param configFolder The folder where the configuration file is stored.
101+
* @param configFileName The name of the configuration file. Must match the YAML file name pattern.
102+
* @return A new instance of [DazzlConfConfigManager].
103+
*/
76104
@JvmStatic
77105
fun <C> create(
78106
configClass: Class<C>,

0 commit comments

Comments
 (0)