From 09d647c3c2be598a474f9b9245e5ca31f385e20c Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Sun, 4 Jul 2021 11:21:12 +0300 Subject: [PATCH] Refactor & get rid or lateinit --- .../kraftwerk28/spigot_tg_bridge/Plugin.kt | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) 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 45b4cd0..b6ccce9 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt @@ -7,10 +7,9 @@ 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 + var config: Configuration? = null override fun onEnable() { try { @@ -20,41 +19,41 @@ class Plugin : JavaPlugin() { return } - if (!config.isEnabled) - return - - val cmdHandler = CommandHandler(this) - - tgBot?.run { stop() } - tgBot = TgBot(this, config).also { bot -> - eventHandler = EventHandler(bot, config).also { - server.pluginManager.registerEvents(it, this) + config?.let { config -> + if (!config.isEnabled) return + val cmdHandler = CommandHandler(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 -> + tgBot?.sendMessageToTelegram(message) } } - getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler) - - config.serverStartMessage?.let { message -> - tgBot?.sendMessageToTelegram(message) - } } override fun onDisable() { - if (!config.isEnabled) return - config.serverStopMessage?.let { - tgBot?.sendMessageToTelegram(it, blocking = true) + config?.let { config -> + if (!config.isEnabled) return + config.serverStopMessage?.let { + tgBot?.sendMessageToTelegram(it, blocking = true) + } + eventHandler?.let { HandlerList.unregisterAll(it) } + tgBot?.run { stop() } + tgBot = null } - eventHandler?.let { HandlerList.unregisterAll(it) } - tgBot?.run { stop() } - tgBot = null } fun sendMessageToMinecraft( text: String, username: String? = null, chatTitle: String? = null, - ) = - config.minecraftFormat + ) = config?.run { + minecraftFormat .replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeEmoji()) .run { username?.let { @@ -67,18 +66,21 @@ class Plugin : JavaPlugin() { } ?: this } .also { server.broadcastMessage(it) } + } fun reload() { - if (!config.isEnabled) return - logger.info(C.INFO.reloading) - config = Configuration(this) - eventHandler?.let { HandlerList.unregisterAll(it) } - tgBot?.run { stop() } - tgBot = TgBot(this, config).also { bot -> - eventHandler = EventHandler(bot, config).also { - server.pluginManager.registerEvents(it, this) + config?.let { 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 -> + eventHandler = EventHandler(bot, config).also { + server.pluginManager.registerEvents(it, this) + } } + logger.info(C.INFO.reloadComplete) } - logger.info(C.INFO.reloadComplete) } }