Skip to content

Commit 4a00808

Browse files
committed
feature: release 0.0.13
Changelog: - bug fixes - better placeholders for message formatting
1 parent 8c7fe22 commit 4a00808

File tree

7 files changed

+25
-21
lines changed

7 files changed

+25
-21
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ P. S. You can always update plugin configuration without restarting the server.
5757
| logPlayerDeath | If true, plugin will send message to Telegram if player died | `boolean` | :x: | `false` |
5858
| logPlayerAsleep | If true, plugin will send message to Telegram if player fell asleep | `boolean` | :x: | `false` |
5959
| strings | Dictionary of tokens - strings for plugin i18n | `Map<string, string>` | :x: | See default config |
60-
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :x: | See default config |
60+
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :heavy_check_mark: | See below |
6161
| telegramMessageFormat | Format string for TGtoMC chat message | `string` | :x: | See default config |
6262

6363

6464
## Telegram bot commands:
6565

66-
Commands are customizeable through config, but there are default values for them as well
66+
Commands are customizeable through config. If command doesn't exist in config, it will be disabled
6767

6868
| Command | Description |
6969
|:-------:|:------------|

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Configuration(plugin: Plugin) {
1010
var isEnabled: Boolean = false
1111
var logFromMCtoTG: Boolean = false
1212
var telegramMessageFormat: String = ""
13+
var minecraftMessageFormat: String = ""
1314
var serverStartMessage: String? = null
1415
var serverStopMessage: String? = null
1516

@@ -51,6 +52,7 @@ class Configuration(plugin: Plugin) {
5152
logFromTGtoMC = getBoolean("logFromTGtoMC", true)
5253
logFromMCtoTG = getBoolean("logFromMCtoTG", true)
5354
telegramMessageFormat = getString("telegramMessageFormat", "<%username%>: %message%")!!
55+
minecraftMessageFormat = getString("minecraftMessageFormat", "<i>%username%</i>: %message%")!!
5456
allowedChats = getLongList("chats")
5557
serverStartMessage = getString("serverStartMessage")
5658
serverStopMessage = getString("serverStopMessage")
@@ -67,9 +69,9 @@ class Configuration(plugin: Plugin) {
6769

6870
logJoinLeave = getBoolean("logJoinLeave", false)
6971
onlineString = getString("strings.online", "Online")!!
70-
nobodyOnlineString = getString("strings.offline", "Nobody online")!!
71-
joinString = getString("strings.joined", "joined")
72-
leaveString = getString("strings.left", "left")
72+
nobodyOnlineString = getString("strings.nobodyOnline", "Nobody online")!!
73+
joinString = getString("strings.joined", "<i>%username%</i> joined.")
74+
leaveString = getString("strings.left", "<i>%username%</i> left.")
7375
logDeath = getBoolean("logPlayerDeath", false)
7476
logPlayerAsleep = getBoolean("logPlayerAsleep", false)
7577

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Constants {
88
const val noUsername = "Bot username must be defined."
99
}
1010
object INFO {
11-
const val reloading = "Reloading plugin... This may take some time"
11+
const val reloading = "Reloading plugin... This may take some time."
1212
const val reloadComplete = "Reload completed."
1313
}
1414
object TIMES_OF_DAY {

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ class EventHandler(
2424

2525
@EventHandler
2626
fun onPlayerJoin(event: PlayerJoinEvent) {
27-
if (!config.logJoinLeave) return
28-
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
29-
"${config.joinString}."
27+
if (!config.logJoinLeave || config.joinString == null) return
28+
val username = TgBot.fullEscape(event.player.displayName)
29+
val text = config.joinString!!.replace("%username%", username)
3030
plugin.tgBot.broadcastToTG(text)
3131
}
3232

3333
@EventHandler
3434
fun onPlayerLeave(event: PlayerQuitEvent) {
35-
if (!config.logJoinLeave) return
36-
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
37-
"${config.leaveString}."
35+
if (!config.logJoinLeave || config.leaveString == null) return
36+
val username = TgBot.fullEscape(event.player.displayName)
37+
val text = config.leaveString!!.replace("%username%", username)
3838
plugin.tgBot.broadcastToTG(text)
3939
}
4040

4141
@EventHandler
4242
fun onPlayerDied(event: PlayerDeathEvent) {
4343
if (!config.logDeath) return
4444
event.deathMessage?.let {
45-
val plName = event.entity.displayName
46-
val text = it.replace(plName, "<i>$plName</i>")
45+
val username = TgBot.fullEscape(event.entity.displayName)
46+
val text = it.replace(username, "<i>$username</i>")
4747
plugin.tgBot.broadcastToTG(text)
4848
}
4949
}

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.github.kotlintelegrambot.entities.ParseMode
88
import com.github.kotlintelegrambot.entities.Update
99
import com.github.kotlintelegrambot.entities.User
1010
import okhttp3.logging.HttpLoggingInterceptor
11-
import kotlin.reflect.KProperty0
1211
import org.kraftwerk28.spigot_tg_bridge.Constants as C
1312

1413
fun Bot.skipUpdates(lastUpdateID: Long = 0) {
@@ -106,7 +105,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
106105
val playerList = plugin.server.onlinePlayers
107106
val playerStr = plugin.server
108107
.onlinePlayers
109-
.mapIndexed { i, s -> "${i + 1}. ${s.displayName}" }
108+
.mapIndexed { i, s -> "${i + 1}. ${fullEscape(s.displayName)}" }
110109
.joinToString("\n")
111110
val text =
112111
if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr"
@@ -159,7 +158,9 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
159158
}
160159

161160
private fun messageFromMinecraft(username: String, text: String): String =
162-
"<i>${escape(username)}</i>: $text"
161+
config.minecraftMessageFormat
162+
.replace("%username%", fullEscape(username))
163+
.replace("%message%", escapeHTML(text))
163164

164165
private fun rawUserMention(user: User): String =
165166
(if (user.firstName.length < 2) null else user.firstName)
@@ -176,6 +177,6 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
176177
fun escapeHTML(s: String) =
177178
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
178179
fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "")
179-
fun escape(s: String) = escapeColorCodes(escapeHTML(s))
180+
fun fullEscape(s: String) = escapeColorCodes(escapeHTML(s))
180181
}
181182
}

src/main/resources/config.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ logFromTGtoMC: true
1111
logPlayerDeath: false
1212
logPlayerAsleep: false
1313
telegramMessageFormat: '<%username%>: %message%'
14+
minecraftMessageFormat: '<i>%username%</i>: %message%'
1415
strings:
1516
online: '<b>Online</b>'
1617
nobodyOnline: '<b>Nobody online...</b>'
17-
joined: 'joined'
18-
left: 'left'
18+
joined: '<i>%username%</i> joined.'
19+
left: '<i>%username%</i> left.'
1920
commands:
2021
time: 'time'
2122
online: 'online'

src/main/resources/plugin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: SpigotTGBridge
2-
version: 0.0.12
2+
version: 0.0.13
33
api-version: '1.15'
44
main: org.kraftwerk28.spigot_tg_bridge.Plugin
55
description: Telegram <-> Minecraft communication plugin for Spigot.

0 commit comments

Comments
 (0)