Skip to content

Commit 09d647c

Browse files
committed
Refactor & get rid or lateinit
1 parent 7aaaab1 commit 09d647c

File tree

1 file changed

+35
-33
lines changed
  • src/main/kotlin/org/kraftwerk28/spigot_tg_bridge

1 file changed

+35
-33
lines changed

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt

+35-33
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import java.lang.Exception
77
import org.kraftwerk28.spigot_tg_bridge.Constants as C
88

99
class Plugin : JavaPlugin() {
10-
1110
var tgBot: TgBot? = null
1211
var eventHandler: EventHandler? = null
13-
lateinit var config: Configuration
12+
var config: Configuration? = null
1413

1514
override fun onEnable() {
1615
try {
@@ -20,41 +19,41 @@ class Plugin : JavaPlugin() {
2019
return
2120
}
2221

23-
if (!config.isEnabled)
24-
return
25-
26-
val cmdHandler = CommandHandler(this)
27-
28-
tgBot?.run { stop() }
29-
tgBot = TgBot(this, config).also { bot ->
30-
eventHandler = EventHandler(bot, config).also {
31-
server.pluginManager.registerEvents(it, this)
22+
config?.let { config ->
23+
if (!config.isEnabled) return
24+
val cmdHandler = CommandHandler(this)
25+
tgBot?.run { stop() }
26+
tgBot = TgBot(this, config).also { bot ->
27+
eventHandler = EventHandler(bot, config).also {
28+
server.pluginManager.registerEvents(it, this)
29+
}
30+
}
31+
getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler)
32+
config.serverStartMessage?.let { message ->
33+
tgBot?.sendMessageToTelegram(message)
3234
}
3335
}
3436

35-
getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler)
36-
37-
config.serverStartMessage?.let { message ->
38-
tgBot?.sendMessageToTelegram(message)
39-
}
4037
}
4138

4239
override fun onDisable() {
43-
if (!config.isEnabled) return
44-
config.serverStopMessage?.let {
45-
tgBot?.sendMessageToTelegram(it, blocking = true)
40+
config?.let { config ->
41+
if (!config.isEnabled) return
42+
config.serverStopMessage?.let {
43+
tgBot?.sendMessageToTelegram(it, blocking = true)
44+
}
45+
eventHandler?.let { HandlerList.unregisterAll(it) }
46+
tgBot?.run { stop() }
47+
tgBot = null
4648
}
47-
eventHandler?.let { HandlerList.unregisterAll(it) }
48-
tgBot?.run { stop() }
49-
tgBot = null
5049
}
5150

5251
fun sendMessageToMinecraft(
5352
text: String,
5453
username: String? = null,
5554
chatTitle: String? = null,
56-
) =
57-
config.minecraftFormat
55+
) = config?.run {
56+
minecraftFormat
5857
.replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeEmoji())
5958
.run {
6059
username?.let {
@@ -67,18 +66,21 @@ class Plugin : JavaPlugin() {
6766
} ?: this
6867
}
6968
.also { server.broadcastMessage(it) }
69+
}
7070

7171
fun reload() {
72-
if (!config.isEnabled) return
73-
logger.info(C.INFO.reloading)
74-
config = Configuration(this)
75-
eventHandler?.let { HandlerList.unregisterAll(it) }
76-
tgBot?.run { stop() }
77-
tgBot = TgBot(this, config).also { bot ->
78-
eventHandler = EventHandler(bot, config).also {
79-
server.pluginManager.registerEvents(it, this)
72+
config?.let { config ->
73+
if (!config.isEnabled) return
74+
logger.info(C.INFO.reloading)
75+
this.config = Configuration(this)
76+
eventHandler?.let { HandlerList.unregisterAll(it) }
77+
tgBot?.run { stop() }
78+
tgBot = TgBot(this, config).also { bot ->
79+
eventHandler = EventHandler(bot, config).also {
80+
server.pluginManager.registerEvents(it, this)
81+
}
8082
}
83+
logger.info(C.INFO.reloadComplete)
8184
}
82-
logger.info(C.INFO.reloadComplete)
8385
}
8486
}

0 commit comments

Comments
 (0)