Skip to content

Commit

Permalink
feat(command): Add ReloadCommand.kt to reload language config
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Oct 11, 2024
1 parent b60c3e8 commit c323891
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ object SystemSpec : ConfigSpec("system") {
val modrinthView by required<String>()
val courseforgeBase by required<String>()
val courseforgeView by required<String>()
val reloadStart by required<String>()
val reloadFinish by required<String>()
}
2 changes: 2 additions & 0 deletions src/main/resources/lang/en_us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/lang/example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/lang/zh_cn.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/lang/zh_tw.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit c323891

Please sign in to comment.