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
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)
}
}