execute bridge commands ingame
This commit is contained in:
parent
42dcd2c543
commit
4627a60c9d
|
@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -30,11 +30,11 @@ fun String.mapFormat(env: Map<String, String>): String {
|
|||
return result
|
||||
}
|
||||
|
||||
fun String.lazyFormat(env: Map<String, () -> String>): String {
|
||||
fun String.lazyFormat(env: Map<String, () -> 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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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, () -> String> = mapOf(
|
||||
fun getReplacements(user: String, env: CommandEnvironment, args: String): Map<String, () -> 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 }
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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<String>()
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue