Usage

KotlinBukkitAPI provides a module apart for using Kotlinx.serialization.

Add this following dependency to your gradle build.

compileOnly("tech.carcadex.kotlinbukkitkit:architecture:$kotlinbukkitkit_version")
compileOnly("tech.carcadex.kotlinbukkitkit:serialization:$kotlinbukkitkit_version")

And setup your project with Kotlinx.serialization gradle plugin.

KotlinPlugin

For setup your configuration file using Kotlinx.serialization and KotlinBukkitAPI you will need to declare with in a KotlinPlugin class.

First declare your example configuration models.

@Serializable
data class YourConfig(
  @ChangeColor
  val noPermissionMessage: String = "&cYou don't have permission to use this command",
  val commandsMessages: CommandsMessage = CommandsMessages()
)

@Serializable
data class CommandsMessage(
  @ChangeColor
  val onTeleportHome: String = "&eYou teleport to your home."
)

And now create declare at your config at KotlinPlugin.

Notes that the KotlinBukkitAPI will create the file and set the defaultModel as the default for the configuration and will load from the file.

class MyPlugin : KotlinPlugin() {
  val yourConfig = config(
      "config.yml",
      YourConfig(),
      YourConfig.serializer()
  )
}

We can do more with the config()

fun <T : Any> KotlinPlugin.config(
        file: String,
        defaultModel: T,
        serializer: KSerializer<T>,
        type: StringFormat = Yaml(BukkitSerialModule()),
        loadOnEnable: Boolean = false,
        saveOnDisable: Boolean = false,
        alwaysRestoreDefaults: Boolean = true
): SerializationConfig<T>
  • You can use custom StringFormats from Kotlinx.serialization like Json with the type parameter.

  • You can set to your config only loads on the Plugin onEnable with loadOnEnable.

  • You can set to your config be saved when the plugins disable/server closes with saveOnDisable.

Last updated