Skip to content

Commit

Permalink
+ config system
Browse files Browse the repository at this point in the history
  • Loading branch information
FiLKoNd committed Dec 5, 2024
1 parent 4b6665c commit fc81353
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 9 deletions.
10 changes: 3 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ subprojects {
apply(plugin = "maven-publish")

group = "com.filkond"
version = "1.1.1"
version = "1.1.3"

repositories {
mavenCentral()
Expand All @@ -24,6 +24,7 @@ subprojects {

dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compileOnly(kotlin("reflect"))
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
}

Expand Down Expand Up @@ -57,11 +58,6 @@ subprojects {
delete("$rootDir/target")
}

java {
withSourcesJar()
withJavadocJar()
}

kotlin {
jvmToolchain(17)
}
Expand All @@ -83,9 +79,9 @@ subprojects {
}
publications {
create<MavenPublication>(project.name) {
from(components["kotlin"])
artifact(tasks["sourcesJar"])
artifact(tasks["javadocJar"])
artifact(tasks.shadowJar)
artifactId = project.name
groupId = project.group.toString()
version = project.version.toString()
Expand Down
1 change: 1 addition & 0 deletions paperktlib-config/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ plugins {

dependencies {
compileOnly(project(":paperktlib-adventure"))
compileOnly("com.charleskorn.kaml:kaml:0.66.0")
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.filkond.paperktlib.config

import com.charleskorn.kaml.Yaml
import com.filkond.paperktlib.config.ext.loadConfigFromFileOrDefault
import com.filkond.paperktlib.config.ext.update
import kotlinx.serialization.StringFormat
import kotlinx.serialization.json.Json
import java.io.File
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance

abstract class ConfigManager(
protected val folder: File,
private val formatter: StringFormat
) {
val configs: MutableMap<File, Config> = mutableMapOf()

protected inline fun <reified T : Config> config(fileName: String): T {
val file = File(folder, fileName)
return config(file, T::class)
}

protected fun <T : Config> config(file: File, clazz: KClass<T>): T {
val config = loadConfig(file, clazz)
configs[file] = config
return config
}

inline fun <reified T : Config> T.reload() {
configs.values.first { T::class == it::class }.update(this)
}

fun reload(fileName: String) {
val file = File(folder, fileName)
configs[file]!!.update(file)
}

fun reloadAll() {
configs.forEach { it.value.update(it.key) }
}

private inline fun <reified T : Config> T.update(file: File) = loadConfig(file, T::class).update(this)
private fun <T : Config> loadConfig(file: File, clazz: KClass<T>): T =
loadConfigFromFileOrDefault(formatter, file, clazz) {
clazz.createInstance()
}
}
class JsonConfigManager(folder: File, json: Json = Json) : ConfigManager(folder, json)
class YamlConfigManager(folder: File, yaml: Yaml = Yaml()) : ConfigManager(folder, yaml)

interface Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@file:OptIn(InternalSerializationApi::class)
package com.filkond.paperktlib.config.ext

import com.filkond.paperktlib.config.Config
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.StringFormat
import kotlinx.serialization.serializer
import org.apache.logging.log4j.LogManager
import java.io.File
import java.time.LocalDate
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty

private val logger = LogManager.getLogger()

fun <T : Config> T.update(newObject: T) {
val clazz = this::class
clazz.members.filterIsInstance<KMutableProperty<*>>().forEach {
it.setter.call(this, it.getter.call(newObject))
}
}

fun <T : Config> loadConfigFromFileOrDefault(formatter: StringFormat, file: File, clazz: KClass<T>, getDefault: () -> T): T {
return try {
formatter.decodeFromString(clazz.serializer(), file.readText())
} catch (e: Exception) {
logger.warn("Failed to load config ${file.name}, using default: $e")
file.copyTo(File(file.parentFile, "${file.nameWithoutExtension}-backup-${LocalDate.now()}-.${file.extension}"))
writeAndGetDefaultConfig(formatter, file, getDefault, clazz)
}
}

private fun <T : Config> writeAndGetDefaultConfig(formatter: StringFormat, file: File, getDefault: () -> T, clazz: KClass<T>): T {
file.parentFile.mkdirs()
file.createNewFile()
return getDefault().also {
file.writeText(formatter.encodeToString(clazz.serializer(), it))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ data class LocationSection(
val x: Double,
val y: Double,
val z: Double,
val yaw: Float,
val pitch: Float
val yaw: Float = 0.0F,
val pitch: Float = 0.0F
) {
fun toLocation(): Location = Location(Bukkit.getWorld(world), x, y, z, yaw, pitch)
}
1 change: 1 addition & 0 deletions paperktlib-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation("com.charleskorn.kaml:kaml:0.66.0")
implementation(kotlin("reflect"))

implementation(project(":paperktlib-towny"))
Expand Down

0 comments on commit fc81353

Please sign in to comment.