refactor: split EventHandler to class

This commit is contained in:
kraftwerk28 2020-07-11 12:49:35 +03:00
parent f293d8ae62
commit a3207e0b22
4 changed files with 70 additions and 37 deletions

View File

@ -12,7 +12,7 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" project-jdk-name="13" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" project-jdk-name="14" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -11,11 +11,18 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException
class Bot(private var plugin: Plugin) : TelegramLongPollingBot() {
private var allowedChats: List<Long> = 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<Long>
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

View File

@ -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(
"<b>${event.player.displayName}</b> $joinStr."
)
}
@EventHandler
fun onPlayerLeave(event: PlayerQuitEvent) {
if (!logJoinLeave) return
plugin.tgBot?.broadcastToTG(
"<b>${event.player.displayName}</b> b $leftStr."
)
}
}

View File

@ -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("<b>${event.player.displayName}</b> $joinStr.")
}
}
@EventHandler
fun onPlayerLeave(event: PlayerQuitEvent) {
if (config.getBoolean("logJoinLeave", false)) {
val leftStr = config.getString("strings.left", "joined")
tgBot?.broadcastToTG("<b>${event.player.displayName}</b> $leftStr.")
}
}
}