diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt index 556a40e..a76e804 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt @@ -7,8 +7,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent import org.bukkit.event.player.PlayerBedEnterEvent import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerQuitEvent -import org.bukkit.event.world.WorldLoadEvent -import org.bukkit.event.world.WorldUnloadEvent class EventHandler( private val tgBot: TgBot, 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 228b106..45b4cd0 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt @@ -2,12 +2,14 @@ package org.kraftwerk28.spigot_tg_bridge import com.vdurmont.emoji.EmojiParser import org.bukkit.plugin.java.JavaPlugin +import org.bukkit.event.HandlerList import java.lang.Exception import org.kraftwerk28.spigot_tg_bridge.Constants as C class Plugin : JavaPlugin() { var tgBot: TgBot? = null + var eventHandler: EventHandler? = null lateinit var config: Configuration override fun onEnable() { @@ -22,11 +24,14 @@ class Plugin : JavaPlugin() { return val cmdHandler = CommandHandler(this) - loadBot() - tgBot?.let { bot -> - val eventHandler = EventHandler(bot, config) - server.pluginManager.registerEvents(eventHandler, this) + + tgBot?.run { stop() } + tgBot = TgBot(this, config).also { bot -> + eventHandler = EventHandler(bot, config).also { + server.pluginManager.registerEvents(it, this) + } } + getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler) config.serverStartMessage?.let { message -> @@ -34,17 +39,14 @@ class Plugin : JavaPlugin() { } } - fun loadBot() { - tgBot?.run { stop() } - tgBot = TgBot(this, config) - } - override fun onDisable() { if (!config.isEnabled) return config.serverStopMessage?.let { tgBot?.sendMessageToTelegram(it, blocking = true) } + eventHandler?.let { HandlerList.unregisterAll(it) } tgBot?.run { stop() } + tgBot = null } fun sendMessageToMinecraft( @@ -67,9 +69,16 @@ class Plugin : JavaPlugin() { .also { server.broadcastMessage(it) } fun reload() { + if (!config.isEnabled) return logger.info(C.INFO.reloading) config = Configuration(this) - loadBot() + eventHandler?.let { HandlerList.unregisterAll(it) } + tgBot?.run { stop() } + tgBot = TgBot(this, config).also { bot -> + eventHandler = EventHandler(bot, config).also { + server.pluginManager.registerEvents(it, this) + } + } logger.info(C.INFO.reloadComplete) } } 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 0b8c73b..427ec86 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -121,10 +121,6 @@ class TgBot( } fun stop() { - client.run { - dispatcher.executorService.shutdown() - connectionPool.evictAll() - } runBlocking { pollJob.cancelAndJoin() handlerJob.cancelAndJoin()