Skip to content

Commit

Permalink
feature: release 0.0.13
Browse files Browse the repository at this point in the history
Changelog:
  - bug fixes
  - better placeholders for message formatting
  • Loading branch information
kraftwerk28 committed Jul 24, 2020
1 parent 8c7fe22 commit 4a00808
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>` | :x: | See default config |
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :x: | See default config |
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :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 |
|:-------:|:------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -51,6 +52,7 @@ class Configuration(plugin: Plugin) {
logFromTGtoMC = getBoolean("logFromTGtoMC", true)
logFromMCtoTG = getBoolean("logFromMCtoTG", true)
telegramMessageFormat = getString("telegramMessageFormat", "<%username%>: %message%")!!
minecraftMessageFormat = getString("minecraftMessageFormat", "<i>%username%</i>: %message%")!!
allowedChats = getLongList("chats")
serverStartMessage = getString("serverStartMessage")
serverStopMessage = getString("serverStopMessage")
Expand All @@ -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", "<i>%username%</i> joined.")
leaveString = getString("strings.left", "<i>%username%</i> left.")
logDeath = getBoolean("logPlayerDeath", false)
logPlayerAsleep = getBoolean("logPlayerAsleep", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ class EventHandler(

@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {
if (!config.logJoinLeave) return
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
"${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 = "<i>${TgBot.escape(event.player.displayName)}</i> " +
"${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)
}

@EventHandler
fun onPlayerDied(event: PlayerDeathEvent) {
if (!config.logDeath) return
event.deathMessage?.let {
val plName = event.entity.displayName
val text = it.replace(plName, "<i>$plName</i>")
val username = TgBot.fullEscape(event.entity.displayName)
val text = it.replace(username, "<i>$username</i>")
plugin.tgBot.broadcastToTG(text)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -159,7 +158,9 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
}

private fun messageFromMinecraft(username: String, text: String): String =
"<i>${escape(username)}</i>: $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)
Expand All @@ -176,6 +177,6 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
fun escapeHTML(s: String) =
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "")
fun escape(s: String) = escapeColorCodes(escapeHTML(s))
fun fullEscape(s: String) = escapeColorCodes(escapeHTML(s))
}
}
5 changes: 3 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ logFromTGtoMC: true
logPlayerDeath: false
logPlayerAsleep: false
telegramMessageFormat: '<%username%>: %message%'
minecraftMessageFormat: '<i>%username%</i>: %message%'
strings:
online: '<b>Online</b>'
nobodyOnline: '<b>Nobody online...</b>'
joined: 'joined'
left: 'left'
joined: '<i>%username%</i> joined.'
left: '<i>%username%</i> left.'
commands:
time: 'time'
online: 'online'
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 4a00808

Please sign in to comment.