Fix updating config.yml after /tgbridge_reload

This commit is contained in:
kraftwerk28 2021-07-06 01:49:07 +03:00
parent 013a6b2f96
commit d6404d3be2
3 changed files with 23 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.Yaml
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
@ -26,7 +27,7 @@ val cfg: Map<String, String> = Yaml()
.load(FileInputStream("$projectDir/src/main/resources/plugin.yml")) .load(FileInputStream("$projectDir/src/main/resources/plugin.yml"))
val pluginVersion = cfg.get("version") val pluginVersion = cfg.get("version")
val spigotApiVersion = cfg.get("api-version") val spigotApiVersion = cfg.get("api-version")
val exposedVersion = "0.31.1" val retrofitVersion = "2.7.1"
version = pluginVersion as Any version = pluginVersion as Any
repositories { repositories {
@ -38,10 +39,6 @@ repositories {
maven(url = "https://oss.sonatype.org/content/repositories/snapshots/") 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 { dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compileOnly("org.spigotmc:spigot-api:$spigotApiVersion-R0.1-SNAPSHOT") compileOnly("org.spigotmc:spigot-api:$spigotApiVersion-R0.1-SNAPSHOT")
@ -62,12 +59,21 @@ tasks {
) )
} }
register<Copy>("copyArtifacts") { register<Copy>("copyArtifacts") {
from("shadowJar") val dest = File(
into(File(homeDir, plugDir)) System.getProperty("user.home"),
"MinecraftServers/spigot_1.17/plugins/",
)
from(shadowJar)
into(dest)
} }
register("pack") { register("pack") {
description = "[For development only!] Build project and copy .jar into servers directory" description = "[For development only!] Build project and copy .jar into servers directory"
dependsOn("shadowJar") dependsOn("shadowJar")
finalizedBy("copyArtifacts") finalizedBy("copyArtifacts")
} }
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
} }

View File

@ -67,10 +67,9 @@ class Plugin : JavaPlugin() {
} }
fun reload() { fun reload() {
config?.let { config -> config = Configuration(this).also { config ->
if (!config.isEnabled) return if (!config.isEnabled) return
logger.info(C.INFO.reloading) logger.info(C.INFO.reloading)
this.config = Configuration(this)
eventHandler?.let { HandlerList.unregisterAll(it) } eventHandler?.let { HandlerList.unregisterAll(it) }
tgBot?.run { stop() } tgBot?.run { stop() }
tgBot = TgBot(this, config).also { bot -> tgBot = TgBot(this, config).also { bot ->

View File

@ -4,7 +4,9 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@ -93,20 +95,15 @@ class TgBot(
} }
} }
} }
updateChan.close()
} }
private fun initHandler() = scope.launch { private fun initHandler() = scope.launch {
loop@ while (true) { updateChan.consumeEach {
try { try {
handleUpdate(updateChan.receive()) handleUpdate(it)
} catch (e: Exception) { } catch (e: Exception) {
when (e) { e.printStackTrace()
is CancellationException -> break@loop
else -> {
e.printStackTrace()
continue@loop
}
}
} }
} }
} }
@ -117,7 +114,8 @@ class TgBot(
return return
update.message?.text?.let { update.message?.text?.let {
commandRegex.matchEntire(it)?.groupValues?.let { commandRegex.matchEntire(it)?.groupValues?.let {
commandMap[it[1]]?.let { it(update) } val (command) = it
commandMap.get(command)?.let { it(update) }
} ?: run { } ?: run {
onTextHandler(update) onTextHandler(update)
} }
@ -127,7 +125,7 @@ class TgBot(
fun stop() { fun stop() {
runBlocking { runBlocking {
pollJob.cancelAndJoin() pollJob.cancelAndJoin()
handlerJob.cancelAndJoin() handlerJob.join()
} }
} }