mirror of
https://github.com/amalthea-mc/spigot-tg-bridge.git
synced 2024-11-23 10:52:26 +00:00
refactor: split EventHandler to class
This commit is contained in:
parent
f293d8ae62
commit
a3207e0b22
@ -12,7 +12,7 @@
|
|||||||
<component name="JavaScriptSettings">
|
<component name="JavaScriptSettings">
|
||||||
<option name="languageLevel" value="ES6" />
|
<option name="languageLevel" value="ES6" />
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -11,11 +11,18 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException
|
|||||||
|
|
||||||
class Bot(private var plugin: Plugin) : TelegramLongPollingBot() {
|
class Bot(private var plugin: Plugin) : TelegramLongPollingBot() {
|
||||||
|
|
||||||
private var allowedChats: List<Long> = plugin.config.getLongList("chats")
|
private val allowedChats: List<Long>
|
||||||
private var chatToMC: Boolean =
|
private val chatToMC: Boolean
|
||||||
plugin.config.getBoolean("logFromTGtoMC", false)
|
private val botToken: String
|
||||||
private var botToken: String = plugin.config.getString("botToken")!!
|
private val botUsername: String
|
||||||
private var botUsername: String = plugin.config.getString("botUsername")!!
|
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
|
override fun getBotToken() = botToken
|
||||||
|
|
||||||
|
@ -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."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,22 @@
|
|||||||
package org.kraftwerk28.spigot_tg_bridge
|
package org.kraftwerk28.spigot_tg_bridge
|
||||||
|
|
||||||
import com.vdurmont.emoji.EmojiParser
|
import com.vdurmont.emoji.EmojiParser
|
||||||
import org.bukkit.event.EventHandler
|
|
||||||
import org.bukkit.event.Listener
|
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.bukkit.plugin.java.JavaPlugin
|
||||||
import org.telegram.telegrambots.ApiContextInitializer
|
import org.telegram.telegrambots.ApiContextInitializer
|
||||||
import org.telegram.telegrambots.meta.TelegramBotsApi
|
import org.telegram.telegrambots.meta.TelegramBotsApi
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class Plugin : JavaPlugin(), Listener {
|
class Plugin : JavaPlugin() {
|
||||||
|
|
||||||
private var tgBot: Bot? = null
|
var tgBot: Bot? = null
|
||||||
private var chatToTG: Boolean = false
|
var chatToTG: Boolean = false
|
||||||
|
|
||||||
|
init {
|
||||||
|
config.run {
|
||||||
|
chatToTG = getBoolean("logFromMCtoTG", false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
val configFile = File(
|
val configFile = File(
|
||||||
@ -30,11 +32,10 @@ class Plugin : JavaPlugin(), Listener {
|
|||||||
ApiContextInitializer.init()
|
ApiContextInitializer.init()
|
||||||
val botsApi = TelegramBotsApi()
|
val botsApi = TelegramBotsApi()
|
||||||
tgBot = Bot(this)
|
tgBot = Bot(this)
|
||||||
chatToTG = config.getBoolean("logFromMCtoTG", false)
|
|
||||||
|
|
||||||
botsApi.registerBot(tgBot)
|
botsApi.registerBot(tgBot)
|
||||||
|
|
||||||
server.pluginManager.registerEvents(this, this)
|
server.pluginManager.registerEvents(EventHandler(this), this)
|
||||||
|
|
||||||
// Notify everything about server start
|
// Notify everything about server start
|
||||||
val startMsg = config.getString("serverStartMessage", null)
|
val startMsg = config.getString("serverStartMessage", null)
|
||||||
@ -57,26 +58,4 @@ class Plugin : JavaPlugin(), Listener {
|
|||||||
val prep = EmojiParser.parseToAliases("<$username> $text")
|
val prep = EmojiParser.parseToAliases("<$username> $text")
|
||||||
server.broadcastMessage(prep)
|
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.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user