Finish command framework

This commit is contained in:
DaMachinator 2018-02-08 10:29:00 -05:00 committed by Arcanitor
parent cbe720ec0f
commit eab9376adc
5 changed files with 33 additions and 20 deletions

View File

@ -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/]$")
)
)

View File

@ -0,0 +1,26 @@
package matterlink.bridge
import matterlink.cfg
class BridgeCommand(val name: String, command: (List<String>) -> Boolean) {
val execute: (List<String>) -> Boolean = command //return true for success and false for failure
companion object Handler {
private val commandMap: HashMap<String, BridgeCommand> = 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
}
}
}

View File

@ -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

View File

@ -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
}
}

View File

@ -1,2 +0,0 @@
package matterlink.bridge.command