Refactor & get rid or lateinit

This commit is contained in:
kraftwerk28 2021-07-04 11:21:12 +03:00
parent 7aaaab17d7
commit 09d647c3c2

View File

@ -7,10 +7,9 @@ import java.lang.Exception
import org.kraftwerk28.spigot_tg_bridge.Constants as C import org.kraftwerk28.spigot_tg_bridge.Constants as C
class Plugin : JavaPlugin() { class Plugin : JavaPlugin() {
var tgBot: TgBot? = null var tgBot: TgBot? = null
var eventHandler: EventHandler? = null var eventHandler: EventHandler? = null
lateinit var config: Configuration var config: Configuration? = null
override fun onEnable() { override fun onEnable() {
try { try {
@ -20,41 +19,41 @@ class Plugin : JavaPlugin() {
return return
} }
if (!config.isEnabled) config?.let { config ->
return if (!config.isEnabled) return
val cmdHandler = CommandHandler(this)
val cmdHandler = CommandHandler(this) tgBot?.run { stop() }
tgBot = TgBot(this, config).also { bot ->
tgBot?.run { stop() } eventHandler = EventHandler(bot, config).also {
tgBot = TgBot(this, config).also { bot -> server.pluginManager.registerEvents(it, this)
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() { override fun onDisable() {
if (!config.isEnabled) return config?.let { config ->
config.serverStopMessage?.let { if (!config.isEnabled) return
tgBot?.sendMessageToTelegram(it, blocking = true) 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( fun sendMessageToMinecraft(
text: String, text: String,
username: String? = null, username: String? = null,
chatTitle: String? = null, chatTitle: String? = null,
) = ) = config?.run {
config.minecraftFormat minecraftFormat
.replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeEmoji()) .replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeEmoji())
.run { .run {
username?.let { username?.let {
@ -67,18 +66,21 @@ class Plugin : JavaPlugin() {
} ?: this } ?: this
} }
.also { server.broadcastMessage(it) } .also { server.broadcastMessage(it) }
}
fun reload() { fun reload() {
if (!config.isEnabled) return config?.let { config ->
logger.info(C.INFO.reloading) if (!config.isEnabled) return
config = Configuration(this) logger.info(C.INFO.reloading)
eventHandler?.let { HandlerList.unregisterAll(it) } this.config = Configuration(this)
tgBot?.run { stop() } eventHandler?.let { HandlerList.unregisterAll(it) }
tgBot = TgBot(this, config).also { bot -> tgBot?.run { stop() }
eventHandler = EventHandler(bot, config).also { tgBot = TgBot(this, config).also { bot ->
server.pluginManager.registerEvents(it, this) eventHandler = EventHandler(bot, config).also {
server.pluginManager.registerEvents(it, this)
}
} }
logger.info(C.INFO.reloadComplete)
} }
logger.info(C.INFO.reloadComplete)
} }
} }