Spigot configuration library for Kotlin using class annotations. The library generates configuration loaders at build-time, ensuring zero runtime overhead (except for YamlConfiguration operations).
- Zero Runtime Overhead: All configuration loaders are generated at build-time (KSP).
- Type-Safe: Fully typed configuration using Kotlin data classes.
- Wide Type Support: Supports primitives, collections, Bukkit types, and more out of the box.
- Rich Features: Built-in support for comments and custom serializers.
- Default Values: Support for default values using Kotlin default values (e.g.,
val count: Int = 0).
Add the following dependencies to your build.gradle.kts
plugins {
kotlin("jvm") version "2.2.21"
id("com.google.devtools.ksp") version "2.3.2"
}
repositories {
mavenCentral()
}
dependencies {
implementation("dev.s7a:ktConfig:2.0.0")
ksp("dev.s7a:ktConfig-ksp:2.0.0")
}- Auto generate configuration loaders on build:
./gradlew build. - Manually generate loaders:
./gradlew kspKotlin(maybe required)
Add the @KtConfig annotation to your data class
@KtConfig
data class ServerConfig(
val serverName: String,
val maxPlayers: Int
)The loader class will be defined, allowing you to perform loading and saving operations.
override fun onEnable() {
val file = plugin.dataFolder.resolve("config.yml")
val config = ServerConfigLoader.load(file)
// config.serverName : "My Server"
// config.maxPlayers : 100
}serverName: "My Server"
maxPlayers: 100The loader class provides the following methods:
load(File): TloadFromString(String): Tsave(T, File)saveToString(T): String
ktConfig provides various annotations to customize configuration behavior:
@KtConfig: Marks a class as a configuration class. Required for code generation.@Comment: Adds comments to configuration headers or properties.@PathName: Customizes the YAML path name for a property.@UseSerializer: Specifies a custom serializer for a property.
You can add comments to the generated YAML file using the @Comment annotation.
@KtConfig
@Comment("Global settings")
data class AppConfig(
@Comment("Enable debug mode")
val debug: Boolean
)You can customize the YAML path name using the @PathName annotation.
@KtConfig
data class ServerConfig(
@PathName("server-name")
val serverName: String
)The YAML file will look like this:
server-name: "My Server"You can support Kotlin's default values by adding hasDefault = true property to your @KtConfig annotation.
@KtConfig(hasDefault = true)
data class AppConfig(
val message: String = "Hello",
val count: Int = 10
)Warning
- All properties MUST have default values.
You cannot mix properties with and without default values in a @KtConfig(hasDefault = true) annotated class.
γ
// π This is valid
@KtConfig(hasDefault = true)
data class AppConfig(
val message: String = "Hello"
)
// β This is invalid
@KtConfig(hasDefault = true)
data class AppConfig(
val message: String,
val count: Int = 10
)- Default values must be static.
Default values are generated once during construction and reused, so they must be static values.
// π This is valid
@KtConfig(hasDefault = true)
data class AppConfig(
val message: String = "Hello",
val count: Int = 10,
val list: List<String> = listOf("a", "b")
)
// β This is invalid
@KtConfig(hasDefault = true)
data class AppConfig(
val timestamp: Long = System.currentTimeMillis(), // Not static
val random: Int = Random().nextInt(), // Not static
val uuid: UUID = UUID.randomUUID() // Not static
)- No Auto-Save for defaults.
If a value is missing in the file and the default value is used during loading, it is not automatically written back to the file. You must manually save the configuration if you want to persist the default values. Example of saving manually:
// Load configuration (uses default values if keys are missing)
val config = AppConfigLoader.load(file)
// Manually save to ensure default values are written to the file
AppConfigLoader.save(file, config)You can define custom serialization logic for specific types.
The easiest way is to transform your type into a supported type (like String or Map).
Extend TransformSerializer<YOUR_TYPE, BASE_TYPE> and pass a base serializer to the constructor.
Classes implementing Serializer.Keyable<T> can be used as Map keys.
// Example: Serialize a custom Wrapper class as a String
object WrapperSerializer : TransformSerializer<Wrapper, String>(StringSerializer) {
override fun decode(value: String): Wrapper {
return Wrapper(value) // Convert String -> Wrapper
}
override fun encode(value: Wrapper): String {
return value.data // Convert Wrapper -> String
}
}For full control, implement the Serializer<T> interface directly.
Classes implementing Serializer.Keyable<T> can be used as Map keys.
object MyTypeSerializer : Serializer<MyType> {
override fun deserialize(value: Any): MyType {
// Convert raw YAML value (Map, String, Int, etc.) to MyType
}
override fun serialize(value: MyType): Any? {
// Convert MyType to a YAML-compatible format
}
}Use the @UseSerializer annotation on the property to apply your custom serializer.
@KtConfig
data class CustomConfig(
val data: @UseSerializer(WrapperSerializer::class) Wrapper
)ktConfig supports the following types:
BooleanByteShortIntLongFloatDoubleCharStringUByteUShortUIntULongBigIntegerBigDecimal
ListSetMapArrayDequeArrayBooleanArrayByteArrayCharArrayShortArrayIntArrayLongArrayFloatArrayDoubleArrayUByteArrayUShortArrayUIntArrayULongArray
org.bukkit.configuration.serialization.ConfigurationSerializable: ItemStack, Location, ...java.util.UUIDjava.time.Instantjava.time.LocalTimejava.time.LocalDatejava.time.LocalDateTimejava.time.Yearjava.time.YearMonthjava.time.OffsetTimejava.time.OffsetDateTimejava.time.ZonedDateTimejava.time.Durationjava.time.Period- Enum classes
- Inline value classes
ktConfig provides several formatted types for easier string-based serialization:
FormattedBlock: Represents org.bukkit.Block (World, X, Y, Z)FormattedBlockVector: Represents org.bukkit.util.BlockVector (X, Y, Z)FormattedColor: Represents org.bukkit.Color (#AARRGGBB,#RRGGBB,AARRGGBB,RRGGBB)FormattedLocation: Represents org.bukkit.Location (World, X, Y, Z,World, X, Y, Z, Yaw, Pitch)FormattedVector: Represents org.bukkit.Vector (X, Y, Z)FormattedWorld: Represents org.bukkit.World by its name (WorldName)
These types are automatically serialized to and from their string representations.
Log example:
[ksp] Unsupported type: java.util.Date
If you encounter an error when using a type that is not supported by ktConfig.
@KtConfig
class InvalidConfig(
val date: java.util.Date, // Unsupported type
)Define a custom serializer and specify it using @UseSerializer annotation to handle this type.
object DateSerializer : Serializer<java.util.Date> {
// ...
}
@KtConfig
data class Config(
val date: @UseSerializer(DateSerializer::class) java.util.Date,
)Alternatively, handle YAML using supported types and convert them externally.
@KtConfig
data class Config(
val instant: java.time.Instant, // Supported type
) {
val date
get() = Date.from(instant)
}Log example:
Unresolved reference 'text'.
If you encounter unresolved reference errors when using ktConfig, make sure your properties are properly declared.
Properties in Kotlin must be declared using val or var to be accessible:
@KtConfig
class InvalidConfig(
text: String,
)Using data classes is recommended as they enforce val/var declarations for all primary constructor properties
automatically.
@KtConfig
data class Config(
val text: String,
)Log example:
Unresolved reference 'getOrThrow'
Unresolved reference 'set'
Inapplicable candidate(s): fun deserialize(value: Any): Date
Unresolved reference 'serialize'
If you encounter unresolved reference errors when using custom serializers, make sure you use objects instead of classes.
class DateSerializer : Serializer<java.util.Date> {
// ...
}Should be:
object DateSerializer : Serializer<java.util.Date> {
// ...
}Log example:
'org.gradle.api.provider.Property org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions.getJvmDefault()'
This error might occur due to version incompatibility between Kotlin and KSP. Please check and ensure that your Kotlin and KSP versions are compatible.
MIT License
Copyright (c) 2025 sya-ri
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.