paste long output

This commit is contained in:
nikky 2018-07-10 02:54:21 +02:00
parent 13a607fcd1
commit 42dcd2c543
18 changed files with 193 additions and 52 deletions

View File

@ -25,7 +25,9 @@ class MatterLinkCommandSender(
return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand(
this, this,
cmdString cmdString
) ).apply {
sendReply(cmdString)
}
} }
override fun getDisplayName(): ITextComponent { override fun getDisplayName(): ITextComponent {
@ -48,8 +50,7 @@ class MatterLinkCommandSender(
} }
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
sendReply(component!!.unformattedComponentText) appendReply(component!!.unformattedComponentText)
} }
override fun sendCommandFeedback(): Boolean { override fun sendCommandFeedback(): Boolean {

View File

@ -25,7 +25,9 @@ class MatterLinkCommandSender(
return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand(
this, this,
cmdString cmdString
) ).apply {
sendReply(cmdString)
}
} }
override fun getDisplayName(): ITextComponent { override fun getDisplayName(): ITextComponent {
@ -48,7 +50,7 @@ class MatterLinkCommandSender(
} }
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
sendReply(component!!.unformattedComponentText) appendReply(component!!.unformattedComponentText)
} }
override fun sendCommandFeedback(): Boolean { override fun sendCommandFeedback(): Boolean {

View File

@ -1,5 +1,6 @@
package matterlink.command package matterlink.command
import matterlink.logger
import net.minecraft.command.CommandBase import net.minecraft.command.CommandBase
import net.minecraft.command.ICommandSender import net.minecraft.command.ICommandSender
import net.minecraft.command.WrongUsageException import net.minecraft.command.WrongUsageException

View File

@ -21,7 +21,9 @@ class MatterLinkCommandSender(
return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand( return 0 < FMLCommonHandler.instance().minecraftServerInstance.commandManager.executeCommand(
this, this,
cmdString cmdString
) ).apply {
sendReply(cmdString)
}
} }
override fun getDisplayName(): ITextComponent { override fun getDisplayName(): ITextComponent {
@ -44,7 +46,7 @@ class MatterLinkCommandSender(
} }
override fun sendMessage(@Nonnull component: ITextComponent?) { override fun sendMessage(@Nonnull component: ITextComponent?) {
sendReply(component!!.unformattedComponentText) appendReply(component!!.unformattedComponentText)
} }
override fun sendCommandFeedback(): Boolean { override fun sendCommandFeedback(): Boolean {

View File

@ -20,7 +20,9 @@ class MatterLinkCommandSender(
return 0 < MinecraftServer.getServer().commandManager.executeCommand( return 0 < MinecraftServer.getServer().commandManager.executeCommand(
this, this,
cmdString cmdString
) ).apply {
sendReply(cmdString)
}
} }
override fun getFormattedCommandSenderName(): IChatComponent { override fun getFormattedCommandSenderName(): IChatComponent {
@ -39,7 +41,7 @@ class MatterLinkCommandSender(
} }
override fun addChatMessage(component: IChatComponent) { override fun addChatMessage(component: IChatComponent) {
sendReply(component.unformattedText) appendReply(component.unformattedText)
} }
override fun getCommandSenderPosition(): ChunkCoordinates = ChunkCoordinates(0, 0, 0) override fun getCommandSenderPosition(): ChunkCoordinates = ChunkCoordinates(0, 0, 0)

2
api

@ -1 +1 @@
Subproject commit 8e17cb9937e333c099f8115289b93482dfa76a1d Subproject commit 15e4d115dd0e1ac1e5d287c48d6dc76a6a488ed7

View File

@ -0,0 +1,92 @@
package matterlink
import blue.endless.jankson.Jankson
import blue.endless.jankson.JsonObject
import blue.endless.jankson.impl.Marshaller
import java.net.HttpURLConnection
import java.net.URL
/**
* Created by nikky on 09/07/18.
* @author Nikky
*/
data class Paste(
val encrypted: Boolean = false,
val description: String,
val sections: List<PasteSection>
)
data class PasteSection(
val name: String,
val syntax: String = "text",
val contents: String
)
data class PasteResponse(
val id: String,
val link: String
)
object PasteUtil {
private const val DEFAULT_KEY = "uKJoyicVJFnmpnrIZMklOURWxrCKXYaiBWOzPmvon"
private val jankson = Jankson.builder()
.registerTypeAdapter {
PasteResponse(
id = it.getReified("id") ?: "",
link = it.getReified<String>("link")
?.replace("\\/", "/")
?: "invalid"
)
}
.registerSerializer { paste: Paste, marshaller: Marshaller ->
JsonObject().apply {
with(paste) {
if (description.isNotBlank())
this@apply["description"] = marshaller.serialize(description)
if (encrypted)
this@apply["encrypted"] = marshaller.serialize(encrypted)
this@apply["sections"] = marshaller.serialize(sections)
}
}
}
.registerSerializer { section: PasteSection, marshaller: Marshaller ->
JsonObject().apply {
with(section) {
if (name.isNotBlank())
this@apply["name"] = marshaller.serialize(name)
this@apply["syntax"] = marshaller.serialize(syntax)
this@apply["contents"] = marshaller.serialize(contents.replace("\n", "\\n"))
}
}
}
.build()
fun paste(paste: Paste, key: String = ""): PasteResponse {
val apiKey = key.takeIf { it.isNotBlank() } ?: DEFAULT_KEY
val url = URL("https://api.paste.ee/v1/pastes")
val http = url.openConnection() as HttpURLConnection
http.requestMethod = "POST"
http.doOutput = true
val out = jankson.toJson(paste)
.toJson(false, false).apply { println(this) }
.toByteArray()
http.setFixedLengthStreamingMode(out.size)
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8")
http.setRequestProperty("X-Auth-Token", apiKey)
http.connect()
http.outputStream.use { os ->
os.write(out)
}
val textResponse = http.inputStream.bufferedReader().use { it.readText() }
instance.debug("response: $textResponse")
// val jsonObject = jankson.load(http.inputStream)
return jankson.fromJson(textResponse)
}
}

View File

@ -1,15 +0,0 @@
package matterlink
import com.google.gson.*
import java.lang.reflect.Type
object RegexDeSerializer: JsonSerializer<Regex>, JsonDeserializer<Regex> {
override fun serialize(src: Regex, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(src.pattern)
}
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Regex {
return json.asString.toRegex()
}
}

View File

@ -73,6 +73,7 @@ fun <T : Any> JsonObject.getOrDefault(key: String, default: T, comment: String?
} }
inline fun <reified T : Any> Jankson.fromJson(obj: JsonObject): T = this.fromJson(obj, T::class.java) inline fun <reified T : Any> Jankson.fromJson(obj: JsonObject): T = this.fromJson(obj, T::class.java)
inline fun <reified T : Any> Jankson.fromJson(json: String): T = this.fromJson(json, T::class.java)
inline fun <reified T : Any> Jankson.Builder.registerTypeAdapter(noinline adapter: (JsonObject) -> T) = this.registerTypeAdapter(T::class.java, adapter) inline fun <reified T : Any> Jankson.Builder.registerTypeAdapter(noinline adapter: (JsonObject) -> T) = this.registerTypeAdapter(T::class.java, adapter)

View File

@ -1,11 +1,38 @@
package matterlink.bridge package matterlink.bridge
import matterlink.antiping import matterlink.*
import matterlink.api.ApiMessage import matterlink.api.ApiMessage
import matterlink.api.MessageHandler import matterlink.api.MessageHandler
import matterlink.mapFormat
object MessageHandlerInst: MessageHandler() object MessageHandlerInst : MessageHandler() {
override fun transmit(msg: ApiMessage) {
transmit(msg, cause = "")
}
fun transmit(msg: ApiMessage, cause: String, maxLines: Int = 3) {
if (msg.text.count { it == '\n' } >= maxLines) {
try {
val response = PasteUtil.paste(
Paste(
description = cause,
sections = listOf(
PasteSection(
name = "log.txt",
syntax = "text",
contents = msg.text
)
)
)
)
msg.text = msg.text.substringBefore('\n')
.take(15) + "... " + response.link
} catch(e: Exception) {
instance.error(e.stackTraceString)
}
}
super.transmit(msg)
}
}
fun ApiMessage.format(fmt: String): String { fun ApiMessage.format(fmt: String): String {
return fmt.mapFormat( return fmt.mapFormat(

View File

@ -40,14 +40,15 @@ data class CustomCommand(
val commandSender = instance.commandSenderFor(user, userId, platform, uuid, username, execOp ?: false) val commandSender = instance.commandSenderFor(user, userId, platform, uuid, username, execOp ?: false)
val cmd = execute?.lazyFormat(getReplacements(user, userId, platform, uuid, args))?.stripColorIn val cmd = execute?.lazyFormat(getReplacements(user, userId, platform, uuid, args))?.stripColorIn
?: return false ?: return false
commandSender.execute(cmd) || commandSender.reply.isNotBlank() commandSender.execute(cmd) || commandSender.reply.isNotEmpty()
} }
CommandType.RESPONSE -> { CommandType.RESPONSE -> {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = (response?.lazyFormat(getReplacements(user, userId, platform, uuid, args)) text = (response?.lazyFormat(getReplacements(user, userId, platform, uuid, args))
?: "") ?: "")
) ),
cause = "response to command: $alias"
) )
true true
} }

View File

@ -22,7 +22,8 @@ object HelpCommand : IBridgeCommand() {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = msg.stripColorOut text = msg.stripColorOut
) ),
cause = "Help Requested $args"
) )
return true return true
} }

View File

@ -3,6 +3,7 @@ package matterlink.bridge.command
import matterlink.api.ApiMessage import matterlink.api.ApiMessage
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.stripColorOut 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 userId: String, val server: String, val uuid: String?, val username: String?, val op: Boolean) {
/** /**
@ -21,14 +22,27 @@ abstract class IMinecraftCommandSender(val user: String, val userId: String, val
return command.canExecute(uuid) return command.canExecute(uuid)
} }
var reply: String = "" var finished = true
val reply = mutableListOf<String>()
fun sendReply(text: String) { /**
reply = text * accumulates response
*/
fun appendReply(text: String) {
if(finished) {
reply.clear()
finished = false
}
reply += text
}
fun sendReply(cmdString: String) {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( msg = ApiMessage(
text = text.stripColorOut text = reply.joinToString("\n").stripColorOut
) ),
cause = "executed command: $cmdString"
) )
finished = true
} }
} }

View File

@ -52,26 +52,27 @@ data class BaseConfig(val rootDir: File) {
) )
data class DebugOptions( data class DebugOptions(
var logLevel: String = "INFO" val logLevel: String = "INFO"
) )
data class IncomingOptions( data class IncomingOptions(
val chat: String = "<{username}> {text}", val chat: String = "<{username}> {text}",
val joinPart: String = "§6-- {username} {text}", val joinPart: String = "§6-- {username} {text}",
val action: String = "§5* {username} {text}", val action: String = "§5* {username} {text}",
var stripColors: Boolean = true val stripColors: Boolean = true
) )
data class OutgoingOptions( data class OutgoingOptions(
val systemUser: String = "Server", val systemUser: String = "Server",
//outgoing toggles //outgoing toggles
var announceConnect: Boolean = true, val announceConnect: Boolean = true,
var announceDisconnect: Boolean = true, val announceDisconnect: Boolean = true,
val advancements: Boolean = true, val advancements: Boolean = true,
var stripColors: Boolean = true, val stripColors: Boolean = true,
val pasteEEKey: String = "",
var joinPart: JoinPartOptions = JoinPartOptions(), val joinPart: JoinPartOptions = JoinPartOptions(),
var death: DeathOptions = DeathOptions() val death: DeathOptions = DeathOptions()
) )
data class DeathOptions( data class DeathOptions(
@ -288,6 +289,11 @@ data class BaseConfig(val rootDir: File) {
stripColors, stripColors,
"strip colors from nicknames and messages" "strip colors from nicknames and messages"
), ),
pasteEEKey = it.getOrDefault(
"pasteEEKey",
pasteEEKey,
"paste.ee api key, leave empty to use application default"
),
death = it.getOrDefault( death = it.getOrDefault(
"death", "death",
DeathOptions(), DeathOptions(),

View File

@ -9,10 +9,13 @@ object ChatProcessor {
fun sendToBridge(user: String, msg: String, event: String) { fun sendToBridge(user: String, msg: String, event: String) {
val message = msg.trim() val message = msg.trim()
when { when {
message.isNotBlank() -> MessageHandlerInst.transmit(ApiMessage( message.isNotBlank() -> MessageHandlerInst.transmit(
username = user.stripColorOut, ApiMessage(
text = message.stripColorOut, username = user.stripColorOut,
event = event) text = message.stripColorOut,
event = event
),
cause = "Message from $user"
) )
else -> instance.warn("WARN: dropped blank message by '$user'") else -> instance.warn("WARN: dropped blank message by '$user'")
} }

View File

@ -23,7 +23,7 @@ object DeathHandler {
val damageEmoji = emojis[random.nextInt(emojis.size)] val damageEmoji = emojis[random.nextInt(emojis.size)]
msg += " $damageEmoji" msg += " $damageEmoji"
} }
MessageHandlerInst.transmit(ApiMessage(text = msg)) MessageHandlerInst.transmit(ApiMessage(text = msg), cause = "Death Event of $player")
} }
} }
} }

View File

@ -21,7 +21,8 @@ object JoinLeaveHandler {
ApiMessage( ApiMessage(
text = msg, text = msg,
event = JOIN_LEAVE event = JOIN_LEAVE
) ),
cause = "$player joined"
) )
} }
} }
@ -38,7 +39,8 @@ object JoinLeaveHandler {
ApiMessage( ApiMessage(
text = msg, text = msg,
event = JOIN_LEAVE event = JOIN_LEAVE
) ),
cause = "$player left"
) )
} }
} }

View File

@ -14,7 +14,8 @@ object ProgressHandler {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = "$usr $message $display".stripColorOut text = "$usr $message $display".stripColorOut
) ),
cause = "Progress Event by $usr"
) )
} }
} }