Skip to content

Commit

Permalink
feat(config): Add config file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 12, 2024
1 parent e1094ae commit 36f2218
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 16 deletions.
31 changes: 27 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ val modName: String by project
val modVersion: String by project
val mavenGroup: String by project
val targetJavaVersion = 21
val javaVersion = JavaVersion.VERSION_21

version = "$modVersion${getVersionMetadata()}"
group = mavenGroup
Expand All @@ -28,12 +29,13 @@ repositories {
}

dependencies {
detektPlugins(libs.detekt)
minecraft(libs.minecraft)
mappings(variantOf(libs.yarn.mappings) { classifier("v2") })
modImplementation(libs.bundles.fabric)
modImplementation(libs.architectury)
modImplementation(libs.bundles.konf)
shadow(libs.bundles.konf)
detektPlugins(libs.detekt)
}

base {
Expand All @@ -42,6 +44,8 @@ base {

java {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
withSourcesJar()
}

Expand Down Expand Up @@ -87,9 +91,28 @@ tasks {
}

jar {
from("LICENSE") {
rename { "${it}_${project.base.archivesName}" }
}
from("LICENSE")
}

shadowJar {
from("LICENSE")

configurations = listOf(
project.configurations.shadow.get()
)
archiveClassifier.set("dev-all")

exclude("kotlin/**", "kotlinx/**", "javax/**")
exclude("org/intellij/**", "org/jetbrains/annotations/**")
exclude("org/slf4j/**")

val relocatePath = "net.superricky.tpaplusplus.libs."
relocate("com.moandjiezana.toml", relocatePath + "com.moandjiezana.toml")
}

remapJar {
dependsOn(shadowJar)
inputFile = shadowJar.get().archiveFile
}
}

Expand Down
12 changes: 6 additions & 6 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ comments:
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**']
DeprecatedBlockTag:
active: true
UndocumentedPublicClass:
active: true
UndocumentedPublicFunction:
active: true
UndocumentedPublicProperty:
active: true

complexity:
active: true
Expand Down Expand Up @@ -183,6 +179,8 @@ formatting:
naming:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**']
MemberNameEqualsClassName:
active: false
BooleanPropertyNaming:
active: true
FunctionMaxLength:
Expand Down Expand Up @@ -241,10 +239,12 @@ style:
BracesOnIfStatements:
active: true
multiLine: consistent
WildcardImport:
active: false
UnusedParameter:
active: false
BracesOnWhenStatements:
active: true
CascadingCallWrapping:
active: true
ClassOrdering:
active: true
CollapsibleIfStatements:
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/Const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.superricky.tpaplusplus

import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

object Const {
const val MOD_ID = "tpaplusplus"
const val CONFIG_FOLDER_PATH = MOD_ID
const val CONFIG_FILE_NAME = "$MOD_ID.toml"
const val CONFIG_FILE_PATH = "$CONFIG_FOLDER_PATH/$CONFIG_FILE_NAME"
val logger: Logger = LogManager.getLogger(MOD_ID)
}
50 changes: 44 additions & 6 deletions src/main/kotlin/net/superricky/tpaplusplus/TpaPlusPlus.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
package net.superricky.tpaplusplus

import net.fabricmc.api.ModInitializer
import org.apache.logging.log4j.LogManager
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.server.MinecraftServer
import net.superricky.tpaplusplus.Const.CONFIG_FILE_NAME
import net.superricky.tpaplusplus.Const.CONFIG_FILE_PATH
import net.superricky.tpaplusplus.Const.CONFIG_FOLDER_PATH
import net.superricky.tpaplusplus.Const.MOD_ID
import net.superricky.tpaplusplus.Const.logger
import net.superricky.tpaplusplus.config.Config
import java.nio.file.Files

/**
* Main class
*/
class TpaPlusPlus : ModInitializer {
private val logger = LogManager.getLogger("TpaPlusPlus")

override fun onInitialize() {
logger.info("TPA plus started")
val version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version
logger.info("Initializing TPA++ ${version.friendlyString}")

if (!Files.isDirectory(FabricLoader.getInstance().configDir.resolve(CONFIG_FOLDER_PATH))) {
logger.info("Config folder not exist, Creating.")
Files.createDirectories(FabricLoader.getInstance().configDir.resolve(CONFIG_FOLDER_PATH))
}

if (!Files.exists(FabricLoader.getInstance().configDir.resolve(CONFIG_FILE_PATH))) {
logger.info("No config file, Creating")
Files.copy(
FabricLoader.getInstance().getModContainer(MOD_ID).get().findPath(CONFIG_FILE_NAME).get(),
FabricLoader.getInstance().configDir.resolve(CONFIG_FILE_PATH)
)
}
logger.info("Loading config file...")
try {
Config.loadAndVerifyConfig()
logger.info("Config file loaded.")
} catch (e: Exception) {
logger.error("Error while loading config file", e)
return
}

ServerLifecycleEvents.SERVER_STARTING.register(::serverStarting)
ServerLifecycleEvents.SERVER_STOPPED.register(::serverStopped)
}

private fun serverStarting(server: MinecraftServer) {
logger.info("Starting TPA++ server")
}

private fun serverStopped(server: MinecraftServer) {
logger.info("Shutting down TPA++")
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/config/AdvancedSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.superricky.tpaplusplus.config

import com.uchuhimo.konf.ConfigSpec

object AdvancedSpec : ConfigSpec("advanced") {
val asyncLoopRate by required<Int>()
val unblockingTickLoop by required<Boolean>()
val autoSaveInterval by required<Int>()
val updateCheckInterval by required<Int>()
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/config/CommonSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.superricky.tpaplusplus.config

import com.uchuhimo.konf.ConfigSpec

object CommonSpec : ConfigSpec("common") {
val showBlockedMessage by required<Boolean>()
val toggledPlayerCommand by required<Boolean>()
val tpaTimeout by required<Int>()
val waitTimeBeforeTp by required<Int>()
}
39 changes: 39 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/config/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.superricky.tpaplusplus.config

import com.uchuhimo.konf.Config
import com.uchuhimo.konf.source.toml
import net.fabricmc.loader.api.FabricLoader
import net.superricky.tpaplusplus.Const
import net.superricky.tpaplusplus.config.command.*

object Config {
private val config: Config = Config {
addSpec(CommonSpec)
addSpec(AdvancedSpec)
addSpec(CommandEnableSpec)
addSpec(CommandNameSpec)
addSpec(CommandDelaySpec)
addSpec(CommandCooldownSpec)
addSpec(CommandDistanceSpec)
addSpec(CommandLimitationsSpec)
}
.from.toml.resource(Const.CONFIG_FILE_NAME)
.from.toml.watchFile(FabricLoader.getInstance().configDir.resolve(Const.CONFIG_FILE_PATH).toFile())
.from.env()
.from.systemProperties()

/**
* @return Config instance
*/
fun getConfig(): Config {
return config
}

/**
* Load and check config file.
* Please call this function before use config.
*/
fun loadAndVerifyConfig() {
config.validateRequired()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandCooldownSpec : ConfigSpec("command.cooldown") {
val globalCooldown by required<Double>()
val denyCooldown by required<Double>()
val cancelCooldown by required<Double>()
val acceptCooldown by required<Double>()
val tpahereCooldown by required<Double>()
val backCooldown by required<Double>()
val blockCooldown by required<Double>()
val toggleCooldown by required<Double>()
val unblockCooldown by required<Double>()
val tpaCooldown by required<Double>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandDelaySpec : ConfigSpec("command.delay") {
val denyDelay by required<Double>()
val cancelDelay by required<Double>()
val acceptDelay by required<Double>()
val tpahereDelay by required<Double>()
val backDelay by required<Double>()
val blockDelay by required<Double>()
val toggleDelay by required<Double>()
val unblockDelay by required<Double>()
val tpaDelay by required<Double>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandDistanceSpec : ConfigSpec("command.distance") {
val denyDistance by required<Double>()
val cancelDistance by required<Double>()
val acceptDistance by required<Double>()
val tpahereDistance by required<Double>()
val backDistance by required<Double>()
val blockDistance by required<Double>()
val toggleDistance by required<Double>()
val unblockDistance by required<Double>()
val tpaDistance by required<Double>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandEnableSpec : ConfigSpec("command.enable") {
val backEnable by required<Boolean>()
val tpacancelEnable by required<Boolean>()
val tpaunblockEnable by required<Boolean>()
val tpablockEnable by required<Boolean>()
val tpatoggleEnable by required<Boolean>()
val tpahereEnable by required<Boolean>()
val tpaacceptEnable by required<Boolean>()
val tpadenyEnable by required<Boolean>()
val tpaEnable by required<Boolean>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandLimitationsSpec : ConfigSpec("command.limitations") {
val crossDimAllowed by required<Boolean>()
val maxTpDistance by required<Double>()
val minTpDistance by required<Double>()
val ignoreDistanceCrossDim by required<Boolean>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.superricky.tpaplusplus.config.command

import com.uchuhimo.konf.ConfigSpec

object CommandNameSpec : ConfigSpec("command.name") {
val backCommand by required<String>()
val tpacancelCommand by required<String>()
val tpaunblockCommand by required<String>()
val tpablockCommand by required<String>()
val tpatoggleCommand by required<String>()
val tpahereCommand by required<String>()
val tpaacceptCommand by required<String>()
val tpadenyCommand by required<String>()
val tpaCommand by required<String>()
}
Loading

0 comments on commit 36f2218

Please sign in to comment.