diff --git a/src/main/kotlin/matterlink/MatterLink.kt b/src/main/kotlin/matterlink/MatterLink.kt index 33bc0bc..e96807a 100644 --- a/src/main/kotlin/matterlink/MatterLink.kt +++ b/src/main/kotlin/matterlink/MatterLink.kt @@ -2,6 +2,7 @@ package matterlink import matterlink.bridge.MessageHandler import matterlink.bridge.ServerChatHelper +import matterlink.bridge.command.Commands import matterlink.command.CommandMatterlink import matterlink.eventhandlers.* import net.minecraftforge.common.MinecraftForge @@ -39,17 +40,21 @@ object MatterLink { PropertiesUtil(Properties()), System.out) + val bridgeCommands = Commands + @Mod.EventHandler fun preInit(event: FMLPreInitializationEvent) { logger = event.modLog logger.info("Building bridge!") MatterLinkConfig(event.suggestedConfigurationFile) + + bridgeCommands.register() } @Mod.EventHandler fun serverStarting(event: FMLServerStartingEvent) { - logger.debug("Registering bridge commands") + logger.debug("Registering server commands") event.registerServerCommand(CommandMatterlink()) MessageHandler.start() diff --git a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt index 1e18981..b72e425 100644 --- a/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt +++ b/src/main/kotlin/matterlink/bridge/ServerChatHelper.kt @@ -1,6 +1,7 @@ package matterlink.bridge import matterlink.MatterLink +import matterlink.bridge.command.BridgeCommand import matterlink.cfg import net.minecraft.util.text.TextComponentString import net.minecraftforge.fml.common.FMLCommonHandler diff --git a/src/main/kotlin/matterlink/bridge/BridgeCommand.kt b/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt similarity index 51% rename from src/main/kotlin/matterlink/bridge/BridgeCommand.kt rename to src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt index c5157a2..c99397e 100644 --- a/src/main/kotlin/matterlink/bridge/BridgeCommand.kt +++ b/src/main/kotlin/matterlink/bridge/command/BridgeCommand.kt @@ -1,16 +1,12 @@ -package matterlink.bridge +package matterlink.bridge.command +import matterlink.MatterLink import matterlink.cfg -import net.minecraftforge.fml.common.FMLCommonHandler import java.util.regex.Pattern class BridgeCommand(val name: String, command: (String) -> Boolean) { val execute: (String) -> Boolean = command //return true for success and false for failure - init { - registerCommand(this) - } - companion object Handler { private val commandMap: HashMap = HashMap() @@ -24,34 +20,16 @@ class BridgeCommand(val name: String, command: (String) -> Boolean) { return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false } - fun registerCommand(cmd: BridgeCommand): Boolean { + fun register(cmd: BridgeCommand): Boolean { if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false commandMap[cmd.name] = cmd return true } - } - - private val commands = object { - private val playerlist = 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 + " " - } - - MessageHandler.transmit(ApiMessage( - username = cfg!!.relay.systemUser, - text = output - )) - - return true - } - ) - + fun registerAll(vararg cmds: BridgeCommand) { + for (cmd in cmds) { + if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name) + } + } } } \ No newline at end of file diff --git a/src/main/kotlin/matterlink/bridge/command/Commands.kt b/src/main/kotlin/matterlink/bridge/command/Commands.kt new file mode 100644 index 0000000..1ec2cc7 --- /dev/null +++ b/src/main/kotlin/matterlink/bridge/command/Commands.kt @@ -0,0 +1,32 @@ +package matterlink.bridge.command + +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 + " " + } + + MessageHandler.transmit(ApiMessage( + username = cfg!!.relay.systemUser, + text = output + )) + + return true + } + ) + + fun register() { + BridgeCommand.registerAll(listPlayers) + } +} \ No newline at end of file