diff --git a/.idea/misc.xml b/.idea/misc.xml
index 53525f9..3520b9e 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -12,7 +12,7 @@
-
+
\ No newline at end of file
diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt
index fa9f734..6609d45 100644
--- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt
+++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt
@@ -11,11 +11,18 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException
class Bot(private var plugin: Plugin) : TelegramLongPollingBot() {
- private var allowedChats: List = plugin.config.getLongList("chats")
- private var chatToMC: Boolean =
- plugin.config.getBoolean("logFromTGtoMC", false)
- private var botToken: String = plugin.config.getString("botToken")!!
- private var botUsername: String = plugin.config.getString("botUsername")!!
+ private val allowedChats: List
+ private val chatToMC: Boolean
+ private val botToken: String
+ private val botUsername: String
+ init {
+ plugin.config.run {
+ allowedChats = getLongList("chats")
+ chatToMC = getBoolean("logFromTGtoMC", false)
+ botToken = getString("botToken") ?: throw Exception("Bot token must be defined.")
+ botUsername = getString("botUsername") ?: throw Exception("Bot username must be defined.")
+ }
+ }
override fun getBotToken() = botToken
diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt
new file mode 100644
index 0000000..0cd0184
--- /dev/null
+++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt
@@ -0,0 +1,47 @@
+package org.kraftwerk28.spigot_tg_bridge
+
+import org.bukkit.event.EventHandler
+import org.bukkit.event.Listener
+import org.bukkit.event.player.AsyncPlayerChatEvent
+import org.bukkit.event.player.PlayerJoinEvent
+import org.bukkit.event.player.PlayerQuitEvent
+
+class EventHandler(private val plugin: Plugin) : Listener {
+
+ private val joinStr: String
+ private val leftStr: String
+ private val logJoinLeave: Boolean
+
+ init {
+ plugin.config.run {
+ joinStr = getString("strings.joined", "joined")!!
+ leftStr = getString("strings.left", "left")!!
+ logJoinLeave = getBoolean("logJoinLeave", false)
+ }
+ }
+
+ @EventHandler
+ fun onPlayerChat(event: AsyncPlayerChatEvent) {
+ if (plugin.chatToTG) {
+ plugin.tgBot?.sendMessageToTGFrom(
+ event.player.displayName, event.message
+ )
+ }
+ }
+
+ @EventHandler
+ fun onPlayerJoin(event: PlayerJoinEvent) {
+ if (!logJoinLeave) return
+ plugin.tgBot?.broadcastToTG(
+ "${event.player.displayName} $joinStr."
+ )
+ }
+
+ @EventHandler
+ fun onPlayerLeave(event: PlayerQuitEvent) {
+ if (!logJoinLeave) return
+ plugin.tgBot?.broadcastToTG(
+ "${event.player.displayName} b $leftStr."
+ )
+ }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt
index 259d6bd..199489d 100644
--- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt
+++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt
@@ -1,20 +1,22 @@
package org.kraftwerk28.spigot_tg_bridge
import com.vdurmont.emoji.EmojiParser
-import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
-import org.bukkit.event.player.AsyncPlayerChatEvent
-import org.bukkit.event.player.PlayerJoinEvent
-import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.java.JavaPlugin
import org.telegram.telegrambots.ApiContextInitializer
import org.telegram.telegrambots.meta.TelegramBotsApi
import java.io.File
-class Plugin : JavaPlugin(), Listener {
+class Plugin : JavaPlugin() {
- private var tgBot: Bot? = null
- private var chatToTG: Boolean = false
+ var tgBot: Bot? = null
+ var chatToTG: Boolean = false
+
+ init {
+ config.run {
+ chatToTG = getBoolean("logFromMCtoTG", false)
+ }
+ }
override fun onEnable() {
val configFile = File(
@@ -30,11 +32,10 @@ class Plugin : JavaPlugin(), Listener {
ApiContextInitializer.init()
val botsApi = TelegramBotsApi()
tgBot = Bot(this)
- chatToTG = config.getBoolean("logFromMCtoTG", false)
botsApi.registerBot(tgBot)
- server.pluginManager.registerEvents(this, this)
+ server.pluginManager.registerEvents(EventHandler(this), this)
// Notify everything about server start
val startMsg = config.getString("serverStartMessage", null)
@@ -57,26 +58,4 @@ class Plugin : JavaPlugin(), Listener {
val prep = EmojiParser.parseToAliases("<$username> $text")
server.broadcastMessage(prep)
}
-
- @EventHandler
- fun onPlayerChat(event: AsyncPlayerChatEvent) {
- if (chatToTG)
- tgBot?.sendMessageToTGFrom(event.player.displayName, event.message)
- }
-
- @EventHandler
- fun onPlayerJoin(event: PlayerJoinEvent) {
- if (config.getBoolean("logJoinLeave", false)) {
- val joinStr = config.getString("strings.joined", "joined")
- tgBot?.broadcastToTG("${event.player.displayName} $joinStr.")
- }
- }
-
- @EventHandler
- fun onPlayerLeave(event: PlayerQuitEvent) {
- if (config.getBoolean("logJoinLeave", false)) {
- val leftStr = config.getString("strings.left", "joined")
- tgBot?.broadcastToTG("${event.player.displayName} $leftStr.")
- }
- }
}