Adding actual commands

This commit is contained in:
DaMachinator 2018-02-08 12:20:16 -05:00 committed by Arcanitor
parent e953674253
commit 6961d34f17
2 changed files with 38 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import matterlink.MatterLink
import matterlink.cfg import matterlink.cfg
import java.util.regex.Pattern import java.util.regex.Pattern
class BridgeCommand(val name: String, command: (String) -> Boolean) { class BridgeCommand(val name: String, command: (String) -> Boolean, val help: String) {
val execute: (String) -> Boolean = command //return true for success and false for failure val execute: (String) -> Boolean = command //return true for success and false for failure
companion object Handler { companion object Handler {
@ -13,8 +13,7 @@ class BridgeCommand(val name: String, command: (String) -> Boolean) {
fun handleCommand(input: String): Boolean { fun handleCommand(input: String): Boolean {
if (input[0] != cfg!!.command.prefix[0] || input.length < 2) return false if (input[0] != cfg!!.command.prefix[0] || input.length < 2) return false
//if you can get it to accept just a char instead of a stupid Pattern that would be great val cmd = input.substring(1).split(delimiters = ' ', ignoreCase = false, limit = 2)
val cmd = input.substring(1).split(Pattern.compile(" "), 2)
val args = if (cmd.size > 1) cmd[1] else "" val args = if (cmd.size > 1) cmd[1] else ""
return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false
@ -31,5 +30,17 @@ class BridgeCommand(val name: String, command: (String) -> Boolean) {
if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name) 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(" | ")
}
} }
} }

View File

@ -1,5 +1,6 @@
package matterlink.bridge.command package matterlink.bridge.command
import matterlink.antiping
import matterlink.bridge.ApiMessage import matterlink.bridge.ApiMessage
import matterlink.bridge.MessageHandler import matterlink.bridge.MessageHandler
import matterlink.cfg import matterlink.cfg
@ -14,7 +15,7 @@ object Commands {
var output: String = "" var output: String = ""
for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) { for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) {
output = output + player + " " output = output + player.antiping() + " "
} }
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
@ -23,10 +24,30 @@ object Commands {
)) ))
return true 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 <command>"
) )
fun register() { fun register() {
BridgeCommand.registerAll(listPlayers) BridgeCommand.registerAll(listPlayers, help)
} }
} }