diff --git a/src/main/kotlin/matterlink/MatterLink.kt b/src/main/kotlin/matterlink/MatterLink.kt index e96807a..0681f4f 100644 --- a/src/main/kotlin/matterlink/MatterLink.kt +++ b/src/main/kotlin/matterlink/MatterLink.kt @@ -2,7 +2,9 @@ package matterlink import matterlink.bridge.MessageHandler import matterlink.bridge.ServerChatHelper -import matterlink.bridge.command.Commands +import matterlink.bridge.command.BridgeCommandRegistry +import matterlink.bridge.command.HelpCommand +import matterlink.bridge.command.PlayerListCommand import matterlink.command.CommandMatterlink import matterlink.eventhandlers.* import net.minecraftforge.common.MinecraftForge @@ -40,8 +42,6 @@ object MatterLink { PropertiesUtil(Properties()), System.out) - val bridgeCommands = Commands - @Mod.EventHandler fun preInit(event: FMLPreInitializationEvent) { logger = event.modLog @@ -49,7 +49,7 @@ object MatterLink { MatterLinkConfig(event.suggestedConfigurationFile) - bridgeCommands.register() + BridgeCommandRegistry.registerAll(PlayerListCommand, HelpCommand) } @Mod.EventHandler diff --git a/src/main/kotlin/matterlink/MatterLinkConfig.kt b/src/main/kotlin/matterlink/MatterLinkConfig.kt index ae8243d..a8c07e0 100644 --- a/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ b/src/main/kotlin/matterlink/MatterLinkConfig.kt @@ -74,7 +74,7 @@ class MatterLinkConfig(file: File) { ) ) - config.addCustomCategoryComment(CATEGORY_COMMAND,"User commands") + config.addCustomCategoryComment(CATEGORY_COMMAND, "User commands") command = CommandOptions( enable = config.getBoolean( "enable", diff --git a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt index 342c2d7..7c215ed 100644 --- a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt +++ b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt @@ -2,6 +2,7 @@ package matterlink.bridge import matterlink.MatterLink import matterlink.bridge.command.BridgeCommand +import matterlink.bridge.command.BridgeCommandRegistry import matterlink.cfg import net.minecraft.util.text.TextComponentString import net.minecraftforge.fml.common.FMLCommonHandler @@ -21,7 +22,7 @@ class ServerChatHelper { val message = when (nextMessage.event) { "user_action" -> nextMessage.format(cfg!!.formatting.action) "" -> { - if (BridgeCommand.handleCommand(nextMessage.text)) return + if (BridgeCommandRegistry.handleCommand(nextMessage.text)) return nextMessage.format(cfg!!.formatting.chat) } "join_leave" -> nextMessage.format(cfg!!.formatting.joinLeave) diff --git a/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt b/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt index 013ff55..2e9cabe 100644 --- a/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt +++ b/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt @@ -1,47 +1,7 @@ package matterlink.bridge.command -import matterlink.MatterLink -import matterlink.cfg -import java.util.regex.Pattern - -class BridgeCommand(val name: String, command: (String) -> Boolean, val help: String) { - val execute: (String) -> Boolean = command //return true for success and false for failure - - companion object Handler { - private val commandMap: HashMap = HashMap() - - fun handleCommand(input: String): Boolean { - if (!cfg!!.command.enable) return false - if (input[0] != cfg!!.command.prefix[0] || input.length < 2) return false - - val cmd = input.substring(1).split(delimiters = ' ', ignoreCase = false, limit = 2) - val args = if (cmd.size > 1) cmd[1] else "" - - return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false - } - - fun register(cmd: BridgeCommand): Boolean { - if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false - commandMap[cmd.name] = cmd - return true - } - - fun registerAll(vararg cmds: BridgeCommand) { - for (cmd in cmds) { - if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name) - } - } - - fun getHelpString(cmd: String): String { - if (!commandMap.containsKey(cmd)) return "No such command." - - val help = commandMap[cmd]!!.help - - return if (help.isNotBlank()) help else "No help for command " + cfg!!.command.prefix + cmd - } - - fun listCommands(): String { - return commandMap.keys.joinToString(" | ") - } - } +interface BridgeCommand { + val name: String + fun command(args: String): Boolean + val help: String } \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt b/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt new file mode 100644 index 0000000..6e9f57e --- /dev/null +++ b/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt @@ -0,0 +1,43 @@ +package matterlink.bridge.command + +import matterlink.MatterLink +import matterlink.cfg + +object BridgeCommandRegistry { + + private val commandMap: HashMap = HashMap() + + fun handleCommand(input: String): Boolean { + if (!cfg!!.command.enable) return false + if (input[0] != cfg!!.command.prefix[0] || input.length < 2) return false + + val cmd = input.substring(1).split(delimiters = ' ', ignoreCase = false, limit = 2) + val args = if (cmd.size > 1) cmd[1] else "" + + return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.command(args)) else false + } + + fun register(cmd: BridgeCommand): Boolean { + if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false + commandMap[cmd.name] = cmd + return true + } + + fun registerAll(vararg cmds: BridgeCommand) { + for (cmd in cmds) { + if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name) + } + } + + fun getHelpString(cmd: String): String { + if (!commandMap.containsKey(cmd)) return "No such command." + + val help = commandMap[cmd]!!.help + + return if (help.isNotBlank()) help else "No help for command " + cfg!!.command.prefix + cmd + } + + fun listCommands(): String { + return commandMap.keys.joinToString(" | ") + } +} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/Commands.kt b/src/main/kotlin/matterlink/bridge/command/Commands.kt deleted file mode 100644 index 16a19e0..0000000 --- a/src/main/kotlin/matterlink/bridge/command/Commands.kt +++ /dev/null @@ -1,53 +0,0 @@ -package matterlink.bridge.command - -import matterlink.antiping -import matterlink.bridge.ApiMessage -import matterlink.bridge.MessageHandler -import matterlink.cfg -import net.minecraftforge.fml.common.FMLCommonHandler - -object Commands { - val listPlayers = BridgeCommand( - "players", - fun(args: String): Boolean { - if (args.isNotEmpty()) return false - - var output: String = "" - - for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) { - output = output + player.antiping() + " " - } - - MessageHandler.transmit(ApiMessage( - username = cfg!!.relay.systemUser, - text = output - )) - - return true - }, - "Lists online players." - ) - - val help = BridgeCommand( - "help", - fun(args: String): Boolean { - var msg: String - if (args.isEmpty()) { - msg = "Available commands: " + BridgeCommand.listCommands() - } else { - val cmd = args.split(delimiters = *charArrayOf(' '), ignoreCase = false, limit = 2)[0] - msg = cmd + ": " + BridgeCommand.getHelpString(cmd) - } - MessageHandler.transmit(ApiMessage( - username = cfg!!.relay.systemUser, - text = msg - )) - return true - }, - "Returns the help string for the given command. Syntax: help " - ) - - fun register() { - BridgeCommand.registerAll(listPlayers, help) - } -} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt b/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt new file mode 100644 index 0000000..938c356 --- /dev/null +++ b/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt @@ -0,0 +1,24 @@ +package matterlink.bridge.command + +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler +import matterlink.cfg + +object HelpCommand : BridgeCommand { + override val name: String = "help" + override val help: String = "Returns the help string for the given command. Syntax: help " + override fun command(args: String): Boolean { + val msg: String = if (args.isEmpty()) { + "Available commands: " + BridgeCommandRegistry.listCommands() + } else { + val cmd = args.split(delimiters = *charArrayOf(' '), ignoreCase = false, limit = 2)[0] + cmd + ": " + BridgeCommandRegistry.getHelpString(cmd) + } + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = msg + )) + return true + } + +} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/PlayerListCommand.kt b/src/main/kotlin/matterlink/bridge/command/PlayerListCommand.kt new file mode 100644 index 0000000..a0e3de7 --- /dev/null +++ b/src/main/kotlin/matterlink/bridge/command/PlayerListCommand.kt @@ -0,0 +1,29 @@ +package matterlink.bridge.command + +import matterlink.antiping +import matterlink.bridge.ApiMessage +import matterlink.bridge.MessageHandler +import matterlink.cfg +import net.minecraftforge.fml.common.FMLCommonHandler + +object PlayerListCommand : BridgeCommand { + override val name: String = "players" + override val help: String = "Lists online players." + override fun command(args: String): Boolean { + if (args.isNotBlank()) return false + + var output = "" + + for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) { + output = output + player.antiping() + " " + } + + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = output + )) + + return true + } + +} \ No newline at end of file