From d4802d372fa3065814743928bfc5beb62db8caed Mon Sep 17 00:00:00 2001 From: NikkyAI Date: Fri, 23 Nov 2018 04:00:38 +0100 Subject: [PATCH] fix idiotic errors in config --- 1.7.10/build.gradle | 16 +- core/build.gradle | 16 +- core/src/main/kotlin/matterlink/api/Config.kt | 7 +- .../kotlin/matterlink/api/MessageHandler.kt | 60 ++++--- .../kotlin/matterlink/api/StreamConnection.kt | 155 ------------------ .../kotlin/matterlink/config/BaseConfig.kt | 31 ++-- .../matterlink/handlers/ChatProcessor.kt | 3 +- .../matterlink/handlers/LocationHandler.kt | 3 + .../matterlink/handlers/ServerChatHandler.kt | 2 + gradle.properties | 6 +- 10 files changed, 84 insertions(+), 215 deletions(-) delete mode 100644 core/src/main/kotlin/matterlink/api/StreamConnection.kt diff --git a/1.7.10/build.gradle b/1.7.10/build.gradle index 0758491..f65652a 100644 --- a/1.7.10/build.gradle +++ b/1.7.10/build.gradle @@ -34,15 +34,15 @@ dependencies { shade (project(':Jankson')) { transitive = false } shade group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: kotlinVersion - shade(group: "com.github.NikkyAi.Fuel", name: "fuel", version: "feature~chunked-SNAPSHOT") - shade(group: "com.github.NikkyAi.Fuel", name: "fuel-coroutines", version: "feature~chunked-SNAPSHOT") + + shade(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: coroutinesVersion) + shade(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: serializationVersion) + + shade(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) + shade(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) shade(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) - - shade(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: "1.0.0") - shade(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: "0.9.0") - - shade group: 'com.github.kittinunf.fuel', name: 'fuel', version: fuelVersion - shade group: 'com.github.kittinunf.result', name: 'result', version: resultVersion +// shade group: 'com.github.kittinunf.fuel', name: 'fuel', version: fuelVersion +// shade group: 'com.github.kittinunf.result', name: 'result', version: resultVersion } diff --git a/core/build.gradle b/core/build.gradle index f858d15..d0ecf9e 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -23,18 +23,18 @@ dependencies { compile group: 'com.google.code.gson', name: 'gson', version: '+' compile group: 'com.google.guava', name: 'guava', version: '+' - compile(group: "com.github.NikkyAi.Fuel", name: "fuel", version: "742c73082e") - shadow(group: "com.github.NikkyAi.Fuel", name: "fuel", version: "742c73082e") { transitive = false } - compile(group: "com.github.NikkyAi.Fuel", name: "fuel-coroutines", version: "742c73082e") - shadow(group: "com.github.NikkyAi.Fuel", name: "fuel-coroutines", version: "742c73082e") { transitive = false } + compile(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) + shadow(group: "com.github.kittinunf.Fuel", name: "fuel", version: fuelVersion) { transitive = false } + compile(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) + shadow(group: "com.github.kittinunf.Fuel", name: "fuel-coroutines", version: fuelVersion) { transitive = false } compile(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) shadow(group: 'com.github.kittinunf.result', name: 'result', version: resultVersion) { transitive = false } - compile(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: "1.0.1") - shadow(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: "1.0.1") { transitive = false } + compile(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: coroutinesVersion) + shadow(group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-core", version: coroutinesVersion) { transitive = false } - compile(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: "0.9.0") - shadow(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: "0.9.0") { transitive = false } + compile(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: serializationVersion) + shadow(group: "org.jetbrains.kotlinx", name: "kotlinx-serialization-runtime", version: serializationVersion) { transitive = false } compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: kotlinVersion } diff --git a/core/src/main/kotlin/matterlink/api/Config.kt b/core/src/main/kotlin/matterlink/api/Config.kt index 0226746..832e2de 100644 --- a/core/src/main/kotlin/matterlink/api/Config.kt +++ b/core/src/main/kotlin/matterlink/api/Config.kt @@ -7,9 +7,4 @@ data class Config( var announceDisconnect: Boolean = true, var reconnectWait: Long = 500, var systemUser: String = "Server" -) { - fun sync(connection: StreamConnection) { - connection.token = token - connection.host = url - } -} \ No newline at end of file +) \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/api/MessageHandler.kt b/core/src/main/kotlin/matterlink/api/MessageHandler.kt index d66af35..2eed241 100644 --- a/core/src/main/kotlin/matterlink/api/MessageHandler.kt +++ b/core/src/main/kotlin/matterlink/api/MessageHandler.kt @@ -12,7 +12,9 @@ import com.github.kittinunf.result.Result import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job +import kotlinx.coroutines.ObsoleteCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.SendChannel @@ -27,6 +29,7 @@ import kotlinx.serialization.json.JSON import kotlinx.serialization.list import matterlink.Logger import java.io.Reader +import java.net.ConnectException import kotlin.coroutines.CoroutineContext /** @@ -46,6 +49,7 @@ open class MessageHandler : CoroutineScope { private var sendChannel: SendChannel = senderActor() private val messageStream = Channel(Channel.UNLIMITED) + @UseExperimental(ExperimentalCoroutinesApi::class) var broadcast: BroadcastChannel = broadcast { while (true) { val msg = messageStream.receive() @@ -135,7 +139,7 @@ open class MessageHandler : CoroutineScope { logger.error("missing gateway on message: $msg") return } - logger.debug("Transmitting: $msg") + logger.info("Transmitting: $msg") sendChannel.send(msg) // } } @@ -144,34 +148,40 @@ open class MessageHandler : CoroutineScope { fun checkConnection() { } + @UseExperimental(ObsoleteCoroutinesApi::class) private fun CoroutineScope.senderActor() = actor(context = Dispatchers.IO) { consumeEach { - logger.debug("sending $it") - val url = "${config.url}/api/message" - val (request, response, result) = url.httpPost() - .apply { - if (config.token.isNotEmpty()) { - headers["Authorization"] = "Bearer ${config.token}" + try { + logger.debug("sending $it") + val url = "${config.url}/api/message" + val (request, response, result) = url.httpPost() + .apply { + if (config.token.isNotEmpty()) { + headers["Authorization"] = "Bearer ${config.token}" + } + } + .jsonBody(it.encode()) + .responseString() + when (result) { + is Result.Success -> { + logger.info("sent $it") + sendErrors = 0 + } + is Result.Failure -> { + sendErrors++ + logger.error("failed to deliver: $it") + logger.error("url: $url") + logger.error("cUrl: ${request.cUrlString()}") + logger.error("response: $response") + logger.error(result.error.exception.localizedMessage) + result.error.exception.printStackTrace() +// close() + throw result.error.exception } } - .jsonBody(it.encode()) - .responseString() - when (result) { - is Result.Success -> { - logger.info("sent $it") - sendErrors = 0 - } - is Result.Failure -> { - sendErrors++ - logger.error("failed to deliver: $it") - logger.error("url: $url") - logger.error("cUrl: ${request.cUrlString()}") - logger.error("response: $response") - logger.error(result.error.exception.localizedMessage) - result.error.exception.printStackTrace() -// close() - throw result.error.exception - } + } catch (connectError: ConnectException) { + connectError.printStackTrace() + sendErrors++ } } } diff --git a/core/src/main/kotlin/matterlink/api/StreamConnection.kt b/core/src/main/kotlin/matterlink/api/StreamConnection.kt deleted file mode 100644 index 9eb08de..0000000 --- a/core/src/main/kotlin/matterlink/api/StreamConnection.kt +++ /dev/null @@ -1,155 +0,0 @@ -package matterlink.api - -import matterlink.Logger -import java.io.IOException -import java.net.ConnectException -import java.net.HttpURLConnection -import java.net.MalformedURLException -import java.net.URL -import java.util.Arrays -import java.util.LinkedList -import java.util.concurrent.ConcurrentLinkedQueue - -/** - * Created by nikky on 07/05/18. - * - * @author Nikky - * @version 1.0 - */ -class StreamConnection(private val rcvQueue: ConcurrentLinkedQueue) : Runnable { - private var thread: Thread = createThread() - private var urlConnection: HttpURLConnection? = null - private val onSuccessCallbacks = LinkedList<(Boolean) -> Unit>() - - var logger = object : Logger { - override fun info(message: String) = println("INFO: $message") - override fun debug(message: String) = println("DEBUG: $message") - override fun error(message: String) = println("ERROR: $message") - override fun warn(message: String) = println("WARN: $message") - override fun trace(message: String) = println("TRACE: $message") - } - var host = "" - var token = "" - - var isConnected = false - private set - var isConnecting = false - private set - var isCancelled = false - private set - - private fun createThread(): Thread { - val thread = Thread(this) - thread.name = "RcvThread" - return thread - } - - fun addOnSuccess(callback: (Boolean) -> Unit) { - onSuccessCallbacks.add(callback) - } - - fun removeOnSuccess(callback: (Boolean) -> Unit) { - onSuccessCallbacks.remove(callback) - } - - private fun onSuccess(success: Boolean) { - isConnecting = false - isConnected = success - for (callback in onSuccessCallbacks) { - callback(success) - } - } - - override fun run() { - try { - val serviceURL = "$host/api/stream" - val myURL: URL - - myURL = URL(serviceURL) - urlConnection = myURL.openConnection() as HttpURLConnection - urlConnection!!.requestMethod = "GET" - if (!token.isEmpty()) { - val bearerAuth = "Bearer $token" - urlConnection!!.setRequestProperty("Authorization", bearerAuth) - } - try { - urlConnection!!.inputStream.use { input -> - logger.info("connection opened") - onSuccess(true) - // BufferedInputStream bufferedInput = new BufferedInputStream(input, 8 * 1024); - val buffer = StringBuilder() - while (!isCancelled) { - val buf = ByteArray(1024) - Thread.sleep(10) - while (input.available() <= 0) { - if (isCancelled) break - Thread.sleep(10) - } - val chars = input.read(buf) - - logger.trace(String.format("read %d chars", chars)) - if (chars > 0) { - val added = String(Arrays.copyOfRange(buf, 0, chars)) - logger.debug("json: $added") - buffer.append(added) - while (buffer.toString().contains("\n")) { - val index = buffer.indexOf("\n") - val line = buffer.substring(0, index) - buffer.delete(0, index + 1) - rcvQueue.add(ApiMessage.decode(line)) - } - } else if (chars < 0) { - break - } - } - } - } finally { - onClose() - } - } catch (e: MalformedURLException) { - e.printStackTrace() - } catch (e: ConnectException) { - e.printStackTrace() - onSuccess(false) - } catch (e: IOException) { - e.printStackTrace() - onSuccess(false) - } catch (e: InterruptedException) { - e.printStackTrace() - } - - } - - private fun onClose() { - logger.info("Bridge connection closed!") - isConnected = false - isConnecting = false - } - - fun open() { - if (!thread.isAlive) { - thread = createThread() - isConnecting = true - isCancelled = false - thread.start() - logger.info("Starting Connection") - } - if (thread.isAlive) { - logger.info("Bridge is connecting") - } - } - - fun close() { - try { - isCancelled = true - if (urlConnection != null) { - urlConnection!!.disconnect() - } - thread.join() - logger.info("Thread stopped") - } catch (e: InterruptedException) { - e.printStackTrace() - } - - } -} diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 19d4877..a358651 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -194,8 +194,7 @@ data class BaseConfig(val rootDir: File) { data class AvatarOptions( val enable: Boolean = true, val urlTemplate: String = "https://visage.surgeplay.com/head/512/{uuid}", - // https://www.freepik.com/free-icon/right-arrow-angle-and-horizontal-down-line-code-signs_732795.htm - val systemUserAvatar: String = "https://image.freepik.com/free-icon/right-arrow-angle-and-horizontal-down-line-code-signs_318-53994.jpg" + val systemUserAvatar: String = "" ) data class JoinPartOptions( @@ -625,20 +624,32 @@ data class BaseConfig(val rootDir: File) { } .registerSerializer { locationSettings: SettingsOutgoing, marshaller: Marshaller -> val jsonObject = JsonObject() + locationSettings.plain?.let { + jsonObject["plain"] = marshaller.serialize(it) + } + locationSettings.action?.let { + jsonObject["action"] = marshaller.serialize(it) + } + locationSettings.join?.let { + jsonObject["join"] = marshaller.serialize(it) + } + locationSettings.leave?.let { + jsonObject["leave"] = marshaller.serialize(it) + } locationSettings.advancement?.let { - jsonObject["advancements"] = marshaller.serialize(it) + jsonObject["advancement"] = marshaller.serialize(it) } locationSettings.death?.let { jsonObject["death"] = marshaller.serialize(it) } - locationSettings.join?.let { - jsonObject["joins"] = marshaller.serialize(it) - } - locationSettings.leave?.let { - jsonObject["leaves"] = marshaller.serialize(it) - } locationSettings.broadcast?.let { - jsonObject["say"] = marshaller.serialize(it) + jsonObject["broadcast"] = marshaller.serialize(it) + } + locationSettings.status?.let { + jsonObject["status"] = marshaller.serialize(it) + } + locationSettings.skip.let { + jsonObject["skip"] = marshaller.serialize(it) } jsonObject } diff --git a/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt b/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt index afaab53..981af6e 100644 --- a/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt +++ b/core/src/main/kotlin/matterlink/handlers/ChatProcessor.kt @@ -28,7 +28,8 @@ object ChatProcessor { msg = message, x = x, y = y, z = z, dimension = dimension, event = event, - cause = "Message from $user" + cause = "Message from $user", + uuid = uuid ) diff --git a/core/src/main/kotlin/matterlink/handlers/LocationHandler.kt b/core/src/main/kotlin/matterlink/handlers/LocationHandler.kt index 9359a0b..e8b2488 100644 --- a/core/src/main/kotlin/matterlink/handlers/LocationHandler.kt +++ b/core/src/main/kotlin/matterlink/handlers/LocationHandler.kt @@ -55,6 +55,9 @@ object LocationHandler { if (!matchesEvent) { logger.info("location: $label dropped message '$msg' from user: '$user', event not enabled") + logger.info("event: $event") + logger.info("location.outgoing: ${location.outgoing}") + logger.info("defaults: $defaults") continue } diff --git a/core/src/main/kotlin/matterlink/handlers/ServerChatHandler.kt b/core/src/main/kotlin/matterlink/handlers/ServerChatHandler.kt index 5862a9f..a1a78b1 100644 --- a/core/src/main/kotlin/matterlink/handlers/ServerChatHandler.kt +++ b/core/src/main/kotlin/matterlink/handlers/ServerChatHandler.kt @@ -1,5 +1,6 @@ package matterlink.handlers +import kotlinx.coroutines.ExperimentalCoroutinesApi import matterlink.api.ApiMessage import matterlink.bridge.MessageHandlerInst import matterlink.bridge.command.BridgeCommandRegistry @@ -10,6 +11,7 @@ import matterlink.logger import java.util.UUID object ServerChatHandler { + @UseExperimental(ExperimentalCoroutinesApi::class) val rcvChannel = MessageHandlerInst.broadcast.openSubscription() /** diff --git a/gradle.properties b/gradle.properties index 9c0e860..f213d4e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,9 +2,11 @@ modName = MatterLink modVersion = 1.6.4 forgelinVersion = 1.8.0 kotlinVersion = 1.3.0 +coroutinesVersion = 1.0.1 +serializationVersion = 0.9.1 shadowVersion = 2.0.2 -fuelVersion = 1.16.0 -resultVersion = 1.6.0 +fuelVersion = b5d2b1fd05 +resultVersion = 2.0.0 cursegradleVersion = 1.1.0 curseId = 287323 curseReleaseType = beta \ No newline at end of file