diff --git a/README.md b/README.md index 3070585..875d3ba 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,15 @@ | logJoinLeave | If true, plugin will send corresponding messages to chats, when player joins or leaves server | boolean | :x: | true | | logFromMCtoTG | If true, plugin will send messages from players on server, to Telegram chats | boolean | :x: | true | | logFromTGtoMC | If true, plugin will send messages from chats, to Minecraft server | boolean | :x: | true | +| 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: + +Commands are customizeable through config, but there are default values for them as well + | Command | Description | |:-------:|:------------| | `/online` | Get players, currently online | diff --git a/build.gradle b/build.gradle index a20d673..b4ade65 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,14 @@ +import org.yaml.snakeyaml.Yaml + +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath group: 'org.yaml', name: 'snakeyaml', version: '1.26' + } +} + plugins { id 'java' id 'org.jetbrains.kotlin.jvm' version '1.3.60' @@ -5,10 +16,10 @@ plugins { } group 'org.kraftwerk28' -version '0.0.4-SNAPSHOT' - -//def defaultOutDir = "$System.env.HOME/MinecraftServers/spigot_1.15.2/plugins/" -//rootProject.libsDirName = System.getenv("OUT_DIR") ?: defaultOutDir +def pluginConfigFile = new File("src/main/resources/plugin.yml").newInputStream() +def cfg = (Map)new Yaml().load(pluginConfigFile) +def v = cfg.get("version") +version "$v-SNAPSHOT" repositories { maven { @@ -23,6 +34,15 @@ repositories { mavenCentral() } +def plugDir = "MinecraftServers/spigot_1.15.2/plugins/" +def homeDir = System.properties['user.home'] + +task cpArtifacts(type: Copy) { + from shadowJar + into "$homeDir/$plugDir" +} +shadowJar.finalizedBy cpArtifacts + dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" testImplementation group: 'junit', name: 'junit', version: '4.12' @@ -30,8 +50,8 @@ dependencies { implementation group: 'org.telegram', name: 'telegrambots', version: '4.6' implementation 'com.vdurmont:emoji-java:5.1.1' - def tgBotVer = '5.0.0' - implementation "io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:$tgBotVer" +// def tgBotVer = '5.0.0' +// implementation "io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:$tgBotVer" } compileKotlin { diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt index 80d26b2..54c55e3 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Bot.kt @@ -87,9 +87,9 @@ class Bot(private var plugin: Plugin) : TelegramLongPollingBot() { } fun broadcastToTG(text: String) { - allowedChats.forEach { + allowedChats.forEach { chatID -> try { - val msg = SendMessage(it, text).setParseMode(ParseMode.HTML) + val msg = SendMessage(chatID, text).setParseMode(ParseMode.HTML) execute(msg) } catch (e: TelegramApiException) { } @@ -118,4 +118,8 @@ class Bot(private var plugin: Plugin) : TelegramLongPollingBot() { (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}" + else "${user.firstName ?: user.lastName}" } 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 a3cf583..fd71f84 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt @@ -12,6 +12,8 @@ object Constants { val SERVER_START_MSG = "serverStartMessage" val SERVER_STOP_MSG = "serverStopMessage" val LOG_JOIN_LEAVE = "logJoinLeave" + val LOG_PLAYER_DEATH = "logPlayerDeath" + val LOG_PLAYER_ASLEEP = "logPlayerAsleep" object STRINGS { val ONLINE = "strings.online" val OFFLINE = "strings.offline" @@ -27,6 +29,8 @@ object Constants { val logFromMCtoTG = false val logFromTGtoMC = false val logJoinLeave = false + val logPlayerDeath = false + val logPlayerAsleep = false object COMMANDS { val TIME = "/time" val ONLINE = "/online" 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 fc234d7..90dffbe 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt @@ -2,7 +2,9 @@ package org.kraftwerk28.spigot_tg_bridge import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.event.entity.PlayerDeathEvent import org.bukkit.event.player.AsyncPlayerChatEvent +import org.bukkit.event.player.PlayerBedEnterEvent import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerQuitEvent import org.kraftwerk28.spigot_tg_bridge.Constants as C @@ -12,13 +14,18 @@ class EventHandler(private val plugin: Plugin) : Listener { private val joinStr: String private val leftStr: String private val logJoinLeave: Boolean + private val logDeathMessage: Boolean + private val logPlayerAsleep: Boolean init { plugin.config.run { joinStr = getString(C.FIELDS.STRINGS.JOINED, C.DEFS.playerJoined)!! leftStr = getString(C.FIELDS.STRINGS.LEFT, C.DEFS.playerLeft)!! logJoinLeave = getBoolean(C.FIELDS.LOG_JOIN_LEAVE, C.DEFS.logJoinLeave) + logDeathMessage = getBoolean(C.FIELDS.LOG_PLAYER_DEATH, C.DEFS.logPlayerDeath) + logPlayerAsleep = getBoolean(C.FIELDS.LOG_PLAYER_ASLEEP, C.DEFS.logPlayerAsleep) } + plugin.logger.info("Log death message: $logDeathMessage") } @EventHandler @@ -43,4 +50,22 @@ class EventHandler(private val plugin: Plugin) : Listener { val text = "${escapeHTML(event.player.displayName)} $leftStr." plugin.tgBot?.broadcastToTG(text) } + + @EventHandler + fun onPlayerDied(event: PlayerDeathEvent) { + if (!logDeathMessage) return + event.deathMessage?.let { + val plName = event.entity.displayName + val text = it.replace(plName, "$plName") + plugin.tgBot?.broadcastToTG(text) + } + } + + @EventHandler + fun onPlayerAsleep(event: PlayerBedEnterEvent) { + if (!logPlayerAsleep) return + if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return + val text = "${event.player.displayName} fell asleep." + plugin.tgBot?.broadcastToTG(text) + } } \ No newline at end of file diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Utils.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Utils.kt index af40d27..e2864af 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Utils.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Utils.kt @@ -5,6 +5,3 @@ import org.telegram.telegrambots.meta.api.objects.User fun escapeHTML(s: String): String = s.replace("&", "&").replace(">", ">").replace("<", "<") -fun telegramUserMention(user: User): String = - if (user.userName != null) "@${user.userName}" - else "${user.firstName ?: user.lastName}" diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 30d22b9..d3740a7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,9 +7,13 @@ serverStopMessage: 'Server stopped.' logJoinLeave: true logFromMCtoTG: true logFromTGtoMC: true +logPlayerDeath: false +logPlayerAsleep: false strings: online: 'Online' nobodyOnline: 'Nobody online...' joined: 'joined' left: 'left' - +commands: + time: '/time' + online: '/online' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index cd42a1d..825d4fd 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SpigotTGBridge -version: 0.0.6 +version: 0.0.7 api-version: '1.15' main: org.kraftwerk28.spigot_tg_bridge.Plugin description: Telegram <-> Minecraft communication plugin for Spigot.