forked from SuperRicky14/TpaPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Add config file reader
- Loading branch information
1 parent
e1094ae
commit 36f2218
Showing
14 changed files
with
407 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/main/kotlin/net/superricky/tpaplusplus/config/AdvancedSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/main/kotlin/net/superricky/tpaplusplus/config/CommonSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/main/kotlin/net/superricky/tpaplusplus/config/Config.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandCooldownSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandDelaySpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandDistanceSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandEnableSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandLimitationsSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/superricky/tpaplusplus/config/command/CommandNameSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
Oops, something went wrong.