From 1b645fb76c9d4c5c70d33e6060ae0e61fa384fe9 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Sun, 9 Feb 2020 19:18:51 +0200 Subject: [PATCH] release: version 0.0.2 add: more config options chore: bug fixes --- .idea/artifacts/rzcraft_bridge_jar.xml | 106 +++++++++--------- .idea/misc.xml | 5 + build.gradle | 1 + src/config.yml | 12 +- .../org/kraftwerk28/rzcraft_bridge/Bot.kt | 59 ++++++---- .../rzcraft_bridge/MessageListener.kt | 19 ---- .../org/kraftwerk28/rzcraft_bridge/Plugin.kt | 50 ++++++++- src/plugin.yml | 2 +- 8 files changed, 157 insertions(+), 97 deletions(-) delete mode 100644 src/main/kotlin/org/kraftwerk28/rzcraft_bridge/MessageListener.kt diff --git a/.idea/artifacts/rzcraft_bridge_jar.xml b/.idea/artifacts/rzcraft_bridge_jar.xml index 87b1e27..442f2da 100644 --- a/.idea/artifacts/rzcraft_bridge_jar.xml +++ b/.idea/artifacts/rzcraft_bridge_jar.xml @@ -1,67 +1,69 @@ - $USER_HOME$/MinecraftServers/spigot_1.15.2/plugins + $USER_HOME$/MinecraftServers/spigot_1.15.2/plugins/ - - - - - - - - - - - - - - - - - - - + - - - - + + + - - - - - - - - - - - - - - - - + + - - - + + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 588f4f9..5acab24 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,10 @@ + + + + + diff --git a/build.gradle b/build.gradle index 8dede57..9812b94 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ dependencies { compileOnly "org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT" compile group: 'org.telegram', name: 'telegrambots', version: '4.6' compile group: 'org.yaml', name: 'snakeyaml', version: '1.25' + compile 'com.vdurmont:emoji-java:5.1.1' } compileKotlin { diff --git a/src/config.yml b/src/config.yml index 9bc3445..fd3c9da 100644 --- a/src/config.yml +++ b/src/config.yml @@ -4,5 +4,13 @@ chats: - '-1001315822496' - '-1001419128681' - '-1001281720535' -serverStartMessage: 'Server started' -serverStopMessage: 'Server stopped' +serverStartMessage: 'Server started.' +serverStopMessage: 'Server stopped.' +logJoinLeave: true +logFromMCtoTG: true +logFromTGtoMC: true +strings: + online: 'Online:' + nobodyOnline: 'Nobody online...' + joined: 'joined' + left: 'left' diff --git a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Bot.kt b/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Bot.kt index e2a8c6c..8ceab95 100644 --- a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Bot.kt +++ b/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Bot.kt @@ -1,70 +1,89 @@ package org.kraftwerk28.rzcraft_bridge import org.telegram.telegrambots.bots.TelegramLongPollingBot +import org.telegram.telegrambots.meta.api.methods.ParseMode import org.telegram.telegrambots.meta.api.methods.send.SendMessage import org.telegram.telegrambots.meta.api.objects.Update import org.telegram.telegrambots.meta.api.objects.User +import org.telegram.telegrambots.meta.exceptions.TelegramApiException class Bot(plugin: Plugin) : TelegramLongPollingBot() { private var plugin: Plugin private var allowedChats: List = listOf() + private var chatToMC: Boolean = false private var botToken: String = "" private var botUsername: String = "" init { this.plugin = plugin + chatToMC = plugin.config.getBoolean("logFromTGtoMC", false) allowedChats = plugin.config.getLongList("chats") botToken = plugin.config.getString("botToken")!! botUsername = plugin.config.getString("botUsername")!! } override fun getBotToken() = botToken + override fun getBotUsername() = botUsername + override fun onUpdateReceived(update: Update?) { val msg = update?.message if (msg == null || msg.text == null) return val allowedChats = plugin.config.getLongList("chats") if (!allowedChats.contains(msg.chatId)) return - // /online cmd: shows online players + // cmd shows online players if (msg.text.startsWith("/online")) { - val playerList = plugin.server - .getOnlinePlayers() - .mapIndexed { i, s -> "${i + 1}. ${s.displayName}" } - .joinToString("\n") + val playerCount = plugin.server.onlinePlayers.size +// val playerList = plugin.server +// .getOnlinePlayers() +// .mapIndexed { i, s -> "${i + 1}. ${s.displayName}" } +// .joinToString("\n") + val onlineStr = plugin.config.getString("strings.online", "Online") + val offlineStr = plugin.config.getString( + "strings.nobodyOnline", + "Nobody online" + ) val text = - if (playerList.length > 0) ("Online:\n" + playerList) - else "Nobody online..." - execute(SendMessage(msg.chatId, text).setParseMode("HTML")) + if (playerCount > 0) "$onlineStr: $playerCount" + else offlineStr + execute(SendMessage(msg.chatId, text).setParseMode(ParseMode.HTML)) } - + // stop, if no command matched: if (msg.text!!.startsWith("/")) return - plugin.logger.info("Got text: ${msg.text}") - plugin.server.broadcastMessage( - mcMessageStr(rawUserMention(msg.from), msg.text) - ) + + if (chatToMC) + plugin.sendMessageToMCFrom(rawUserMention(msg.from), msg.text) } fun sendMessageToTGFrom(username: String, text: String) { allowedChats.forEach { - execute(SendMessage(it, mcMessageStr(username, text))) + try { + val msg = SendMessage(it, mcMessageStr(username, text)) + .setParseMode(ParseMode.HTML) + execute(msg) + } catch (e: TelegramApiException) { + } } } fun broadcastToTG(text: String) { allowedChats.forEach { - execute(SendMessage(it, text)) + try { + val msg = SendMessage(it, text).setParseMode(ParseMode.HTML) + execute(msg) + } catch (e: TelegramApiException) { + } } } - private fun commandStr(command: String): String = - "/$command@${botUsername}" - private fun mcMessageStr(username: String, text: String): String = - "<$username> $text" + "$username: $text" private fun rawUserMention(user: User): String = - user.userName ?: user.firstName ?: user.lastName + (if (user.firstName.length < 2) null else user.firstName) + ?: user.userName + ?: user.lastName private fun telegramUserMention(user: User): String = if (user.userName != null) "@${user.userName}" diff --git a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/MessageListener.kt b/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/MessageListener.kt deleted file mode 100644 index d72946b..0000000 --- a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/MessageListener.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.kraftwerk28.rzcraft_bridge - -import org.bukkit.event.Listener -import org.bukkit.event.EventHandler -import org.bukkit.event.player.AsyncPlayerChatEvent - - -class MessageListener(plugin: Plugin) : Listener { - private var plugin: Plugin? = null - init { this.plugin = plugin } - - @EventHandler - fun onPlayerChat1(event: AsyncPlayerChatEvent) { - plugin?.tgBot?.sendMessageToTGFrom( - event.player.displayName, - event.message - ) - } -} diff --git a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Plugin.kt b/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Plugin.kt index 8067c78..d46a31c 100644 --- a/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/rzcraft_bridge/Plugin.kt @@ -1,12 +1,21 @@ package org.kraftwerk28.rzcraft_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() { - var tgBot: Bot? = null +class Plugin : JavaPlugin(), Listener { + + private var tgBot: Bot? = null + private var chatToTG: Boolean = false + override fun onEnable() { if (!File("plugins/${name}/config.yml").exists()) { logger.warning("No config file found! Saving default one.") @@ -17,10 +26,13 @@ class Plugin : JavaPlugin() { ApiContextInitializer.init() val botsApi = TelegramBotsApi() tgBot = Bot(this) + chatToTG = config.getBoolean("logFromMCtoTG", false) + botsApi.registerBot(tgBot) - server.pluginManager.registerEvents(MessageListener(this), this) + server.pluginManager.registerEvents(this, this) + // Notify everything about server start val startMsg = config.getString("serverStartMessage", null) if (startMsg != null) tgBot?.broadcastToTG(startMsg) logger.info("Plugin started") @@ -31,4 +43,36 @@ class Plugin : JavaPlugin() { if (stopMsg != null) tgBot?.broadcastToTG(stopMsg) logger.info("Plugin stopped") } + + fun sendMessageToMC(text: String) { + val prep = EmojiParser.parseToAliases(text) + server.broadcastMessage(prep) + } + + fun sendMessageToMCFrom(username: String, text: String) { + 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.") + } + } } diff --git a/src/plugin.yml b/src/plugin.yml index 793c488..a4307f0 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: RZCraftBridge -version: 0.0.1 +version: 0.0.2 api-version: '1.15' main: org.kraftwerk28.rzcraft_bridge.Plugin description: Telegram <-> Minecraft communication plugin for RzaszkówCraft.