From 4a00808ab54aadabcb8df98b02f210c80cee5aef Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Fri, 24 Jul 2020 15:24:07 +0300 Subject: [PATCH] feature: release 0.0.13 Changelog: - bug fixes - better placeholders for message formatting --- README.md | 4 ++-- .../spigot_tg_bridge/Configuration.kt | 8 +++++--- .../kraftwerk28/spigot_tg_bridge/Constants.kt | 2 +- .../kraftwerk28/spigot_tg_bridge/EventHandler.kt | 16 ++++++++-------- .../org/kraftwerk28/spigot_tg_bridge/TgBot.kt | 9 +++++---- src/main/resources/config.yml | 5 +++-- src/main/resources/plugin.yml | 2 +- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 854b1eb..1cf6a49 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,13 @@ P. S. You can always update plugin configuration without restarting the server. | logPlayerDeath | If true, plugin will send message to Telegram if player died | `boolean` | :x: | `false` | | logPlayerAsleep | If true, plugin will send message to Telegram if player fell asleep | `boolean` | :x: | `false` | | strings | Dictionary of tokens - strings for plugin i18n | `Map` | :x: | See default config | -| commands | Dictionary of command text used in Telegram bot | `Map` | :x: | See default config | +| commands | Dictionary of command text used in Telegram bot | `Map` | :heavy_check_mark: | See below | | telegramMessageFormat | Format string for TGtoMC chat message | `string` | :x: | See default config | ## Telegram bot commands: -Commands are customizeable through config, but there are default values for them as well +Commands are customizeable through config. If command doesn't exist in config, it will be disabled | Command | Description | |:-------:|:------------| diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt index fd48d6f..3d99a22 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt @@ -10,6 +10,7 @@ class Configuration(plugin: Plugin) { var isEnabled: Boolean = false var logFromMCtoTG: Boolean = false var telegramMessageFormat: String = "" + var minecraftMessageFormat: String = "" var serverStartMessage: String? = null var serverStopMessage: String? = null @@ -51,6 +52,7 @@ class Configuration(plugin: Plugin) { logFromTGtoMC = getBoolean("logFromTGtoMC", true) logFromMCtoTG = getBoolean("logFromMCtoTG", true) telegramMessageFormat = getString("telegramMessageFormat", "<%username%>: %message%")!! + minecraftMessageFormat = getString("minecraftMessageFormat", "%username%: %message%")!! allowedChats = getLongList("chats") serverStartMessage = getString("serverStartMessage") serverStopMessage = getString("serverStopMessage") @@ -67,9 +69,9 @@ class Configuration(plugin: Plugin) { logJoinLeave = getBoolean("logJoinLeave", false) onlineString = getString("strings.online", "Online")!! - nobodyOnlineString = getString("strings.offline", "Nobody online")!! - joinString = getString("strings.joined", "joined") - leaveString = getString("strings.left", "left") + nobodyOnlineString = getString("strings.nobodyOnline", "Nobody online")!! + joinString = getString("strings.joined", "%username% joined.") + leaveString = getString("strings.left", "%username% left.") logDeath = getBoolean("logPlayerDeath", false) logPlayerAsleep = getBoolean("logPlayerAsleep", false) diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt index 31b310c..374e312 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt @@ -8,7 +8,7 @@ object Constants { const val noUsername = "Bot username must be defined." } object INFO { - const val reloading = "Reloading plugin... This may take some time" + const val reloading = "Reloading plugin... This may take some time." const val reloadComplete = "Reload completed." } object TIMES_OF_DAY { diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt index 02cec29..ab364b7 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt @@ -24,17 +24,17 @@ class EventHandler( @EventHandler fun onPlayerJoin(event: PlayerJoinEvent) { - if (!config.logJoinLeave) return - val text = "${TgBot.escape(event.player.displayName)} " + - "${config.joinString}." + if (!config.logJoinLeave || config.joinString == null) return + val username = TgBot.fullEscape(event.player.displayName) + val text = config.joinString!!.replace("%username%", username) plugin.tgBot.broadcastToTG(text) } @EventHandler fun onPlayerLeave(event: PlayerQuitEvent) { - if (!config.logJoinLeave) return - val text = "${TgBot.escape(event.player.displayName)} " + - "${config.leaveString}." + if (!config.logJoinLeave || config.leaveString == null) return + val username = TgBot.fullEscape(event.player.displayName) + val text = config.leaveString!!.replace("%username%", username) plugin.tgBot.broadcastToTG(text) } @@ -42,8 +42,8 @@ class EventHandler( fun onPlayerDied(event: PlayerDeathEvent) { if (!config.logDeath) return event.deathMessage?.let { - val plName = event.entity.displayName - val text = it.replace(plName, "$plName") + val username = TgBot.fullEscape(event.entity.displayName) + val text = it.replace(username, "$username") plugin.tgBot.broadcastToTG(text) } } diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt index a95b89d..4699de8 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -8,7 +8,6 @@ import com.github.kotlintelegrambot.entities.ParseMode import com.github.kotlintelegrambot.entities.Update import com.github.kotlintelegrambot.entities.User import okhttp3.logging.HttpLoggingInterceptor -import kotlin.reflect.KProperty0 import org.kraftwerk28.spigot_tg_bridge.Constants as C fun Bot.skipUpdates(lastUpdateID: Long = 0) { @@ -106,7 +105,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { val playerList = plugin.server.onlinePlayers val playerStr = plugin.server .onlinePlayers - .mapIndexed { i, s -> "${i + 1}. ${s.displayName}" } + .mapIndexed { i, s -> "${i + 1}. ${fullEscape(s.displayName)}" } .joinToString("\n") val text = if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr" @@ -159,7 +158,9 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { } private fun messageFromMinecraft(username: String, text: String): String = - "${escape(username)}: $text" + config.minecraftMessageFormat + .replace("%username%", fullEscape(username)) + .replace("%message%", escapeHTML(text)) private fun rawUserMention(user: User): String = (if (user.firstName.length < 2) null else user.firstName) @@ -176,6 +177,6 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { fun escapeHTML(s: String) = s.replace("&", "&").replace(">", ">").replace("<", "<") fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "") - fun escape(s: String) = escapeColorCodes(escapeHTML(s)) + fun fullEscape(s: String) = escapeColorCodes(escapeHTML(s)) } } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1bc9f6e..d988f5b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,11 +11,12 @@ logFromTGtoMC: true logPlayerDeath: false logPlayerAsleep: false telegramMessageFormat: '<%username%>: %message%' +minecraftMessageFormat: '%username%: %message%' strings: online: 'Online' nobodyOnline: 'Nobody online...' - joined: 'joined' - left: 'left' + joined: '%username% joined.' + left: '%username% left.' commands: time: 'time' online: 'online' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2b24352..7c2f09a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SpigotTGBridge -version: 0.0.12 +version: 0.0.13 api-version: '1.15' main: org.kraftwerk28.spigot_tg_bridge.Plugin description: Telegram <-> Minecraft communication plugin for Spigot.