From 826c556ae9d5316ebf710bfaba122168a52a7a72 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Sat, 3 Jul 2021 19:22:13 +0300 Subject: [PATCH] Refactor configuration class Deprecate "minecraftMessageFormat" and "telegramMessageFormat" in favor of new options --- build.gradle.kts | 2 +- .../spigot_tg_bridge/BotCommands.kt | 6 +- .../spigot_tg_bridge/Configuration.kt | 133 ++++++++++-------- .../kraftwerk28/spigot_tg_bridge/Constants.kt | 1 + .../spigot_tg_bridge/EventHandler.kt | 11 +- .../kraftwerk28/spigot_tg_bridge/Plugin.kt | 26 ++-- .../org/kraftwerk28/spigot_tg_bridge/TgBot.kt | 16 ++- src/main/resources/config.yml | 16 +-- 8 files changed, 126 insertions(+), 85 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d9cd66b..3be5592 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,7 +37,7 @@ repositories { val tgBotVersion = "6.0.4" val retrofitVersion = "2.7.1" -val plugDir = "MinecraftServers/spigot_1.16.5/plugins/" +val plugDir = "MinecraftServers/spigot_1.17/plugins/" val homeDir = System.getProperty("user.home") tasks { diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt index 51bb56c..e3030b4 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt @@ -1,14 +1,14 @@ package org.kraftwerk28.spigot_tg_bridge -import org.bukkit.configuration.file.YamlConfiguration +import org.bukkit.configuration.file.FileConfiguration -class Commands(yamlCfg: YamlConfiguration) { +class Commands(cfg: FileConfiguration) { val time: String? val online: String? val chatID: String? init { - yamlCfg.run { + cfg.run { time = getString("commands.time") online = getString("commands.online") chatID = getString("commands.chat_id") 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 776eeaf..904ef59 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt @@ -4,73 +4,96 @@ import org.bukkit.configuration.file.YamlConfiguration import java.io.File import org.kraftwerk28.spigot_tg_bridge.Constants as C -class Configuration { - private lateinit var yamlCfg: YamlConfiguration - - var isEnabled: Boolean = false - var logFromMCtoTG: Boolean = false - var telegramMessageFormat: String = "" - var minecraftMessageFormat: String = "" - var serverStartMessage: String? = null - var serverStopMessage: String? = null +class Configuration(plugin: Plugin) { + val isEnabled: Boolean + val logFromMCtoTG: Boolean + val telegramFormat: String + val minecraftFormat: String + val serverStartMessage: String? + val serverStopMessage: String? + val logJoinLeave: Boolean + val joinString: String + val leaveString: String + val logDeath: Boolean + val logPlayerAsleep: Boolean + val onlineString: String + val nobodyOnlineString: String // Telegram bot stuff - var botToken: String = "" - var allowedChats: List = listOf() - var logFromTGtoMC: Boolean = false - var allowWebhook: Boolean = false - var webhookConfig: Map? = null + val botToken: String + val allowedChats: List + val logFromTGtoMC: Boolean + val allowWebhook: Boolean + val webhookConfig: Map? - var logJoinLeave: Boolean = false - var joinString: String? = null - var leaveString: String? = null - var logDeath: Boolean = false - var logPlayerAsleep: Boolean = false - var onlineString: String = "" - var nobodyOnlineString: String = "" + var commands: Commands - lateinit var commands: Commands - - fun reload(plugin: Plugin) { + init { val cfgFile = File(plugin.dataFolder, C.configFilename); if (!cfgFile.exists()) { cfgFile.parentFile.mkdirs() - plugin.saveResource(C.configFilename, false); + plugin.saveDefaultConfig() + // plugin.saveResource(C.configFilename, false); throw Exception(C.WARN.noConfigWarning) } - yamlCfg = YamlConfiguration() - yamlCfg.load(cfgFile) - - yamlCfg.run { - isEnabled = getBoolean("enable", true) - 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") - botToken = getString("botToken") ?: throw Exception(C.WARN.noToken) - allowWebhook = getBoolean("useWebhook", false) - val whCfg = get("webhookConfig") - if (whCfg is Map<*, *>) { - @Suppress("UNCHECKED_CAST") - webhookConfig = whCfg as Map? - } - - logJoinLeave = getBoolean("logJoinLeave", false) - onlineString = getString("strings.online", "Online")!! - 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) - + val pluginConfig = plugin.getConfig() + pluginConfig.getString("minecraftMessageFormat")?.let { + plugin.logger.warning(""" + Config option "minecraftMessageFormat" is deprecated. + Moved it to new key "telegramFormat" + """.trimIndent().replace('\n', ' ')) + pluginConfig.set("telegramFormat", it) + pluginConfig.set("minecraftMessageFormat", null) + plugin.saveConfig() + } + pluginConfig.getString("telegramMessageFormat")?.let { + plugin.logger.warning(""" + Config option "telegramMessageFormat" is deprecated. + Moved it to new key "minecraftFormat" + """.trimIndent().replace('\n', ' ')) + pluginConfig.set("minecraftFormat", it) + pluginConfig.set("telegramMessageFormat", null) + plugin.saveConfig() } - commands = Commands(yamlCfg) + pluginConfig.run { + isEnabled = getBoolean("enable", true) + serverStartMessage = getString("serverStartMessage") + serverStopMessage = getString("serverStopMessage") + logFromTGtoMC = getBoolean("logFromTGtoMC", true) + logFromMCtoTG = getBoolean("logFromMCtoTG", true) + telegramFormat = getString( + "telegramFormat", + "%username%: %message%", + )!! + minecraftFormat = getString( + "minecraftFormat", + "<%username%>: %message%", + )!! + // isEnabled = getBoolean("enable", true) + allowedChats = getLongList("chats") + botToken = getString("botToken") ?: throw Exception(C.WARN.noToken) + allowWebhook = getBoolean("useWebhook", false) + @Suppress("unchecked_cast") + webhookConfig = get("webhookConfig") as Map? + logJoinLeave = getBoolean("logJoinLeave", false) + onlineString = getString("strings.online", "Online")!! + 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) + commands = Commands(this) + } } - fun load(plugin: Plugin) = reload(plugin) + companion object { + } } 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 374e312..563c9b2 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt @@ -19,6 +19,7 @@ object Constants { } const val USERNAME_PLACEHOLDER = "%username%" const val MESSAGE_TEXT_PLACEHOLDER = "%message%" + const val CHAT_TITLE_PLACEHOLDER = "%chat%" object COMMANDS { const val PLUGIN_RELOAD = "tgbridge_reload" } 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 7f9b524..0be14f3 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt @@ -25,17 +25,17 @@ class EventHandler( @EventHandler fun onPlayerJoin(event: PlayerJoinEvent) { - if (!config.logJoinLeave || config.joinString == null) return + if (!config.logJoinLeave) return val username = event.player.displayName.fullEscape() - val text = config.joinString!!.replace("%username%", username) + val text = config.joinString.replace("%username%", username) tgBot.sendMessageToTelegram(text) } @EventHandler fun onPlayerLeave(event: PlayerQuitEvent) { - if (!config.logJoinLeave || config.leaveString == null) return + if (!config.logJoinLeave) return val username = event.player.displayName.fullEscape() - val text = config.leaveString!!.replace("%username%", username) + val text = config.leaveString.replace("%username%", username) tgBot.sendMessageToTelegram(text) } @@ -52,7 +52,8 @@ class EventHandler( @EventHandler fun onPlayerAsleep(event: PlayerBedEnterEvent) { if (!config.logPlayerAsleep) return - if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return + if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) + return val text = "${event.player.displayName} fell asleep." tgBot.sendMessageToTelegram(text) } diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt index 7c13e3f..fdda5db 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt @@ -8,11 +8,11 @@ import org.kraftwerk28.spigot_tg_bridge.Constants as C class Plugin : JavaPlugin() { var tgBot: TgBot? = null - val config: Configuration = Configuration() + lateinit var config: Configuration override fun onEnable() { try { - config.load(this) + config = Configuration(this) } catch (e: Exception) { logger.warning(e.message) return @@ -42,26 +42,36 @@ class Plugin : JavaPlugin() { } override fun onDisable() { - if (!config.isEnabled) return + if (!config.isEnabled) + return config.serverStopMessage?.let { tgBot?.sendMessageToTelegram(it) } logger.info("Plugin stopped.") } - fun sendMessageToMinecraft(text: String, username: String? = null) = - config.telegramMessageFormat + fun sendMessageToMinecraft( + text: String, + username: String? = null, + chatTitle: String? = null, + ) = + config.minecraftFormat .replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeEmoji()) .run { - username?.let { username -> - replace(C.USERNAME_PLACEHOLDER, username.escapeEmoji()) + username?.let { + replace(C.USERNAME_PLACEHOLDER, it.escapeEmoji()) + } ?: this + } + .run { + chatTitle?.let { + replace(C.CHAT_TITLE_PLACEHOLDER, it) } ?: this } .also { server.broadcastMessage(it) } fun reload() { logger.info(C.INFO.reloading) - config.reload(this) + config = Configuration(this) loadBot() logger.info(C.INFO.reloadComplete) } 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 ea74914..98b534b 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -176,7 +176,9 @@ class TgBot( } fun sendMessageToTelegram(text: String, username: String? = null) { - val messageText = username?.let { formatMsgFromMinecraft(it, text) } ?: text + val messageText = username?.let { + formatMsgFromMinecraft(it, text) + } ?: text config.allowedChats.forEach { chatId -> scope.launch { api.sendMessage(chatId, messageText) @@ -188,14 +190,18 @@ class TgBot( val msg = update.message!! if (!config.logFromTGtoMC || msg.from == null) return - plugin.sendMessageToMinecraft(msg.text!!, msg.from.rawUserMention()) + plugin.sendMessageToMinecraft( + text = msg.text!!, + username = msg.from.rawUserMention(), + chatTitle = msg.chat.title, + ) } private fun formatMsgFromMinecraft( username: String, text: String ): String = - config.minecraftMessageFormat - .replace("%username%", username.fullEscape()) - .replace("%message%", text.escapeHtml()) + config.telegramFormat + .replace(C.USERNAME_PLACEHOLDER, username.fullEscape()) + .replace(C.MESSAGE_TEXT_PLACEHOLDER, text.escapeHtml()) } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e2306d7..dc98202 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,20 +2,20 @@ enable: true botToken: abcdef123456789 chats: - -1234567890123 -serverStartMessage: 'Server started.' -serverStopMessage: 'Server stopped.' +serverStartMessage: "Server started." +serverStopMessage: "Server stopped." logJoinLeave: true logFromMCtoTG: true logFromTGtoMC: true logPlayerDeath: false logPlayerAsleep: false -telegramMessageFormat: '<%username%>: %message%' -minecraftMessageFormat: '%username%: %message%' +minecraftFormat: "<%username%>: %message%" +telegramFormat: "%username%: %message%" strings: - online: 'Online' - nobodyOnline: 'Nobody online...' - joined: '%username% joined.' - left: '%username% left.' + online: "Online" + nobodyOnline: "Nobody online..." + joined: "%username% joined." + left: "%username% left." commands: time: 'time' online: 'online'