Use own scope so we can properly clean up #3

Merged
NotAlexNoyle merged 1 commits from main into main 2025-06-06 09:29:15 -04:00
7 changed files with 39 additions and 37 deletions

No files matched your search

@@ -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
}
}
+16 -10
View File
@@ -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()
}
}
}
@@ -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)
}
}
+8 -9
View File
@@ -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)
}
}
@@ -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
@@ -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}<reset>: <red>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) {
@@ -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}<reset>: <red>Failed to set your preferred language. Try again later."))