From 4627a60c9d9a10d499683a8244133ee0d9080f28 Mon Sep 17 00:00:00 2001 From: nikky Date: Tue, 10 Jul 2018 05:54:51 +0200 Subject: [PATCH] execute bridge commands ingame --- .../main/kotlin/matterlink/EventHandler.kt | 5 +- .../src/main/kotlin/matterlink/MatterLink.kt | 8 +-- .../command/MatterLinkCommandSender.kt | 8 +-- .../main/kotlin/matterlink/EventHandler.kt | 5 +- .../src/main/kotlin/matterlink/MatterLink.kt | 8 +-- .../command/MatterLinkCommandSender.kt | 8 +-- .../main/kotlin/matterlink/EventHandler.kt | 6 +- .../src/main/kotlin/matterlink/MatterLink.kt | 9 +-- .../command/MatterLinkCommandSender.kt | 8 +-- .../main/kotlin/matterlink/EventHandler.kt | 5 +- .../src/main/kotlin/matterlink/MatterLink.kt | 8 +-- .../command/MatterLinkCommandSender.kt | 8 +-- .../src/main/kotlin/matterlink/IMatterLink.kt | 3 +- core/src/main/kotlin/matterlink/PasteUtil.kt | 2 +- core/src/main/kotlin/matterlink/Util.kt | 4 +- .../bridge/command/AuthBridgeCommand.kt | 39 ++++++------- .../bridge/command/BridgeCommandRegistry.kt | 37 ++++++++++-- .../bridge/command/CustomCommand.kt | 56 +++++++++++-------- .../matterlink/bridge/command/HelpCommand.kt | 10 ++-- .../bridge/command/IBridgeCommand.kt | 43 +++++++++++++- .../bridge/command/IMinecraftCommandSender.kt | 25 ++++----- .../command/RequestPermissionsCommand.kt | 29 ++++------ .../kotlin/matterlink/config/BaseConfig.kt | 10 ++-- .../kotlin/matterlink/config/CommandConfig.kt | 6 +- .../matterlink/handlers/ChatProcessor.kt | 8 ++- 25 files changed, 203 insertions(+), 155 deletions(-) diff --git a/1.10.2/src/main/kotlin/matterlink/EventHandler.kt b/1.10.2/src/main/kotlin/matterlink/EventHandler.kt index 07e0f6c..a962298 100644 --- a/1.10.2/src/main/kotlin/matterlink/EventHandler.kt +++ b/1.10.2/src/main/kotlin/matterlink/EventHandler.kt @@ -44,10 +44,11 @@ object EventHandler { @JvmStatic fun chatEvent(e: ServerChatEvent) { if(e.isCanceled) return - ChatProcessor.sendToBridge( + e.isCanceled = ChatProcessor.sendToBridge( user = e.player.displayName.unformattedText, msg = e.message, - event = "" + event = "", + uuid = e.player.gameProfile.id.toString() ) } diff --git a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt index d5ddc39..bc6db96 100644 --- a/1.10.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.10.2/src/main/kotlin/matterlink/MatterLink.kt @@ -1,6 +1,7 @@ package matterlink import com.mojang.authlib.GameProfile +import matterlink.bridge.command.IBridgeCommand import matterlink.command.MatterLinkCommand import matterlink.command.MatterLinkCommandSender import matterlink.config.BaseConfig @@ -107,12 +108,9 @@ object MatterLink : IMatterLink() { override fun commandSenderFor( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, + env: IBridgeCommand.CommandEnvironment, op: Boolean - ) = MatterLinkCommandSender(user, userId, server, uuid, username, op) + ) = MatterLinkCommandSender(user, env, op) override val mcVersion: String = MCVERSION override val modVersion: String = MODVERSION diff --git a/1.10.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt b/1.10.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt index 5f6f513..e69fa78 100644 --- a/1.10.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt +++ b/1.10.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt @@ -1,5 +1,6 @@ package matterlink.command +import matterlink.bridge.command.IBridgeCommand import matterlink.bridge.command.IMinecraftCommandSender import net.minecraft.command.CommandResultStats import net.minecraft.command.ICommandSender @@ -15,11 +16,8 @@ import javax.annotation.Nonnull class MatterLinkCommandSender( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, - op: Boolean) : IMinecraftCommandSender(user, userId, server, uuid, username, op), ICommandSender { + env: IBridgeCommand.CommandEnvironment, + op: Boolean) : IMinecraftCommandSender(user, env, op), ICommandSender { override fun execute(cmdString: String): Boolean { return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( diff --git a/1.11.2/src/main/kotlin/matterlink/EventHandler.kt b/1.11.2/src/main/kotlin/matterlink/EventHandler.kt index 87d40ca..8a8a98a 100644 --- a/1.11.2/src/main/kotlin/matterlink/EventHandler.kt +++ b/1.11.2/src/main/kotlin/matterlink/EventHandler.kt @@ -45,10 +45,11 @@ object EventHandler { @JvmStatic fun chatEvent(e: ServerChatEvent) { if(e.isCanceled) return - ChatProcessor.sendToBridge( + e.isCanceled = ChatProcessor.sendToBridge( user = e.player.displayName.unformattedText, msg = e.message, - event = "" + event = "", + uuid = e.player.gameProfile.id.toString() ) } diff --git a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt index 22b4740..c1b3ea6 100644 --- a/1.11.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.11.2/src/main/kotlin/matterlink/MatterLink.kt @@ -1,6 +1,7 @@ package matterlink import com.mojang.authlib.GameProfile +import matterlink.bridge.command.IBridgeCommand import matterlink.command.MatterLinkCommand import matterlink.command.MatterLinkCommandSender import matterlink.config.BaseConfig @@ -106,12 +107,9 @@ object MatterLink : IMatterLink() { override fun commandSenderFor( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, + env: IBridgeCommand.CommandEnvironment, op: Boolean - ) = MatterLinkCommandSender(user, userId, server, uuid, username, op) + ) = MatterLinkCommandSender(user, env, op) override val mcVersion: String = MCVERSION override val modVersion: String = MODVERSION diff --git a/1.11.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt b/1.11.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt index e2043f1..ed962bf 100644 --- a/1.11.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt +++ b/1.11.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt @@ -1,5 +1,6 @@ package matterlink.command +import matterlink.bridge.command.IBridgeCommand import matterlink.bridge.command.IMinecraftCommandSender import net.minecraft.command.CommandResultStats import net.minecraft.command.ICommandSender @@ -15,11 +16,8 @@ import javax.annotation.Nonnull class MatterLinkCommandSender( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, - op: Boolean) : IMinecraftCommandSender(user, userId, server, uuid, username, op), ICommandSender { + env: IBridgeCommand.CommandEnvironment, + op: Boolean) : IMinecraftCommandSender(user, env, op), ICommandSender { override fun execute(cmdString: String): Boolean { return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( diff --git a/1.12.2/src/main/kotlin/matterlink/EventHandler.kt b/1.12.2/src/main/kotlin/matterlink/EventHandler.kt index f9fb90e..f9a3976 100644 --- a/1.12.2/src/main/kotlin/matterlink/EventHandler.kt +++ b/1.12.2/src/main/kotlin/matterlink/EventHandler.kt @@ -37,10 +37,11 @@ object EventHandler { @JvmStatic fun chatEvent(e: ServerChatEvent) { if(e.isCanceled) return - ChatProcessor.sendToBridge( + e.isCanceled = ChatProcessor.sendToBridge( user = e.player.displayName.unformattedText, msg = e.message, - event = "" + event = "", + uuid = e.player.gameProfile.id.toString() ) } @@ -65,7 +66,6 @@ object EventHandler { else -> return } ChatProcessor.sendToBridge(user = sender, msg = args, event = type) - } //FORGE-DEPENDENT diff --git a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt index 51073e3..82f77be 100644 --- a/1.12.2/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.12.2/src/main/kotlin/matterlink/MatterLink.kt @@ -1,7 +1,7 @@ package matterlink import com.mojang.authlib.GameProfile -import jline.internal.Log.warn +import matterlink.bridge.command.IBridgeCommand import matterlink.command.AuthCommand import matterlink.command.MatterLinkCommand import matterlink.command.MatterLinkCommandSender @@ -111,12 +111,9 @@ object MatterLink : IMatterLink() { override fun commandSenderFor( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, + env: IBridgeCommand.CommandEnvironment, op: Boolean - ) = MatterLinkCommandSender(user, userId, server, uuid, username, op) + ) = MatterLinkCommandSender(user, env, op) override val mcVersion: String = MCVERSION override val modVersion: String = MODVERSION diff --git a/1.12.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt b/1.12.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt index adde4e1..7a54141 100644 --- a/1.12.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt +++ b/1.12.2/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt @@ -1,5 +1,6 @@ package matterlink.command +import matterlink.bridge.command.IBridgeCommand import matterlink.bridge.command.IMinecraftCommandSender import net.minecraft.command.ICommandSender import net.minecraft.server.MinecraftServer @@ -11,11 +12,8 @@ import javax.annotation.Nonnull class MatterLinkCommandSender( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, - op: Boolean) : IMinecraftCommandSender(user, userId, server, uuid, username, op), ICommandSender { + env: IBridgeCommand.CommandEnvironment, + op: Boolean) : IMinecraftCommandSender(user, env, op), ICommandSender { override fun execute(cmdString: String): Boolean { return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( diff --git a/1.7.10/src/main/kotlin/matterlink/EventHandler.kt b/1.7.10/src/main/kotlin/matterlink/EventHandler.kt index f6f6234..aebeb7d 100644 --- a/1.7.10/src/main/kotlin/matterlink/EventHandler.kt +++ b/1.7.10/src/main/kotlin/matterlink/EventHandler.kt @@ -41,10 +41,11 @@ object EventHandler { @SubscribeEvent fun chatEvent(e: ServerChatEvent) { if(e.isCanceled) return - ChatProcessor.sendToBridge( + e.isCanceled = ChatProcessor.sendToBridge( user = e.player.displayName, msg = e.message, - event = "" + event = "", + uuid = e.player.gameProfile.id.toString() ) } diff --git a/1.7.10/src/main/kotlin/matterlink/MatterLink.kt b/1.7.10/src/main/kotlin/matterlink/MatterLink.kt index 2e7f50e..5b6c8d4 100644 --- a/1.7.10/src/main/kotlin/matterlink/MatterLink.kt +++ b/1.7.10/src/main/kotlin/matterlink/MatterLink.kt @@ -7,6 +7,7 @@ import cpw.mods.fml.common.event.FMLInitializationEvent import cpw.mods.fml.common.event.FMLPreInitializationEvent import cpw.mods.fml.common.event.FMLServerStartingEvent import cpw.mods.fml.common.event.FMLServerStoppingEvent +import matterlink.bridge.command.IBridgeCommand import matterlink.command.MatterLinkCommand import matterlink.command.MatterLinkCommandSender import matterlink.config.BaseConfig @@ -110,12 +111,9 @@ class MatterLink : IMatterLink() { override fun commandSenderFor( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, + env: IBridgeCommand.CommandEnvironment, op: Boolean - ) = MatterLinkCommandSender(user, userId, server, uuid, username, op) + ) = MatterLinkCommandSender(user, env, op) override val mcVersion: String = MCVERSION override val modVersion: String = MODVERSION diff --git a/1.7.10/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt b/1.7.10/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt index 9f20539..564e697 100644 --- a/1.7.10/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt +++ b/1.7.10/src/main/kotlin/matterlink/command/MatterLinkCommandSender.kt @@ -1,5 +1,6 @@ package matterlink.command +import matterlink.bridge.command.IBridgeCommand import matterlink.bridge.command.IMinecraftCommandSender import net.minecraft.command.ICommandSender import net.minecraft.server.MinecraftServer @@ -10,11 +11,8 @@ import net.minecraft.world.World class MatterLinkCommandSender( user: String, - userId: String, - server: String, - uuid: String?, - username: String?, - op: Boolean) : IMinecraftCommandSender(user, userId, server, uuid, username, op), ICommandSender { + env: IBridgeCommand.CommandEnvironment, + op: Boolean) : IMinecraftCommandSender(user, env, op), ICommandSender { override fun execute(cmdString: String): Boolean { return 0 < MinecraftServer.getServer().commandManager.executeCommand( diff --git a/core/src/main/kotlin/matterlink/IMatterLink.kt b/core/src/main/kotlin/matterlink/IMatterLink.kt index 0d3385b..ffa2d85 100644 --- a/core/src/main/kotlin/matterlink/IMatterLink.kt +++ b/core/src/main/kotlin/matterlink/IMatterLink.kt @@ -2,6 +2,7 @@ package matterlink import matterlink.bridge.MessageHandlerInst import matterlink.bridge.command.BridgeCommandRegistry +import matterlink.bridge.command.IBridgeCommand import matterlink.bridge.command.IMinecraftCommandSender import matterlink.config.cfg import matterlink.update.UpdateChecker @@ -13,7 +14,7 @@ abstract class IMatterLink { abstract val modVersion: String abstract val forgeVersion: String - abstract fun commandSenderFor(user: String, userId: String, server: String, uuid: String?, username: String?, op: Boolean): IMinecraftCommandSender + abstract fun commandSenderFor(user: String, env: IBridgeCommand.CommandEnvironment, op: Boolean): IMinecraftCommandSender abstract fun wrappedSendToPlayers(msg: String) diff --git a/core/src/main/kotlin/matterlink/PasteUtil.kt b/core/src/main/kotlin/matterlink/PasteUtil.kt index 7c0215d..1ba0e1f 100644 --- a/core/src/main/kotlin/matterlink/PasteUtil.kt +++ b/core/src/main/kotlin/matterlink/PasteUtil.kt @@ -73,7 +73,7 @@ object PasteUtil { http.doOutput = true val out = jankson.toJson(paste) - .toJson(false, false).apply { println(this) } + .toJson(false, false) .toByteArray() http.setFixedLengthStreamingMode(out.size) diff --git a/core/src/main/kotlin/matterlink/Util.kt b/core/src/main/kotlin/matterlink/Util.kt index e324a45..ca90763 100644 --- a/core/src/main/kotlin/matterlink/Util.kt +++ b/core/src/main/kotlin/matterlink/Util.kt @@ -30,11 +30,11 @@ fun String.mapFormat(env: Map): String { return result } -fun String.lazyFormat(env: Map String>): String { +fun String.lazyFormat(env: Map String?>): String { var result = this env.forEach { key, value -> if (result.contains(key)) { - result = result.replace(key, value()) + result = result.replace(key, value().toString()) } } return result diff --git a/core/src/main/kotlin/matterlink/bridge/command/AuthBridgeCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/AuthBridgeCommand.kt index 201da37..e8a001a 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/AuthBridgeCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/AuthBridgeCommand.kt @@ -1,7 +1,5 @@ package matterlink.bridge.command -import matterlink.api.ApiMessage -import matterlink.bridge.MessageHandlerInst import matterlink.config.AuthRequest import matterlink.config.IdentitiesConfig import matterlink.config.cfg @@ -9,21 +7,28 @@ import matterlink.instance import matterlink.randomString object AuthBridgeCommand : IBridgeCommand() { - override val help: String = "Requests authentication on the bridge. Syntax: auth [username]" + val syntax = "Syntax: auth [username]" + override val help: String = "Requests authentication on the bridge. $syntax" override val permLevel: Double get() = cfg.command.defaultPermUnauthenticated - override fun execute(alias: String, user: String, userId: String, platform: String, uuid: String?, args: String): Boolean { + override fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean { + if (env !is CommandEnvironment.BridgeEnv) { + env.respond("please initiate authentication from linked external chat") + return true + } - if (uuid != null) { - val name = instance.uuidToName(uuid) - respond("you are already authenticated as name: $name uuid: $uuid") + if (env.uuid != null) { + val name = instance.uuidToName(env.uuid) + env.respond("you are already authenticated as name: $name uuid: ${env.uuid}") return true } val argList = args.split(' ', limit = 2) val target = argList.getOrNull(0) ?: run { - respond("no username/uuid provided") + env.respond("no username/uuid provided\n" + + syntax) + return true } @@ -31,7 +36,7 @@ object AuthBridgeCommand : IBridgeCommand() { val targetUUid: String = instance.nameToUUID(target) ?: run { targetUserName = instance.uuidToName(target) ?: run { - respond("cannot find player by username/uuid $target") + env.respond("cannot find player by username/uuid $target") return true } target @@ -39,7 +44,7 @@ object AuthBridgeCommand : IBridgeCommand() { val online = instance.isOnline(targetUserName) if (!online) { - respond("$targetUserName is not online, please log in and try again to send instructions") + env.respond("$targetUserName is not online, please log in and try again to send instructions") return true } val nonce = randomString(length = 3).toUpperCase() @@ -50,19 +55,9 @@ object AuthBridgeCommand : IBridgeCommand() { instance.wrappedSendToPlayer(targetUserName, "otherwise you may ignore this message") - IdentitiesConfig.authRequests.put(requestId, AuthRequest(username = targetUserName, uuid = targetUUid, nonce = nonce, platform = platform, userid = userId)) - respond("please accept the authentication request ingame, do not share the code") + IdentitiesConfig.authRequests.put(requestId, AuthRequest(username = targetUserName, uuid = targetUUid, nonce = nonce, platform = env.platform, userid = env.userId)) + env.respond("please accept the authentication request ingame, do not share the code") return true } - - - private fun respond(text: String) { - MessageHandlerInst.transmit( - ApiMessage( - text = text - ) - ) - } - } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt index 2a5a1b8..d06957a 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt @@ -28,6 +28,7 @@ object BridgeCommandRegistry { val uuid = IdentitiesConfig.getUUID(input.account, input.userid) + val env = IBridgeCommand.CommandEnvironment.BridgeEnv(input.username, input.userid, input.account, uuid) return commandMap[cmd[0]]?.let { if (!it.reachedTimeout()) { instance.debug("dropped command ${it.alias}") @@ -35,14 +36,39 @@ object BridgeCommandRegistry { } it.preExecute() // resets the tickCounter if (!it.canExecute(uuid)) { - MessageHandlerInst.transmit( - ApiMessage( - text = "${input.username} is not permitted to perform command: ${cmd[0]}".stripColorOut - ) + env.respond( + text = "${input.username} is not permitted to perform command: ${cmd[0]}" ) return false } - it.execute(cmd[0], input.username, input.userid, input.account, uuid, args) + it.execute(cmd[0], input.username, env, args) + } ?: false + } + + fun handleCommand(text: String, username: String, uuid: String): Boolean { + if (!cfg.command.enable || text.isBlank()) return false + + if (text[0] != cfg.command.prefix || text.length < 2) return false + + val cmd = text.substring(1).split(' ', ignoreCase = false, limit = 2) + val args = if (cmd.size == 2) cmd[1] else "" + + val env = IBridgeCommand.CommandEnvironment.GameEnv(username, uuid) + + return commandMap[cmd[0]]?.let { + if (!it.reachedTimeout()) { + instance.debug("dropped command ${it.alias}") + return false + } + it.preExecute() // resets the tickCounter + if (!it.canExecute(uuid)) { + env.respond( + text = "$username is not permitted to perform command: ${cmd[0]}" + ) + return false + } + + it.execute(cmd[0], username, env, args) } ?: false } @@ -102,4 +128,5 @@ object BridgeCommandRegistry { } return null } + } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt index f47ec80..9dc102c 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt @@ -1,7 +1,5 @@ package matterlink.bridge.command -import matterlink.api.ApiMessage -import matterlink.bridge.MessageHandlerInst import matterlink.instance import matterlink.lazyFormat import matterlink.stripColorIn @@ -19,37 +17,27 @@ data class CustomCommand( val argumentsRegex: Regex? = null ) : IBridgeCommand() { - override fun execute(alias: String, user: String, userId: String, platform: String, uuid: String?, args: String): Boolean { + override fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean { if (argumentsRegex != null) { instance.debug("testing '$args' against '${argumentsRegex.pattern}'") if (!argumentsRegex.matches(args)) { - MessageHandlerInst.transmit( - ApiMessage( - text = "$user sent invalid input to command $alias".stripColorOut - ) - ) + env.respond("$user sent invalid input to command $alias") return false } } - val username = instance.uuidToName(uuid) - return when (type) { CommandType.EXECUTE -> { // uses a new commandsender for each use - val commandSender = instance.commandSenderFor(user, userId, platform, uuid, username, execOp ?: false) - val cmd = execute?.lazyFormat(getReplacements(user, userId, platform, uuid, args))?.stripColorIn + val commandSender = instance.commandSenderFor(user, env, execOp ?: false) + val cmd = execute?.lazyFormat(getReplacements(user, env, args))?.stripColorIn ?: return false commandSender.execute(cmd) || commandSender.reply.isNotEmpty() } CommandType.RESPONSE -> { - MessageHandlerInst.transmit( - ApiMessage( - text = (response?.lazyFormat(getReplacements(user, userId, platform, uuid, args)) - ?: "") - ), - cause = "response to command: $alias" - ) + env.respond(response?.lazyFormat(getReplacements(user, env, args)) + ?: "", cause = "response to command: $alias") + true } } @@ -71,13 +59,33 @@ data class CustomCommand( companion object { val DEFAULT = CustomCommand() - fun getReplacements(user: String, userId: String, platform: String, uuid: String?, args: String): Map String> = mapOf( + fun getReplacements(user: String, env: CommandEnvironment, args: String): Map String?> = mapOf( "{uptime}" to instance::getUptimeAsString, "{user}" to { user }, - "{userid}" to { userId }, - "{uuid}" to { uuid.toString() }, - "{username}" to { uuid?.let { instance.uuidToName(it) }.toString() }, - "{platform}" to { platform }, + "{userid}" to { + when (env) { + is CommandEnvironment.BridgeEnv -> env.userId + else -> null + } + }, + "{uuid}" to { + when (env) { + is CommandEnvironment.BridgeEnv -> env.uuid + is CommandEnvironment.GameEnv -> env.uuid + } + }, + "{username}" to { + when (env) { + is CommandEnvironment.BridgeEnv -> env.uuid + is CommandEnvironment.GameEnv -> env.uuid + }?.let { instance.uuidToName(it) } + }, + "{platform}" to { + when (env) { + is CommandEnvironment.BridgeEnv -> env.platform + else -> null + } + }, "{args}" to { args } ) } diff --git a/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt index 3c9d830..29d415f 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt @@ -10,19 +10,17 @@ object HelpCommand : IBridgeCommand() { override val permLevel: Double get() = cfg.command.defaultPermUnauthenticated - override fun execute(alias: String, user: String, userId: String, platform: String, uuid: String?, args: String): Boolean { + override fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean { val msg: String = when { args.isEmpty() -> - "Available commands: ${BridgeCommandRegistry.getCommandList(IBridgeCommand.getPermLevel(uuid))}" + "Available commands: ${BridgeCommandRegistry.getCommandList(IBridgeCommand.getPermLevel(env.uuid))}" else -> args.split(" ", ignoreCase = false) .joinToString(separator = "\n") { "$it: ${BridgeCommandRegistry.getHelpString(it)}" } } - MessageHandlerInst.transmit( - ApiMessage( - text = msg.stripColorOut - ), + env.respond( + text = msg, cause = "Help Requested $args" ) return true diff --git a/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt index ec1fea7..c2473a6 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt @@ -6,18 +6,55 @@ import matterlink.config.PermissionConfig import matterlink.config.cfg import matterlink.handlers.TickHandler import matterlink.instance +import matterlink.stripColorOut abstract class IBridgeCommand { abstract val help: String abstract val permLevel: Double open val timeout: Int = 20 - protected var lastUsed: Int = 0 + sealed class CommandEnvironment { + abstract val uuid: String? + abstract val username: String? + + data class BridgeEnv( + val name: String, + val userId: String, + val platform: String, + override val uuid: String? + ) : CommandEnvironment() { + override val username: String? + get() = instance.uuidToName(uuid) + } + data class GameEnv( + override val username: String, + override val uuid: String + ) : CommandEnvironment() + + fun respond(text: String, cause: String = "") { + when(this) { + is BridgeEnv -> { + MessageHandlerInst.transmit( + ApiMessage( + text = text.stripColorOut + ), + cause = cause + ) + } + is GameEnv -> { + instance.wrappedSendToPlayer(uuid, text) + } + } + + } + } + + + private var lastUsed: Int = 0 val alias: String get() = BridgeCommandRegistry.getName(this)!! - fun reachedTimeout(): Boolean { return (TickHandler.tickCounter - lastUsed > timeout) } @@ -30,7 +67,7 @@ abstract class IBridgeCommand { * * @return consume message flag */ - abstract fun execute(alias: String, user: String, userId: String, platform: String, uuid: String?, args: String): Boolean + abstract fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean fun canExecute(uuid: String?): Boolean { instance.trace("canExecute this: $this uuid: $uuid permLevel: $permLevel") diff --git a/core/src/main/kotlin/matterlink/bridge/command/IMinecraftCommandSender.kt b/core/src/main/kotlin/matterlink/bridge/command/IMinecraftCommandSender.kt index 9d6f02f..838caff 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/IMinecraftCommandSender.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/IMinecraftCommandSender.kt @@ -1,11 +1,8 @@ package matterlink.bridge.command -import matterlink.api.ApiMessage -import matterlink.bridge.MessageHandlerInst import matterlink.stripColorOut -import java.awt.SystemColor.text -abstract class IMinecraftCommandSender(val user: String, val userId: String, val server: String, val uuid: String?, val username: String?, val op: Boolean) { +abstract class IMinecraftCommandSender(val user: String, val env: IBridgeCommand.CommandEnvironment, val op: Boolean) { /** * @param cmdString The command to execute with its arguments * @@ -13,23 +10,27 @@ abstract class IMinecraftCommandSender(val user: String, val userId: String, val */ abstract fun execute(cmdString: String): Boolean - val displayName = username ?: user - val accountName = "$user (id=$userId server=$server uuid=$uuid)" + val displayName = env.username ?: user + val accountName = when (env) { + is IBridgeCommand.CommandEnvironment.BridgeEnv -> "$user (id=${env.userId} platform=${env.platform}${env.uuid?.let { " uuid=$it" } + ?: ""}${env.username?.let { " username=$it" } ?: ""})" + is IBridgeCommand.CommandEnvironment.GameEnv -> "$user (username=${env.username} uuid=${env.uuid})" + } fun canExecute(commandName: String): Boolean { if (op) return true val command = BridgeCommandRegistry[commandName] ?: return false - return command.canExecute(uuid) + return command.canExecute(env.uuid) } - var finished = true + private var finished = true val reply = mutableListOf() /** * accumulates response */ fun appendReply(text: String) { - if(finished) { + if (finished) { reply.clear() finished = false } @@ -37,10 +38,8 @@ abstract class IMinecraftCommandSender(val user: String, val userId: String, val } fun sendReply(cmdString: String) { - MessageHandlerInst.transmit( - msg = ApiMessage( - text = reply.joinToString("\n").stripColorOut - ), + env.respond( + text = reply.joinToString("\n"), cause = "executed command: $cmdString" ) finished = true diff --git a/core/src/main/kotlin/matterlink/bridge/command/RequestPermissionsCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/RequestPermissionsCommand.kt index 47bf457..339e4ed 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/RequestPermissionsCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/RequestPermissionsCommand.kt @@ -1,29 +1,30 @@ package matterlink.bridge.command -import matterlink.api.ApiMessage -import matterlink.bridge.MessageHandlerInst import matterlink.config.PermissionConfig import matterlink.config.PermissionRequest import matterlink.config.cfg import matterlink.randomString object RequestPermissionsCommand : IBridgeCommand() { - override val help: String = "Requests permissions on the bridge. Syntax: request [permissionLevel]" + val syntax = " Syntax: request [permissionLevel]" + override val help: String = "Requests permissions on the bridge. $syntax" override val permLevel: Double get() = cfg.command.defaultPermAuthenticated - override fun execute(alias: String, user: String, userId: String, platform: String, uuid: String?, args: String): Boolean { + override fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean { + val uuid = env.uuid if (uuid == null) { - respond("$user is not authenticated (userid: $userId platform: $platform)") + env.respond("$user is not authenticated ($env)") return true } val argList = args.split(' ', limit = 2) val requestedLevelArg = argList.getOrNull(0) - val requestedlevel = requestedLevelArg?.let { + val requestedLevel = requestedLevelArg?.takeIf { it.isNotEmpty() }?.let { it.toDoubleOrNull() ?: run { - respond("cannot parse permlevel") + env.respond("cannot parse permlevel '$requestedLevelArg'\n" + + syntax) return true } } @@ -32,19 +33,9 @@ object RequestPermissionsCommand : IBridgeCommand() { val requestId = user.toLowerCase() - PermissionConfig.permissionRequests.put(requestId, PermissionRequest(uuid = uuid, user = user, nonce = nonce, powerlevel = requestedlevel)) - respond("please ask a op to accept your permission elevation with `/ml permAccept $requestId $nonce [powerlevel]`") + PermissionConfig.permissionRequests.put(requestId, PermissionRequest(uuid = uuid, user = user, nonce = nonce, powerlevel = requestedLevel)) + env.respond("please ask a op to accept your permission elevation with `/ml permAccept $requestId $nonce [permLevel]`") return true } - - - private fun respond(text: String) { - MessageHandlerInst.transmit( - ApiMessage( - text = text - ) - ) - } - } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 7b96321..63842d4 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -113,8 +113,8 @@ data class BaseConfig(val rootDir: File) { data class JoinPartOptions( val enable: Boolean = true, - val joinServer: String = "{username:antiping} has connected to the server", - val partServer: String = "{username:antiping} has disconnected from the server" + val joinServer: String = "{username:antiping} has connected to the platform", + val partServer: String = "{username:antiping} has disconnected from the platform" ) data class UpdateOptions( @@ -208,12 +208,12 @@ data class BaseConfig(val rootDir: File) { url = it.getOrDefault( "url", url, - "The URL or IP address of the bridge server" + "The URL or IP address of the bridge platform" ), authToken = it.getOrDefault( "authToken", authToken, - "Auth token used to connect to the bridge server" + "Auth token used to connect to the bridge platform" ), gateway = it.getOrDefault( "gateway", @@ -267,7 +267,7 @@ data class BaseConfig(val rootDir: File) { systemUser = it.getOrDefault( "systemUser", systemUser, - "Name of the server user (used by death and advancement messages and the /say command)" + "Name of the platform user (used by death and advancement messages and the /say command)" ), advancements = it.getOrDefault( "advancements", diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index e21ef8a..eb39afe 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -26,7 +26,7 @@ object CommandConfig { to CustomCommand( type = CommandType.EXECUTE, execute = "forge tps", - help = "Print server tps", + help = "Print platform tps", timeout = 200, defaultCommand = true )), @@ -41,14 +41,14 @@ object CommandConfig { to CustomCommand( type = CommandType.EXECUTE, execute = "seed", - help = "Print server world seed", + help = "Print platform world seed", defaultCommand = true )), "uptime" to ("this is a reponse command, it uses the uptime function, time since the mod was first loaded" to CustomCommand( type = CommandType.RESPONSE, response = "{uptime}", - help = "Print server uptime", + help = "Print platform uptime", defaultCommand = true )), "whoami" to ("this shows you some of the other response macros" diff --git a/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt b/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt index 4a90669..20a4a31 100644 --- a/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt +++ b/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt @@ -2,12 +2,17 @@ package matterlink.handlers import matterlink.api.ApiMessage import matterlink.bridge.MessageHandlerInst +import matterlink.bridge.command.BridgeCommandRegistry import matterlink.instance import matterlink.stripColorOut object ChatProcessor { - fun sendToBridge(user: String, msg: String, event: String) { + /** + * @return cancel message flag + */ + fun sendToBridge(user: String, msg: String, event: String, uuid: String? = null): Boolean { val message = msg.trim() + if(uuid != null && BridgeCommandRegistry.handleCommand(message, user, uuid)) return true when { message.isNotBlank() -> MessageHandlerInst.transmit( ApiMessage( @@ -19,5 +24,6 @@ object ChatProcessor { ) else -> instance.warn("WARN: dropped blank message by '$user'") } + return false } }