From c32389178e4d18e590572cb833f053d7aa318495 Mon Sep 17 00:00:00 2001 From: Half_nothing Date: Fri, 11 Oct 2024 15:36:22 +0800 Subject: [PATCH] feat(command): Add ReloadCommand.kt to reload language config --- .../net/superricky/tpaplusplus/GlobalConst.kt | 2 + .../tpaplusplus/command/CommandRegister.kt | 2 + .../command/subcommands/ReloadCommand.kt | 40 +++++++++++++++++++ .../command/subcommands/RootCommand.kt | 3 ++ .../config/language/LanguageConfig.kt | 1 + .../tpaplusplus/config/language/SystemSpec.kt | 2 + src/main/resources/lang/en_us.toml | 2 + src/main/resources/lang/example.toml | 2 + src/main/resources/lang/zh_cn.toml | 2 + src/main/resources/lang/zh_tw.toml | 2 + 10 files changed, 58 insertions(+) create mode 100644 src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/ReloadCommand.kt diff --git a/src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt b/src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt index 33acbb9..98dde3d 100644 --- a/src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt +++ b/src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt @@ -24,5 +24,7 @@ object GlobalConst { const val SERVER_TICK_RATE = 20.0 const val ONE_SECOND = 1000L + const val PERMISSION_LEVEL = 3 + val logger: Logger = LogManager.getLogger(MOD_ID) } diff --git a/src/main/kotlin/net/superricky/tpaplusplus/command/CommandRegister.kt b/src/main/kotlin/net/superricky/tpaplusplus/command/CommandRegister.kt index cb3179a..7ea05bc 100644 --- a/src/main/kotlin/net/superricky/tpaplusplus/command/CommandRegister.kt +++ b/src/main/kotlin/net/superricky/tpaplusplus/command/CommandRegister.kt @@ -3,6 +3,7 @@ package net.superricky.tpaplusplus.command import net.superricky.tpaplusplus.GlobalConst import net.superricky.tpaplusplus.GlobalConst.logger import net.superricky.tpaplusplus.command.commands.* +import net.superricky.tpaplusplus.command.subcommands.ReloadCommand import net.superricky.tpaplusplus.command.subcommands.RootCommand import net.superricky.tpaplusplus.config.config.Config.get import net.superricky.tpaplusplus.config.config.command.CommandEnableSpec @@ -11,6 +12,7 @@ import net.superricky.tpaplusplus.utility.Dispatcher object CommandRegister { fun registerCommands(dispatcher: Dispatcher) { val rootNode = RootCommand.build() + rootNode.addChild(ReloadCommand.build()) logger.info("Register command /${GlobalConst.MOD_ID}...") dispatcher.root.addChild(rootNode) diff --git a/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/ReloadCommand.kt b/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/ReloadCommand.kt new file mode 100644 index 0000000..097c0f8 --- /dev/null +++ b/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/ReloadCommand.kt @@ -0,0 +1,40 @@ +package net.superricky.tpaplusplus.command.subcommands + +import net.minecraft.server.command.CommandManager.literal +import net.superricky.tpaplusplus.GlobalConst.PERMISSION_LEVEL +import net.superricky.tpaplusplus.command.BuildableCommand +import net.superricky.tpaplusplus.command.CommandResult +import net.superricky.tpaplusplus.config.config.CommonSpec +import net.superricky.tpaplusplus.config.config.Config.get +import net.superricky.tpaplusplus.config.language.LanguageConfig +import net.superricky.tpaplusplus.config.language.LanguageConfig.getMutableText +import net.superricky.tpaplusplus.config.language.SystemSpec +import net.superricky.tpaplusplus.utility.Context +import net.superricky.tpaplusplus.utility.LiteralNode +import net.superricky.tpaplusplus.utility.TextColorPallet + +object ReloadCommand : BuildableCommand { + override fun build(): LiteralNode = + literal("reload") + .requires { + it.hasPermissionLevel(PERMISSION_LEVEL) + } + .then( + literal("message") + .executes { reloadMessage(it) } + ) + .executes { + reloadMessage(it) + } + .build() + + private fun reloadMessage(context: Context): Int { + val source = context.source + val sender = source.player + sender ?: return CommandResult.SENDER_NOT_EXIST.status + sender.sendMessage(SystemSpec.reloadStart.getMutableText().setStyle(TextColorPallet.primary)) + LanguageConfig.loadLangFile(CommonSpec.language.get().lowercase()) + sender.sendMessage(SystemSpec.reloadFinish.getMutableText().setStyle(TextColorPallet.primary)) + return CommandResult.NORMAL.status + } +} diff --git a/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/RootCommand.kt b/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/RootCommand.kt index 8061602..7b5a27c 100644 --- a/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/RootCommand.kt +++ b/src/main/kotlin/net/superricky/tpaplusplus/command/subcommands/RootCommand.kt @@ -15,6 +15,9 @@ import net.superricky.tpaplusplus.utility.literal object RootCommand : BuildableCommand { override fun build(): LiteralNode = literal(GlobalConst.MOD_ID) + .then( + literal("version").executes { showMetaData(it) } + ) .executes { showMetaData(it) } .build() diff --git a/src/main/kotlin/net/superricky/tpaplusplus/config/language/LanguageConfig.kt b/src/main/kotlin/net/superricky/tpaplusplus/config/language/LanguageConfig.kt index 976d762..908a94f 100644 --- a/src/main/kotlin/net/superricky/tpaplusplus/config/language/LanguageConfig.kt +++ b/src/main/kotlin/net/superricky/tpaplusplus/config/language/LanguageConfig.kt @@ -47,6 +47,7 @@ object LanguageConfig { FabricLoader.getInstance().configDir.resolve(LANG_FOLDER_PATH).resolve(languageFileName).toFile() ) .from.env() + config.validateRequired() } private fun checkLanguageFile(language: String): Boolean { diff --git a/src/main/kotlin/net/superricky/tpaplusplus/config/language/SystemSpec.kt b/src/main/kotlin/net/superricky/tpaplusplus/config/language/SystemSpec.kt index 24a2ec4..24077f1 100644 --- a/src/main/kotlin/net/superricky/tpaplusplus/config/language/SystemSpec.kt +++ b/src/main/kotlin/net/superricky/tpaplusplus/config/language/SystemSpec.kt @@ -10,4 +10,6 @@ object SystemSpec : ConfigSpec("system") { val modrinthView by required() val courseforgeBase by required() val courseforgeView by required() + val reloadStart by required() + val reloadFinish by required() } diff --git a/src/main/resources/lang/en_us.toml b/src/main/resources/lang/en_us.toml index 95459a3..9c6aaf0 100644 --- a/src/main/resources/lang/en_us.toml +++ b/src/main/resources/lang/en_us.toml @@ -7,6 +7,8 @@ modrinthBase = "Modrinth: %s" modrinthView = "View on Modrinth" courseforgeBase = "CourseForge: %s" courseforgeView = "View on CourseForge" +reloadStart = "Reloading language file..." +reloadFinish = "Language file reload complete" [error] # A message sent when the sender player is not exist ( Only happened when command block run command ) diff --git a/src/main/resources/lang/example.toml b/src/main/resources/lang/example.toml index 95459a3..9c6aaf0 100644 --- a/src/main/resources/lang/example.toml +++ b/src/main/resources/lang/example.toml @@ -7,6 +7,8 @@ modrinthBase = "Modrinth: %s" modrinthView = "View on Modrinth" courseforgeBase = "CourseForge: %s" courseforgeView = "View on CourseForge" +reloadStart = "Reloading language file..." +reloadFinish = "Language file reload complete" [error] # A message sent when the sender player is not exist ( Only happened when command block run command ) diff --git a/src/main/resources/lang/zh_cn.toml b/src/main/resources/lang/zh_cn.toml index b702a56..3feb173 100644 --- a/src/main/resources/lang/zh_cn.toml +++ b/src/main/resources/lang/zh_cn.toml @@ -7,6 +7,8 @@ modrinthBase = "Modrinth: %s" modrinthView = "在 Modrinth 上查看" courseforgeBase = "CourseForge: %s" courseforgeView = "在 CourseForge 上查看" +reloadStart = "正在重载语言文件" +reloadFinish = "语言文件重载完成" [error] # A message sent when the sender player is not exist ( Only happened when command block run command ) diff --git a/src/main/resources/lang/zh_tw.toml b/src/main/resources/lang/zh_tw.toml index 7645fe8..a7cbe35 100644 --- a/src/main/resources/lang/zh_tw.toml +++ b/src/main/resources/lang/zh_tw.toml @@ -7,6 +7,8 @@ modrinthBase = "Modrinth: %s" modrinthView = "在 Modrinth 上查看" courseforgeBase = "CourseForge: %s" courseforgeView = "在 CourseForge 上查看" +reloadStart = "正在重載語言檔" +reloadFinish = "語言檔重載完成" [error] # A message sent when the sender player is not exist ( Only happened when command block run command )