From 6b6bce4e5ae0efe95132e7af2a1aef8940ea3d5b Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 4 Jun 2025 00:43:08 +0200 Subject: [PATCH] Use own scope so we can properly clean up --- .../nl/skbotnl/chatog/BlocklistManager.kt | 16 ++++++------ src/main/kotlin/nl/skbotnl/chatog/ChatOG.kt | 26 ++++++++++++------- .../nl/skbotnl/chatog/ChatSystemHelper.kt | 5 ++-- src/main/kotlin/nl/skbotnl/chatog/Events.kt | 17 ++++++------ .../nl/skbotnl/chatog/LanguageDatabase.kt | 3 +-- .../chatog/commands/TranslateMessage.kt | 5 ++-- .../chatog/commands/TranslateSettings.kt | 4 +-- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/nl/skbotnl/chatog/BlocklistManager.kt b/src/main/kotlin/nl/skbotnl/chatog/BlocklistManager.kt index 2f4723e..4d811cd 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/BlocklistManager.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/BlocklistManager.kt @@ -1,7 +1,6 @@ package nl.skbotnl.chatog import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.net.URI @@ -18,7 +17,7 @@ object BlocklistManager { ChatOG.plugin.logger.info("Loaded the blocklists!") - GlobalScope.launch { + ChatOG.scope.launch { while (true) { delay(TimeUnit.DAYS.toMillis(1)) refresh() @@ -27,13 +26,14 @@ object BlocklistManager { } private fun refresh() { - URI("https://raw.githubusercontent.com/hagezi/dns-blocklists/main/domains/multi.txt").toURL().openStream().use { input -> - input.bufferedReader().use { bufferedReader -> - bufferedReader.lines().skip(11).forEach { - blockList += it + URI("https://raw.githubusercontent.com/hagezi/dns-blocklists/main/domains/multi.txt").toURL().openStream() + .use { input -> + input.bufferedReader().use { bufferedReader -> + bufferedReader.lines().skip(11).forEach { + blockList += it + } } } - } } private val urlRegex = Regex("^(https?|ftp|file)://([-a-zA-Z0-9+&@#/%?=~_|!:,.;]*?[^/]*)") @@ -47,7 +47,7 @@ object BlocklistManager { } blockList.forEach { - if (it == baseUrl!!) { + if (it == baseUrl) { return true } } diff --git a/src/main/kotlin/nl/skbotnl/chatog/ChatOG.kt b/src/main/kotlin/nl/skbotnl/chatog/ChatOG.kt index 0f4163f..d3dc2c6 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/ChatOG.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/ChatOG.kt @@ -1,9 +1,7 @@ package nl.skbotnl.chatog import com.earth2me.essentials.Essentials -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import net.milkbowl.vault.chat.Chat import nl.skbotnl.chatog.commands.* import org.bukkit.Bukkit @@ -13,6 +11,8 @@ import java.util.* class ChatOG : JavaPlugin() { @OptIn(DelicateCoroutinesApi::class) companion object { + val scope = CoroutineScope(Dispatchers.Default + SupervisorJob()) + lateinit var plugin: JavaPlugin lateinit var chat: Chat var translator: OpenAI? = null @@ -22,31 +22,31 @@ class ChatOG : JavaPlugin() { @JvmStatic @Suppress("unused") fun sendMessageWithBot(message: String) { - GlobalScope.launch { DiscordBridge.sendMessageWithBot(message) } + scope.launch { DiscordBridge.sendMessageWithBot(message) } } @JvmStatic @Suppress("unused") fun sendMessage(message: String, player: String, uuid: UUID?) { - GlobalScope.launch { DiscordBridge.sendMessage(message, player, uuid) } + scope.launch { DiscordBridge.sendMessage(message, player, uuid) } } @JvmStatic @Suppress("unused") fun sendStaffMessage(message: String, player: String, uuid: UUID?) { - GlobalScope.launch { DiscordBridge.sendStaffMessage(message, player, uuid) } + scope.launch { DiscordBridge.sendStaffMessage(message, player, uuid) } } @JvmStatic @Suppress("unused") fun sendPremiumMessage(message: String, player: String, uuid: UUID?) { - GlobalScope.launch { DiscordBridge.sendPremiumMessage(message, player, uuid) } + scope.launch { DiscordBridge.sendPremiumMessage(message, player, uuid) } } @JvmStatic @Suppress("unused") fun sendEmbed(message: String, uuid: UUID?, color: Int) { - GlobalScope.launch { DiscordBridge.sendEmbed(message, uuid, color) } + scope.launch { DiscordBridge.sendEmbed(message, uuid, color) } } } @@ -64,7 +64,7 @@ class ChatOG : JavaPlugin() { LanguageDatabase.init() BlocklistManager.load() - GlobalScope.launch { + scope.launch { EmojiConverter.load() } @@ -80,7 +80,7 @@ class ChatOG : JavaPlugin() { this.getCommand("p")?.setExecutor(PremiumChat()) if (Config.discordEnabled) { - GlobalScope.launch { + scope.launch { DiscordBridge.main() } } @@ -93,5 +93,11 @@ class ChatOG : JavaPlugin() { DiscordBridge.jda!!.shutdownNow() } } + + scope.cancel() + + runBlocking { + scope.coroutineContext[Job]?.join() + } } } diff --git a/src/main/kotlin/nl/skbotnl/chatog/ChatSystemHelper.kt b/src/main/kotlin/nl/skbotnl/chatog/ChatSystemHelper.kt index 3697f38..eda40d9 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/ChatSystemHelper.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/ChatSystemHelper.kt @@ -1,7 +1,6 @@ package nl.skbotnl.chatog import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import me.clip.placeholderapi.PlaceholderAPI import net.kyori.adventure.text.Component @@ -37,7 +36,7 @@ object ChatSystemHelper { if (Config.staffDiscordEnabled) { val discordMessageString = Helper.convertEmojis(text) - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendStaffMessage(discordMessageString, removeColor(chatString), player.uniqueId) } } @@ -86,7 +85,7 @@ object ChatSystemHelper { if (Config.premiumDiscordEnabled) { val discordMessageString = Helper.convertEmojis(text) - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendPremiumMessage(discordMessageString, removeColor(chatString), player.uniqueId) } } diff --git a/src/main/kotlin/nl/skbotnl/chatog/Events.kt b/src/main/kotlin/nl/skbotnl/chatog/Events.kt index 99a9be3..28bcb85 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/Events.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/Events.kt @@ -3,7 +3,6 @@ package nl.skbotnl.chatog import io.papermc.paper.advancement.AdvancementDisplay.Frame.* import io.papermc.paper.event.player.AsyncChatEvent import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import me.clip.placeholderapi.PlaceholderAPI import net.ess3.api.events.VanishStatusChangeEvent @@ -53,7 +52,7 @@ class Events : Listener { ) } - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed( "${removeColor(chatString)} has joined the game. ${ Bukkit.getOnlinePlayers().count() @@ -80,7 +79,7 @@ class Events : Listener { ) } - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed( "${removeColor(chatString)} has left the game. ${ Bukkit.getOnlinePlayers().count() - 1 @@ -109,7 +108,7 @@ class Events : Listener { val reason = PlainTextComponentSerializer.plainText().serialize(event.reason()) - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed( "${removeColor(chatString)} was kicked with reason: \"${reason}\". ${ Bukkit.getOnlinePlayers().count() - 1 @@ -148,7 +147,7 @@ class Events : Listener { } } - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed( "${removeColor(chatString)} $advancementMessage.", event.player.uniqueId, @@ -171,7 +170,7 @@ class Events : Listener { return } - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendMessage(content, "[Server] Broadcast", null) } } @@ -201,7 +200,7 @@ class Events : Listener { val discordMessageString = Helper.convertEmojis(oldTextComponent.content()) - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendMessage(discordMessageString, chatString, event.player.uniqueId) } @@ -379,7 +378,7 @@ class Events : Listener { } if (event.deathMessage() is TextComponent) { - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed( removeColor((event.deathMessage() as TextComponent).content()), event.player.uniqueId, @@ -409,7 +408,7 @@ class Events : Listener { val translatedDeathMessage = PlainTextComponentSerializer.plainText().serialize(deathMessage) - GlobalScope.launch { + ChatOG.scope.launch { DiscordBridge.sendEmbed(removeColor(translatedDeathMessage), event.player.uniqueId, 0xFF0000) } } diff --git a/src/main/kotlin/nl/skbotnl/chatog/LanguageDatabase.kt b/src/main/kotlin/nl/skbotnl/chatog/LanguageDatabase.kt index 03d3321..9cd8a1b 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/LanguageDatabase.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/LanguageDatabase.kt @@ -29,8 +29,7 @@ object LanguageDatabase { try { client.set("chatog:language:$uuid", language) return false - } - catch (e: Exception) { + } catch (e: Exception) { ChatOG.plugin.logger.log(Level.SEVERE, "Exception:", e) } return true diff --git a/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateMessage.kt b/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateMessage.kt index 7288d42..30e1f11 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateMessage.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateMessage.kt @@ -1,7 +1,6 @@ package nl.skbotnl.chatog.commands import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import me.clip.placeholderapi.PlaceholderAPI import net.kyori.adventure.text.Component @@ -64,7 +63,7 @@ class TranslateMessage : CommandExecutor { try { uuid = UUID.fromString(args[0]) - } catch (e: IllegalArgumentException) { + } catch (_: IllegalArgumentException) { player.sendMessage(UtilitiesOG.trueogColorize("${Config.prefix}: That is not a valid UUID.")) return true } @@ -98,7 +97,7 @@ class TranslateMessage : CommandExecutor { return true } - GlobalScope.launch { + ChatOG.scope.launch { val language = LanguageDatabase.getPlayerLanguage(player.uniqueId) if (language == null) { diff --git a/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateSettings.kt b/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateSettings.kt index 1fabbb9..18a062b 100755 --- a/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateSettings.kt +++ b/src/main/kotlin/nl/skbotnl/chatog/commands/TranslateSettings.kt @@ -1,9 +1,9 @@ package nl.skbotnl.chatog.commands import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import net.trueog.utilitiesog.UtilitiesOG +import nl.skbotnl.chatog.ChatOG import nl.skbotnl.chatog.Config import nl.skbotnl.chatog.LanguageDatabase import org.bukkit.command.Command @@ -79,7 +79,7 @@ class TranslateSettings : CommandExecutor { return true } - GlobalScope.launch { + ChatOG.scope.launch { val failed = LanguageDatabase.setPlayerLanguage(sender.uniqueId, language) if (failed) { sender.sendMessage(UtilitiesOG.trueogColorize("${Config.prefix}: Failed to set your preferred language. Try again later.")) -- 2.47.3