From d6404d3be2cf4f27cc5b5fb3d4b3be5950a78948 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Tue, 6 Jul 2021 01:49:07 +0300 Subject: [PATCH] Fix updating config.yml after `/tgbridge_reload` --- build.gradle.kts | 20 ++++++++++++------- .../kraftwerk28/spigot_tg_bridge/Plugin.kt | 3 +-- .../org/kraftwerk28/spigot_tg_bridge/TgBot.kt | 20 +++++++++---------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a80e99b..316aefd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,5 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.yaml.snakeyaml.Yaml import java.io.File import java.io.FileInputStream @@ -26,7 +27,7 @@ val cfg: Map = Yaml() .load(FileInputStream("$projectDir/src/main/resources/plugin.yml")) val pluginVersion = cfg.get("version") val spigotApiVersion = cfg.get("api-version") -val exposedVersion = "0.31.1" +val retrofitVersion = "2.7.1" version = pluginVersion as Any repositories { @@ -38,10 +39,6 @@ repositories { maven(url = "https://oss.sonatype.org/content/repositories/snapshots/") } -val retrofitVersion = "2.7.1" -val plugDir = "MinecraftServers/spigot_1.17/plugins/" -val homeDir = System.getProperty("user.home") - dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") compileOnly("org.spigotmc:spigot-api:$spigotApiVersion-R0.1-SNAPSHOT") @@ -62,12 +59,21 @@ tasks { ) } register("copyArtifacts") { - from("shadowJar") - into(File(homeDir, plugDir)) + val dest = File( + System.getProperty("user.home"), + "MinecraftServers/spigot_1.17/plugins/", + ) + from(shadowJar) + into(dest) } register("pack") { description = "[For development only!] Build project and copy .jar into servers directory" dependsOn("shadowJar") finalizedBy("copyArtifacts") } + withType { + kotlinOptions { + jvmTarget = "1.8" + } + } } diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt index 7e16833..d9aef46 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt @@ -67,10 +67,9 @@ class Plugin : JavaPlugin() { } fun reload() { - config?.let { config -> + config = Configuration(this).also { config -> if (!config.isEnabled) return logger.info(C.INFO.reloading) - this.config = Configuration(this) eventHandler?.let { HandlerList.unregisterAll(it) } tgBot?.run { stop() } tgBot = TgBot(this, config).also { bot -> diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt index 66951cc..deadae1 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -4,7 +4,9 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.consumeEach import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import okhttp3.OkHttpClient @@ -93,20 +95,15 @@ class TgBot( } } } + updateChan.close() } private fun initHandler() = scope.launch { - loop@ while (true) { + updateChan.consumeEach { try { - handleUpdate(updateChan.receive()) + handleUpdate(it) } catch (e: Exception) { - when (e) { - is CancellationException -> break@loop - else -> { - e.printStackTrace() - continue@loop - } - } + e.printStackTrace() } } } @@ -117,7 +114,8 @@ class TgBot( return update.message?.text?.let { commandRegex.matchEntire(it)?.groupValues?.let { - commandMap[it[1]]?.let { it(update) } + val (command) = it + commandMap.get(command)?.let { it(update) } } ?: run { onTextHandler(update) } @@ -127,7 +125,7 @@ class TgBot( fun stop() { runBlocking { pollJob.cancelAndJoin() - handlerJob.cancelAndJoin() + handlerJob.join() } }