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,26 +19,25 @@ 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?.run { stop() }
tgBot = TgBot(this, config).also { bot -> tgBot = TgBot(this, config).also { bot ->
eventHandler = EventHandler(bot, config).also { eventHandler = EventHandler(bot, config).also {
server.pluginManager.registerEvents(it, this) server.pluginManager.registerEvents(it, this)
} }
} }
getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler) getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler)
config.serverStartMessage?.let { message -> config.serverStartMessage?.let { message ->
tgBot?.sendMessageToTelegram(message) tgBot?.sendMessageToTelegram(message)
} }
} }
}
override fun onDisable() { override fun onDisable() {
config?.let { config ->
if (!config.isEnabled) return if (!config.isEnabled) return
config.serverStopMessage?.let { config.serverStopMessage?.let {
tgBot?.sendMessageToTelegram(it, blocking = true) tgBot?.sendMessageToTelegram(it, blocking = true)
@ -48,13 +46,14 @@ class Plugin : JavaPlugin() {
tgBot?.run { stop() } tgBot?.run { stop() }
tgBot = null 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,11 +66,13 @@ class Plugin : JavaPlugin() {
} ?: this } ?: this
} }
.also { server.broadcastMessage(it) } .also { server.broadcastMessage(it) }
}
fun reload() { fun reload() {
config?.let { config ->
if (!config.isEnabled) return if (!config.isEnabled) return
logger.info(C.INFO.reloading) logger.info(C.INFO.reloading)
config = Configuration(this) this.config = Configuration(this)
eventHandler?.let { HandlerList.unregisterAll(it) } eventHandler?.let { HandlerList.unregisterAll(it) }
tgBot?.run { stop() } tgBot?.run { stop() }
tgBot = TgBot(this, config).also { bot -> tgBot = TgBot(this, config).also { bot ->
@ -82,3 +83,4 @@ class Plugin : JavaPlugin() {
logger.info(C.INFO.reloadComplete) logger.info(C.INFO.reloadComplete)
} }
} }
}