fixed crash due to runtime constraints

This commit is contained in:
nikky 2018-07-11 00:30:09 +02:00
parent 4627a60c9d
commit 83501c867f
21 changed files with 152 additions and 99 deletions

View File

@ -48,7 +48,7 @@ object EventHandler {
user = e.player.displayName.unformattedText,
msg = e.message,
event = "",
uuid = e.player.gameProfile.id.toString()
uuid = e.player.gameProfile.id
)
}

View File

@ -65,9 +65,21 @@ object MatterLink : IMatterLink() {
FMLCommonHandler.instance().minecraftServerInstance.playerList.sendChatMsg(TextComponentString(msg))
}
override fun wrappedSendToPlayer(user: String, msg: String) {
val profile = profileByName(user) ?: profileByUUID(user) ?: run {
error("cannot find player by name or uuid $user")
override fun wrappedSendToPlayer(username: String, msg: String) {
val profile = profileByName(username) ?: run {
error("cannot find player by name $username")
return
}
val player = playerByProfile(profile) ?: run {
error("${profile.name} is not online")
return
}
player.sendMessage(TextComponentString(msg))
}
override fun wrappedSendToPlayer(uuid: UUID, msg: String) {
val profile = profileByUUID(uuid) ?: run {
error("cannot find player by uuid $uuid")
return
}
val player = playerByProfile(profile) ?: run {
@ -82,8 +94,8 @@ object MatterLink : IMatterLink() {
private fun playerByProfile(gameProfile: GameProfile): EntityPlayerMP? = FMLCommonHandler.instance().minecraftServerInstance.playerList.getPlayerByUUID(gameProfile.id)
private fun profileByUUID(uuid: String): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(UUID.fromString(uuid))
private fun profileByUUID(uuid: UUID): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(uuid)
} catch (e: IllegalArgumentException) {
warn("cannot find profile by uuid $uuid")
null
@ -96,11 +108,9 @@ object MatterLink : IMatterLink() {
null
}
override fun nameToUUID(username: String) = profileByName(username)?.id?.toString()
override fun nameToUUID(username: String): UUID? = profileByName(username)?.id
override fun uuidToName(uuid: String?): String? {
return uuid?.let { profileByUUID(it)?.name }
}
override fun uuidToName(uuid: UUID): String? = profileByUUID(uuid)?.name
override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO), formatString, *data)

View File

@ -49,7 +49,7 @@ object EventHandler {
user = e.player.displayName.unformattedText,
msg = e.message,
event = "",
uuid = e.player.gameProfile.id.toString()
uuid = e.player.gameProfile.id
)
}

View File

@ -65,9 +65,21 @@ object MatterLink : IMatterLink() {
FMLCommonHandler.instance().minecraftServerInstance.playerList.sendMessage(TextComponentString(msg))
}
override fun wrappedSendToPlayer(user: String, msg: String) {
val profile = profileByName(user) ?: profileByUUID(user) ?: run {
error("cannot find player by name or uuid $user")
override fun wrappedSendToPlayer(username: String, msg: String) {
val profile = profileByName(username) ?: run {
error("cannot find player by name $username")
return
}
val player = playerByProfile(profile) ?: run {
error("${profile.name} is not online")
return
}
player.sendMessage(TextComponentString(msg))
}
override fun wrappedSendToPlayer(uuid: UUID, msg: String) {
val profile = profileByUUID(uuid) ?: run {
error("cannot find player by uuid $uuid")
return
}
val player = playerByProfile(profile) ?: run {
@ -82,8 +94,8 @@ object MatterLink : IMatterLink() {
private fun playerByProfile(gameProfile: GameProfile): EntityPlayerMP? = FMLCommonHandler.instance().minecraftServerInstance.playerList.getPlayerByUUID(gameProfile.id)
private fun profileByUUID(uuid: String): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(UUID.fromString(uuid))
private fun profileByUUID(uuid: UUID): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(uuid)
} catch (e: IllegalArgumentException) {
warn("cannot find profile by uuid $uuid")
null
@ -96,11 +108,9 @@ object MatterLink : IMatterLink() {
null
}
override fun nameToUUID(username: String) = profileByName(username)?.id?.toString()
override fun nameToUUID(username: String): UUID? = profileByName(username)?.id
override fun uuidToName(uuid: String?): String? {
return uuid?.let { profileByUUID(it)?.name }
}
override fun uuidToName(uuid: UUID): String? = profileByUUID(uuid)?.name
override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO), formatString, *data)

View File

@ -41,7 +41,7 @@ object EventHandler {
user = e.player.displayName.unformattedText,
msg = e.message,
event = "",
uuid = e.player.gameProfile.id.toString()
uuid = e.player.gameProfile.id
)
}

View File

@ -68,9 +68,21 @@ object MatterLink : IMatterLink() {
FMLCommonHandler.instance().minecraftServerInstance.playerList.sendMessage(TextComponentString(msg))
}
override fun wrappedSendToPlayer(user: String, msg: String) {
val profile = profileByName(user) ?: profileByUUID(user) ?: run {
error("cannot find player by name or uuid $user")
override fun wrappedSendToPlayer(username: String, msg: String) {
val profile = profileByName(username) ?: run {
error("cannot find player by name $username")
return
}
val player = playerByProfile(profile) ?: run {
error("${profile.name} is not online")
return
}
player.sendMessage(TextComponentString(msg))
}
override fun wrappedSendToPlayer(uuid: UUID, msg: String) {
val profile = profileByUUID(uuid) ?: run {
error("cannot find player by uuid $uuid")
return
}
val player = playerByProfile(profile) ?: run {
@ -82,12 +94,11 @@ object MatterLink : IMatterLink() {
override fun isOnline(username: String) = FMLCommonHandler.instance().minecraftServerInstance.onlinePlayerNames.contains(username)
private fun playerByProfile(gameProfile: GameProfile): EntityPlayerMP?
= FMLCommonHandler.instance().minecraftServerInstance.playerList.getPlayerByUUID(gameProfile.id)
private fun playerByProfile(gameProfile: GameProfile): EntityPlayerMP? = FMLCommonHandler.instance().minecraftServerInstance.playerList.getPlayerByUUID(gameProfile.id)
private fun profileByUUID(uuid: String): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(UUID.fromString(uuid))
private fun profileByUUID(uuid: UUID): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.getProfileByUUID(uuid)
} catch (e: IllegalArgumentException) {
warn("cannot find profile by uuid $uuid")
null
@ -100,11 +111,9 @@ object MatterLink : IMatterLink() {
null
}
override fun nameToUUID(username: String) = profileByName(username)?.id?.toString()
override fun nameToUUID(username: String): UUID? = profileByName(username)?.id
override fun uuidToName(uuid: String?): String? {
return uuid?.let { profileByUUID(it)?.name }
}
override fun uuidToName(uuid: UUID): String? = profileByUUID(uuid)?.name
override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO), formatString, *data)

View File

@ -45,7 +45,7 @@ object EventHandler {
user = e.player.displayName,
msg = e.message,
event = "",
uuid = e.player.gameProfile.id.toString()
uuid = e.player.gameProfile.id
)
}

View File

@ -67,9 +67,21 @@ class MatterLink : IMatterLink() {
MinecraftServer.getServer().configurationManager.sendChatMsg(ChatComponentText(msg))
}
override fun wrappedSendToPlayer(user: String, msg: String) {
val profile = profileByName(user) ?: profileByUUID(user) ?: run {
error("cannot find player by name or uuid $user")
override fun wrappedSendToPlayer(username: String, msg: String) {
val profile = profileByName(username) ?: run {
error("cannot find player by name $username")
return
}
val player = playerByProfile(profile) ?: run {
error("${profile.name} is not online")
return
}
player.addChatMessage(ChatComponentText(msg))
}
override fun wrappedSendToPlayer(uuid: UUID, msg: String) {
val profile = profileByUUID(uuid) ?: run {
error("cannot find player by uuid $uuid")
return
}
val player = playerByProfile(profile) ?: run {
@ -86,8 +98,8 @@ class MatterLink : IMatterLink() {
return FMLCommonHandler.instance().minecraftServerInstance.configurationManager.createPlayerForUser(gameProfile)
}
private fun profileByUUID(uuid: String): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.func_152652_a(UUID.fromString(uuid))
private fun profileByUUID(uuid: UUID): GameProfile? = try {
FMLCommonHandler.instance().minecraftServerInstance.playerProfileCache.func_152652_a(uuid)
} catch (e: IllegalArgumentException) {
warn("cannot find profile by uuid $uuid")
null
@ -100,11 +112,9 @@ class MatterLink : IMatterLink() {
null
}
override fun nameToUUID(username: String) = profileByName(username)?.id?.toString()
override fun nameToUUID(username: String): UUID? = profileByName(username)?.id
override fun uuidToName(uuid: String?): String? {
return uuid?.let { profileByUUID(it)?.name }
}
override fun uuidToName(uuid: UUID): String? = profileByUUID(uuid)?.name
override fun log(level: String, formatString: String, vararg data: Any) =
logger.log(Level.toLevel(level, Level.INFO), formatString, *data)

@ -1 +1 @@
Subproject commit 9504df0618d2826aefd98db80445d5d68484b606
Subproject commit bb136c12e3ae50498b3435fc11dbd833efdb6a1c

View File

@ -6,6 +6,7 @@ import matterlink.bridge.command.IBridgeCommand
import matterlink.bridge.command.IMinecraftCommandSender
import matterlink.config.cfg
import matterlink.update.UpdateChecker
import java.util.*
lateinit var instance: IMatterLink
@ -18,10 +19,11 @@ abstract class IMatterLink {
abstract fun wrappedSendToPlayers(msg: String)
abstract fun wrappedSendToPlayer(user: String, msg: String)
abstract fun wrappedSendToPlayer(username: String, msg: String)
abstract fun wrappedSendToPlayer(uuid: UUID, msg: String)
abstract fun isOnline(username: String): Boolean
abstract fun nameToUUID(username: String): String?
abstract fun uuidToName(uuid: String?): String?
abstract fun nameToUUID(username: String): UUID?
abstract fun uuidToName(uuid: UUID): String?
fun start() {
MessageHandlerInst.logger = { level, msg ->

View File

@ -41,27 +41,27 @@ object PasteUtil {
?: "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"))
}
}
}
// .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 {

View File

@ -8,6 +8,7 @@ import blue.endless.jankson.impl.Marshaller
import matterlink.config.cfg
import java.io.PrintWriter
import java.io.StringWriter
import java.lang.Thread.yield
import java.util.*
import kotlin.streams.asSequence
@ -62,10 +63,8 @@ val Exception.stackTraceString: String
return sw.toString()
}
fun randomString(length: Long = 6, source: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ") = Random().ints(length, 0, source.length)
.asSequence()
.map(source::get)
.joinToString("")
fun randomString(length: Int = 6): String =
java.util.UUID.randomUUID().toString().replace("-", "").take(length)
fun <T : Any> JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T {
// instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default")

View File

@ -3,13 +3,14 @@ package matterlink.bridge
import matterlink.*
import matterlink.api.ApiMessage
import matterlink.api.MessageHandler
import matterlink.config.cfg
object MessageHandlerInst : MessageHandler() {
override fun transmit(msg: ApiMessage) {
transmit(msg, cause = "")
}
fun transmit(msg: ApiMessage, cause: String, maxLines: Int = 3) {
fun transmit(msg: ApiMessage, cause: String, maxLines: Int = cfg.outgoing.inlineLimit) {
if (msg.text.count { it == '\n' } >= maxLines) {
try {
val response = PasteUtil.paste(
@ -19,13 +20,13 @@ object MessageHandlerInst : MessageHandler() {
PasteSection(
name = "log.txt",
syntax = "text",
contents = msg.text
contents = msg.text.replace("\n", "\\n")
)
)
)
)
msg.text = msg.text.substringBefore('\n')
.take(15) + "... " + response.link
.take(20) + "... " + response.link
} catch(e: Exception) {
instance.error(e.stackTraceString)
}

View File

@ -5,6 +5,7 @@ import matterlink.config.IdentitiesConfig
import matterlink.config.cfg
import matterlink.instance
import matterlink.randomString
import java.util.*
object AuthBridgeCommand : IBridgeCommand() {
val syntax = "Syntax: auth [username]"
@ -18,9 +19,10 @@ object AuthBridgeCommand : IBridgeCommand() {
return true
}
if (env.uuid != null) {
val name = instance.uuidToName(env.uuid)
env.respond("you are already authenticated as name: $name uuid: ${env.uuid}")
val uuid = env.uuid
if (uuid != null) {
val name = instance.uuidToName(uuid)
env.respond("you are already authenticated as name: $name uuid: $uuid")
return true
}
@ -34,8 +36,8 @@ object AuthBridgeCommand : IBridgeCommand() {
var targetUserName = target
val targetUUid: String = instance.nameToUUID(target) ?: run {
targetUserName = instance.uuidToName(target) ?: run {
val targetUUid: String = instance.nameToUUID(target)?.toString() ?: run {
targetUserName = instance.uuidToName(UUID.fromString(target)) ?: run {
env.respond("cannot find player by username/uuid $target")
return true
}

View File

@ -45,7 +45,7 @@ object BridgeCommandRegistry {
} ?: false
}
fun handleCommand(text: String, username: String, uuid: String): Boolean {
fun handleCommand(text: String, username: String, uuid: UUID): Boolean {
if (!cfg.command.enable || text.isBlank()) return false
if (text[0] != cfg.command.prefix || text.length < 2) return false

View File

@ -3,7 +3,6 @@ package matterlink.bridge.command
import matterlink.instance
import matterlink.lazyFormat
import matterlink.stripColorIn
import matterlink.stripColorOut
data class CustomCommand(
val type: CommandType = CommandType.RESPONSE,
@ -70,8 +69,8 @@ data class CustomCommand(
},
"{uuid}" to {
when (env) {
is CommandEnvironment.BridgeEnv -> env.uuid
is CommandEnvironment.GameEnv -> env.uuid
is CommandEnvironment.BridgeEnv -> env.uuid.toString()
is CommandEnvironment.GameEnv -> env.uuid.toString()
}
},
"{username}" to {

View File

@ -7,6 +7,7 @@ import matterlink.config.cfg
import matterlink.handlers.TickHandler
import matterlink.instance
import matterlink.stripColorOut
import java.util.*
abstract class IBridgeCommand {
abstract val help: String
@ -14,25 +15,26 @@ abstract class IBridgeCommand {
open val timeout: Int = 20
sealed class CommandEnvironment {
abstract val uuid: String?
abstract val uuid: UUID?
abstract val username: String?
data class BridgeEnv(
val name: String,
val userId: String,
val platform: String,
override val uuid: String?
override val uuid: UUID?
) : CommandEnvironment() {
override val username: String?
get() = instance.uuidToName(uuid)
get() = uuid?.let { instance.uuidToName(uuid) }
}
data class GameEnv(
override val username: String,
override val uuid: String
override val uuid: UUID
) : CommandEnvironment()
fun respond(text: String, cause: String = "") {
when(this) {
when (this) {
is BridgeEnv -> {
MessageHandlerInst.transmit(
ApiMessage(
@ -69,7 +71,7 @@ abstract class IBridgeCommand {
*/
abstract fun execute(alias: String, user: String, env: CommandEnvironment, args: String): Boolean
fun canExecute(uuid: String?): Boolean {
fun canExecute(uuid: UUID?): Boolean {
instance.trace("canExecute this: $this uuid: $uuid permLevel: $permLevel")
val canExec = getPermLevel(uuid) >= permLevel
instance.trace("canExecute return $canExec")
@ -79,9 +81,9 @@ abstract class IBridgeCommand {
open fun validate() = true
companion object {
fun getPermLevel(uuid: String?): Double {
if(uuid == null) return cfg.command.defaultPermUnauthenticated
return PermissionConfig.perms[uuid] ?: cfg.command.defaultPermAuthenticated
fun getPermLevel(uuid: UUID?): Double {
if (uuid == null) return cfg.command.defaultPermUnauthenticated
return PermissionConfig.perms[uuid.toString()] ?: cfg.command.defaultPermAuthenticated
}
}
}

View File

@ -70,6 +70,7 @@ data class BaseConfig(val rootDir: File) {
val advancements: Boolean = true,
val stripColors: Boolean = true,
val pasteEEKey: String = "",
val inlineLimit: Int = 5,
val joinPart: JoinPartOptions = JoinPartOptions(),
val death: DeathOptions = DeathOptions()
@ -294,6 +295,11 @@ data class BaseConfig(val rootDir: File) {
pasteEEKey,
"paste.ee api key, leave empty to use application default"
),
inlineLimit = it.getOrDefault(
"inlineLimit",
inlineLimit,
"messages with more lines than this will get shortened via paste.ee"
),
death = it.getOrDefault(
"death",
DeathOptions(),
@ -386,7 +392,7 @@ data class BaseConfig(val rootDir: File) {
configFile.createNewFile()
jankson.marshaller.serialize(MatterLinkConfig()) as JsonObject
}
instance.info("finished loading $jsonObject")
instance.info("finished loading base config")
val tmpCfg = try {
//cfgDirectory.resolve("debug.matterlink.hjson").writeText(jsonObject.toJson(false, true))

View File

@ -11,6 +11,7 @@ import matterlink.getOrDefault
import matterlink.instance
import java.io.File
import java.io.FileNotFoundException
import java.util.*
import java.util.concurrent.TimeUnit
typealias IdentMap = Map<String, Map<String, List<String>>>
@ -114,13 +115,13 @@ object IdentitiesConfig {
}
//TODO: rewrite, store ident map differently in memory
fun getUUID(platform: String, userid: String): String? {
fun getUUID(platform: String, userid: String): UUID? {
return idents.entries.firstOrNull { (uuid, usermap) ->
usermap.entries.any { (_platform, userids) ->
if (platform.equals(_platform, true))
instance.info("platform: $_platform userids: $userids")
platform.equals(_platform, true) && userids.contains(userid)
}
}?.key
}?.key?.let { UUID.fromString(it) }
}
}

View File

@ -9,12 +9,13 @@ import matterlink.getReified
import matterlink.instance
import java.io.File
import java.io.FileNotFoundException
import java.util.*
import java.util.concurrent.TimeUnit
typealias PermissionMap = Map<String, Double>
data class PermissionRequest(
val uuid: String,
val uuid: UUID,
val user: String,
val nonce: String,
val powerlevel: Double? = null
@ -77,8 +78,8 @@ object PermissionConfig {
configFile.writeText(jsonObject.toJson(true, true))
}
fun add(uuid: String, powerlevel: Double, comment: String? = null) {
jsonObject.putDefault(uuid, powerlevel, comment)
fun add(uuid: UUID, powerlevel: Double, comment: String? = null) {
jsonObject.putDefault(uuid.toString(), powerlevel, comment)
load()
}
}

View File

@ -5,12 +5,13 @@ import matterlink.bridge.MessageHandlerInst
import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.instance
import matterlink.stripColorOut
import java.util.*
object ChatProcessor {
/**
* @return cancel message flag
*/
fun sendToBridge(user: String, msg: String, event: String, uuid: String? = null): Boolean {
fun sendToBridge(user: String, msg: String, event: String, uuid: UUID? = null): Boolean {
val message = msg.trim()
if(uuid != null && BridgeCommandRegistry.handleCommand(message, user, uuid)) return true
when {