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