Skip to content

Repository files navigation

ktConfig v2

Spigot configuration library for Kotlin using class annotations. The library generates configuration loaders at build-time, ensuring zero runtime overhead (except for YamlConfiguration operations).

⚡ Features

  • 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).

📦 Installation

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 {
maven(url ="https://central.sonatype.com/repository/maven-snapshots/")
}
dependencies {
implementation("dev.s7a:ktConfig:2.0.0-SNAPSHOT")
ksp("dev.s7a:ktConfig-ksp:2.0.0-SNAPSHOT")
}
  • Auto generate configuration loaders on build: ./gradlew build.
  • Manually generate loaders: ./gradlew kspKotlin (maybe required)

📝 Quick Example

Add the @KtConfig annotation to your data class

@KtConfig
data classServerConfig(
valserverName:String,
valmaxPlayers:Int
)

The loader class will be defined, allowing you to perform loading and saving operations.

overridefunonEnable() {
val file = plugin.dataFolder.resolve("config.yml")
val config =ServerConfigLoader.load(file)
// config.serverName : "My Server"// config.maxPlayers : 100
}
serverName: "My Server"maxPlayers: 100

The loader class provides the following methods:

  • load(File): T
  • loadFromString(String): T
  • save(T, File)
  • saveToString(T): String

🚀 Usage

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.

Adding Comments

You can add comments to the generated YAML file using the @Comment annotation.

@KtConfig
@Comment("Global settings")
data classAppConfig(
@Comment("Enable debug mode")
valdebug:Boolean
)

Change the YAML Path Name

You can customize the YAML path name using the @PathName annotation.

@KtConfig
data classServerConfig(
@PathName("server-name")
valserverName:String
)

The YAML file will look like this:

server-name: "My Server"

Default Values

You can support Kotlin's default values by adding hasDefault = true property to your @KtConfig annotation.

@KtConfig(hasDefault =true)
data classAppConfig(
valmessage:String = "Hello",
valcount:Int = 10
)

Warning

  1. 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 classAppConfig(
valmessage:String = "Hello"
)
// ❌ This is invalid
@KtConfig(hasDefault =true)
data classAppConfig(
valmessage:String,
valcount:Int = 10
)
  1. 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 classAppConfig(
valmessage:String = "Hello",
valcount:Int = 10,
vallist:List<String> = listOf("a", "b")
)
// ❌ This is invalid
@KtConfig(hasDefault =true)
data classAppConfig(
valtimestamp:Long = System.currentTimeMillis(), // Not staticvalrandom:Int = Random().nextInt(), // Not staticvaluuid:UUID = UUID.randomUUID() // Not static
)
  1. 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 fileAppConfigLoader.save(file, config)

Custom Serializers

You can define custom serialization logic for specific types.

1. Using TransformSerializer (Recommended)

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 Stringobject WrapperSerializer : TransformSerializer<Wrapper, String>(StringSerializer) {
overridefundecode(value:String): Wrapper {
returnWrapper(value) // Convert String -> Wrapper
}
overridefunencode(value:Wrapper): String {
return value.data // Convert Wrapper -> String
}
}

2. Implementing Serializer Interface

For full control, implement the Serializer<T> interface directly. Classes implementing Serializer.Keyable<T> can be used as Map keys.

object MyTypeSerializer : Serializer<MyType> {
overridefundeserialize(value:Any): MyType {
// Convert raw YAML value (Map, String, Int, etc.) to MyType
}
overridefunserialize(value:MyType): Any? {
// Convert MyType to a YAML-compatible format
}
}

Applying the Serializer

Use the @UseSerializer annotation on the property to apply your custom serializer.

@KtConfig
data classCustomConfig(
valdata: @UseSerializer(WrapperSerializer::class) Wrapper
)

📦 Supported Types

ktConfig supports the following types:

Primitives

  • Boolean
  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double
  • Char
  • String
  • UByte
  • UShort
  • UInt
  • ULong
  • BigInteger
  • BigDecimal

Collections

  • List
  • Set
  • Map
  • ArrayDeque
  • Array
  • BooleanArray
  • ByteArray
  • CharArray
  • ShortArray
  • IntArray
  • LongArray
  • FloatArray
  • DoubleArray
  • UByteArray
  • UShortArray
  • UIntArray
  • ULongArray

Others

  • org.bukkit.configuration.serialization.ConfigurationSerializable : ItemStack, Location, ...
  • java.util.UUID
  • java.time.Instant
  • java.time.LocalTime
  • java.time.LocalDate
  • java.time.LocalDateTime
  • java.time.Year
  • java.time.YearMonth
  • java.time.OffsetTime
  • java.time.OffsetDateTime
  • java.time.ZonedDateTime
  • java.time.Duration
  • java.time.Period
  • Enum classes
  • Inline value classes

Formatted Types

ktConfig provides several formatted types for easier string-based serialization:

These types are automatically serialized to and from their string representations.

🔧 Troubleshooting

Unsupported type

Log example:

[ksp] Unsupported type: java.util.Date

If you encounter an error when using a type that is not supported by ktConfig.

@KtConfig
classInvalidConfig(
valdate: 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 classConfig(
valdate: @UseSerializer(DateSerializer::class) java.util.Date,
)

Alternatively, handle YAML using supported types and convert them externally.

@KtConfig
data classConfig(
valinstant: java.time.Instant, // Supported type
) {
val date
get() =Date.from(instant)
}

Unresolve reference properties

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
classInvalidConfig(
text:String,
)

Using data classes is recommended as they enforce val/var declarations for all primary constructor properties automatically.

@KtConfig
data classConfig(
valtext:String,
)

Unresolved reference using custom serializers

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.

classDateSerializer : Serializer<java.util.Date> {
// ...
}

Should be:

object DateSerializer : Serializer<java.util.Date> {
// ...
}

Mismatched dependency versions

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.

🔑 License

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.

About

Spigot configuration library for Kotlin using class annotations.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages