version bump 1.1.0

refactor of commands.. again
This commit is contained in:
NikkyAI 2018-02-08 21:44:59 +01:00 committed by Unknown
parent 7916ef76f4
commit 42b5ba9186
7 changed files with 31 additions and 32 deletions

View File

@ -17,7 +17,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. //Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
version = "1.0.0" version = "1.1.0"
group = "matterlink" // http://maven.apache.org/guides/mini/guide-naming-conventions.html group = "matterlink" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "MatterLink" archivesBaseName = "MatterLink"

View File

@ -48,7 +48,10 @@ object MatterLink {
logger.info("Building bridge!") logger.info("Building bridge!")
MatterLinkConfig(event.suggestedConfigurationFile) MatterLinkConfig(event.suggestedConfigurationFile)
}
@Mod.EventHandler
fun init(event: FMLInitializationEvent) {
BridgeCommandRegistry.registerAll(PlayerListCommand, HelpCommand) BridgeCommandRegistry.registerAll(PlayerListCommand, HelpCommand)
} }

View File

@ -1,7 +1,6 @@
package matterlink.bridge package matterlink.bridge
import matterlink.MatterLink import matterlink.MatterLink
import matterlink.bridge.command.BridgeCommand
import matterlink.bridge.command.BridgeCommandRegistry import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.cfg import matterlink.cfg
import net.minecraft.util.text.TextComponentString import net.minecraft.util.text.TextComponentString

View File

@ -5,28 +5,32 @@ import matterlink.cfg
object BridgeCommandRegistry { object BridgeCommandRegistry {
private val commandMap: HashMap<String, BridgeCommand> = HashMap() private val commandMap: HashMap<String, IBridgeCommand> = HashMap()
fun handleCommand(input: String): Boolean { fun handleCommand(input: String): Boolean {
if (!cfg!!.command.enable) return false if (!cfg!!.command.enable) return false
if (input[0] != cfg!!.command.prefix[0] || input.length < 2) 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 cmd = input.substring(1).split(' ', ignoreCase = false, limit = 2)
val args = if (cmd.size > 1) cmd[1] else "" val args = if (cmd.size == 2)
cmd[1]
else
""
return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.command(args)) else false return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.call(args)) else false
} }
fun register(cmd: BridgeCommand): Boolean { fun register(cmd: IBridgeCommand): Boolean {
if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) {
MatterLink.logger.error("Failed to register command: '${cmd.name}'")
return false
}
commandMap[cmd.name] = cmd commandMap[cmd.name] = cmd
return true return true
} }
fun registerAll(vararg cmds: BridgeCommand) { fun registerAll(vararg commands: IBridgeCommand) {
for (cmd in cmds) { commands.forEach { register(it) }
if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name)
}
} }
fun getHelpString(cmd: String): String { fun getHelpString(cmd: String): String {
@ -34,10 +38,9 @@ object BridgeCommandRegistry {
val help = commandMap[cmd]!!.help val help = commandMap[cmd]!!.help
return if (help.isNotBlank()) help else "No help for command " + cfg!!.command.prefix + cmd return if (help.isNotBlank()) help else "No help for '$cmd'"
} }
fun listCommands(): String { val commandList: String
return commandMap.keys.joinToString(" | ") get() = commandMap.keys.joinToString(separator = ", ")
}
} }

View File

@ -4,15 +4,15 @@ import matterlink.bridge.ApiMessage
import matterlink.bridge.MessageHandler import matterlink.bridge.MessageHandler
import matterlink.cfg import matterlink.cfg
object HelpCommand : BridgeCommand { object HelpCommand : IBridgeCommand {
override val name: String = "help" override val name: String = "help"
override val help: String = "Returns the help string for the given command. Syntax: help <command>" override val help: String = "Returns the help string for the given command. Syntax: help <command>"
override fun command(args: String): Boolean { override fun call(args: String): Boolean {
val msg: String = if (args.isEmpty()) { val msg: String = if (args.isEmpty()) {
"Available commands: " + BridgeCommandRegistry.listCommands() "Available commands: ${ BridgeCommandRegistry.commandList}"
} else { } else {
val cmd = args.split(delimiters = *charArrayOf(' '), ignoreCase = false, limit = 2)[0] args.split(" ", ignoreCase = false)
cmd + ": " + BridgeCommandRegistry.getHelpString(cmd) .joinToString(separator = "\n") { "$it: ${ BridgeCommandRegistry.getHelpString(it) }" }
} }
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser, username = cfg!!.relay.systemUser,

View File

@ -1,7 +1,7 @@
package matterlink.bridge.command package matterlink.bridge.command
interface BridgeCommand { interface IBridgeCommand {
val name: String val name: String
fun command(args: String): Boolean fun call(args: String): Boolean
val help: String val help: String
} }

View File

@ -6,21 +6,15 @@ import matterlink.bridge.MessageHandler
import matterlink.cfg import matterlink.cfg
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
object PlayerListCommand : BridgeCommand { object PlayerListCommand : IBridgeCommand {
override val name: String = "players" override val name: String = "players"
override val help: String = "Lists online players." override val help: String = "Lists online players."
override fun command(args: String): Boolean { override fun call(args: String): Boolean {
if (args.isNotBlank()) return false if (args.isNotBlank()) return false
var output = ""
for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) {
output = output + player.antiping() + " "
}
MessageHandler.transmit(ApiMessage( MessageHandler.transmit(ApiMessage(
username = cfg!!.relay.systemUser, username = cfg!!.relay.systemUser,
text = output text = FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames.joinToString(" ") { it.antiping() }
)) ))
return true return true