refactor Commands

implements #1
This commit is contained in:
NikkyAI 2018-02-08 19:50:50 +01:00 committed by Unknown
parent f68439cd13
commit 7916ef76f4
8 changed files with 107 additions and 103 deletions

View File

@ -2,7 +2,9 @@ package matterlink
import matterlink.bridge.MessageHandler import matterlink.bridge.MessageHandler
import matterlink.bridge.ServerChatHelper 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.command.CommandMatterlink
import matterlink.eventhandlers.* import matterlink.eventhandlers.*
import net.minecraftforge.common.MinecraftForge import net.minecraftforge.common.MinecraftForge
@ -40,8 +42,6 @@ object MatterLink {
PropertiesUtil(Properties()), PropertiesUtil(Properties()),
System.out) System.out)
val bridgeCommands = Commands
@Mod.EventHandler @Mod.EventHandler
fun preInit(event: FMLPreInitializationEvent) { fun preInit(event: FMLPreInitializationEvent) {
logger = event.modLog logger = event.modLog
@ -49,7 +49,7 @@ object MatterLink {
MatterLinkConfig(event.suggestedConfigurationFile) MatterLinkConfig(event.suggestedConfigurationFile)
bridgeCommands.register() BridgeCommandRegistry.registerAll(PlayerListCommand, HelpCommand)
} }
@Mod.EventHandler @Mod.EventHandler

View File

@ -74,7 +74,7 @@ class MatterLinkConfig(file: File) {
) )
) )
config.addCustomCategoryComment(CATEGORY_COMMAND,"User commands") config.addCustomCategoryComment(CATEGORY_COMMAND, "User commands")
command = CommandOptions( command = CommandOptions(
enable = config.getBoolean( enable = config.getBoolean(
"enable", "enable",

View File

@ -2,6 +2,7 @@ package matterlink.bridge
import matterlink.MatterLink import matterlink.MatterLink
import matterlink.bridge.command.BridgeCommand import matterlink.bridge.command.BridgeCommand
import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.cfg import matterlink.cfg
import net.minecraft.util.text.TextComponentString import net.minecraft.util.text.TextComponentString
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
@ -21,7 +22,7 @@ class ServerChatHelper {
val message = when (nextMessage.event) { val message = when (nextMessage.event) {
"user_action" -> nextMessage.format(cfg!!.formatting.action) "user_action" -> nextMessage.format(cfg!!.formatting.action)
"" -> { "" -> {
if (BridgeCommand.handleCommand(nextMessage.text)) return if (BridgeCommandRegistry.handleCommand(nextMessage.text)) return
nextMessage.format(cfg!!.formatting.chat) nextMessage.format(cfg!!.formatting.chat)
} }
"join_leave" -> nextMessage.format(cfg!!.formatting.joinLeave) "join_leave" -> nextMessage.format(cfg!!.formatting.joinLeave)

View File

@ -1,47 +1,7 @@
package matterlink.bridge.command package matterlink.bridge.command
import matterlink.MatterLink interface BridgeCommand {
import matterlink.cfg val name: String
import java.util.regex.Pattern fun command(args: String): Boolean
val help: String
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<String, BridgeCommand> = 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(" | ")
}
}
} }

View File

@ -0,0 +1,43 @@
package matterlink.bridge.command
import matterlink.MatterLink
import matterlink.cfg
object BridgeCommandRegistry {
private val commandMap: HashMap<String, BridgeCommand> = 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(" | ")
}
}

View File

@ -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 <command>"
)
fun register() {
BridgeCommand.registerAll(listPlayers, help)
}
}

View File

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

View File

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