diff --git a/src/main/kotlin/matterlink/MatterLinkConfig.kt b/src/main/kotlin/matterlink/MatterLinkConfig.kt index 668ca26..cb0e95d 100644 --- a/src/main/kotlin/matterlink/MatterLinkConfig.kt +++ b/src/main/kotlin/matterlink/MatterLinkConfig.kt @@ -2,6 +2,7 @@ package matterlink import net.minecraftforge.common.config.Configuration import java.io.File +import java.util.regex.Pattern var cfg: MatterLinkConfig? = null @@ -78,8 +79,8 @@ class MatterLinkConfig(file: File) { "commandPrefix", CATEGORY_COMMAND, "!", - "Prefix for MC bridge commands. Accepts a single non-alphanumeric character." - //Pattern.compile("[^0-9A-Za-z/]") + "Prefix for MC bridge commands. Accepts a single character (not alphanumeric or /)", + Pattern.compile("^[^0-9A-Za-z/]$") ) ) diff --git a/src/main/kotlin/matterlink/bridge/BridgeCommand.kt b/src/main/kotlin/matterlink/bridge/BridgeCommand.kt new file mode 100644 index 0000000..db300e8 --- /dev/null +++ b/src/main/kotlin/matterlink/bridge/BridgeCommand.kt @@ -0,0 +1,26 @@ +package matterlink.bridge + +import matterlink.cfg + +class BridgeCommand(val name: String, command: (List) -> Boolean) { + val execute: (List) -> Boolean = command //return true for success and false for failure + + companion object Handler { + private val commandMap: HashMap = HashMap() + + fun handleCommand(input: String): Boolean { + if (input[0] != cfg!!.command.prefix[0]) return false + + val cmd = ArrayList(input.substring(1).split(" ")) + val args = cmd.subList(1, cmd.size - 1) + + return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false + } + + fun registerCommand(cmd: BridgeCommand): Boolean { + if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false + commandMap[cmd.name] = cmd + return true + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt index 2f56943..1e18981 100644 --- a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt +++ b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt @@ -19,7 +19,10 @@ class ServerChatHelper { val section = '\u00A7' val message = when (nextMessage.event) { "user_action" -> nextMessage.format(cfg!!.formatting.action) - "" -> nextMessage.format(cfg!!.formatting.chat) + "" -> { + BridgeCommand.handleCommand(nextMessage.text) + nextMessage.format(cfg!!.formatting.chat) + } "join_leave" -> nextMessage.format(cfg!!.formatting.joinLeave) else -> { val user = nextMessage.username diff --git a/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt b/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt deleted file mode 100644 index 646104e..0000000 --- a/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt +++ /dev/null @@ -1,15 +0,0 @@ -package matterlink.bridge.command - -class BridgeCommand(val name: String, command: (String) -> Boolean) { - private val execute: (String) -> Boolean = command //return true for success and false for failure - - fun tryExecute(input: String): Boolean { - //get the first word - val space = input.indexOf(' ') - if (space == 0) return false //"! " is never a command - - var cmd = if (space > 0) input.substring(0, space) else input - - return if (cmd == name) execute(input.substring(space + 1)) else false - } -} \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/BridgeCommandHandler.kt b/src/main/kotlin/matterlink/bridge/command/BridgeCommandHandler.kt deleted file mode 100644 index 2d8d34a..0000000 --- a/src/main/kotlin/matterlink/bridge/command/BridgeCommandHandler.kt +++ /dev/null @@ -1,2 +0,0 @@ -package matterlink.bridge.command -