Skip to content

Commit

Permalink
add: reload feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftwerk28 committed Jul 19, 2020
1 parent 3afb144 commit e98acd5
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 162 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
|:-----:|:------------|:----:|:--------:|:-------:|
| enable | If plugin should be enabled | `boolean` | :x: | `true` |
| botToken | Telegram bot token ([How to create bot](https://core.telegram.org/bots#3-how-do-i-create-a-bot)) | `string` | :heavy_check_mark: | - |
| botUsername | Telegram bot username | string | :heavy_check_mark: | - |
| botUsername | Telegram bot username | `string` | :heavy_check_mark: | - |
| chats | Chats, where bot will work (to prevent using bot by unknown chats) | `number[] or string[]` | :heavy_check_mark: | `[]` |
| serverStartMessage | What will be sent to chats when server starts | `string` | :x: | `'Server started.'` |
| serverStopMessage | What will be sent to chats when server stops | `string` | :x: | `'Server stopped.'` |
Expand All @@ -35,7 +35,7 @@
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :x: | See default config |
| telegramMessageFormat | Format string for TGtoMC chat message | `string` | :x: | See default config |

## Commands:
## Telegram bot commands:

Commands are customizeable through config, but there are default values for them as well

Expand All @@ -48,4 +48,10 @@ Commands are customizeable through config, but there are default values for them
Must contain `%username%` and `%message` inside.
You can customize message color with it. See [message color codes](https://www.digminecraft.com/lists/color_list_pc.php).

P. S.: related to [this issue](https://github.com/kraftwerk28/spigot-tg-bridge/issues/6)
P. S.: related to [this issue](https://github.com/kraftwerk28/spigot-tg-bridge/issues/6)

## Plugin commands:

| Command | Description |
|:-------:|:------------|
| `/tgbridge_reload` | Reload plugin configuration w/o need to stop the server. Works only through server console |
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ plugins {
group 'org.kraftwerk28'
def pluginConfigFile = new File("src/main/resources/plugin.yml").newInputStream()
def cfg = (Map<String, Object>)new Yaml().load(pluginConfigFile)
def v = cfg.get("version")
version "$v-SNAPSHOT"
version cfg.get("version")

repositories {
maven {
Expand All @@ -42,6 +41,10 @@ task cpArtifacts(type: Copy) {
from shadowJar
into "$homeDir/$plugDir"
}

shadowJar {
archiveClassifier = null
}
shadowJar.finalizedBy cpArtifacts

dependencies {
Expand Down
16 changes: 5 additions & 11 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package org.kraftwerk28.spigot_tg_bridge

import org.kraftwerk28.spigot_tg_bridge.Constants as C
import org.bukkit.configuration.file.YamlConfiguration

class Commands(plugin: Plugin) {
class Commands(yamlCfg: YamlConfiguration) {
val time: String
val online: String
init {
plugin.config.run {
time = getString(
C.FIELDS.COMMANDS.TIME,
C.DEFS.COMMANDS.TIME
)!!
online = getString(
C.FIELDS.COMMANDS.ONLINE,
C.DEFS.COMMANDS.ONLINE
)!!
yamlCfg.run {
time = getString("commands.time", "time")!!
online = getString("commands.online", "online")!!
}
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/CommandHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.kraftwerk28.spigot_tg_bridge

import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.ConsoleCommandSender
import org.kraftwerk28.spigot_tg_bridge.Constants as C

class CommandHandler(private val plugin: Plugin) : CommandExecutor {
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
if (sender !is ConsoleCommandSender) return false
return when (label) {
C.COMMANDS.PLUGIN_RELOAD -> {
plugin.reload()
true
}
else -> false
}
}
}
80 changes: 80 additions & 0 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.kraftwerk28.spigot_tg_bridge

import org.bukkit.configuration.file.YamlConfiguration
import java.io.File
import org.kraftwerk28.spigot_tg_bridge.Constants as C

class Configuration(plugin: Plugin) {
private lateinit var yamlCfg: YamlConfiguration

var isEnabled: Boolean = false
var logFromMCtoTG: Boolean = false
var telegramMessageFormat: String = ""
var serverStartMessage: String? = null
var serverStopMessage: String? = null

// Telegram bot stuff
var botToken: String = ""
var botUsername: String = ""
var allowedChats: List<Long> = listOf()
var logFromTGtoMC: Boolean = false
var allowWebhook: Boolean = false
var webhookConfig: Map<String, Any>? = null

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 = ""

lateinit var commands: Commands

init {
reload(plugin)
}

fun reload(plugin: Plugin) {
val cfgFile = File(plugin.dataFolder, C.configFilename);
if (!cfgFile.exists()) {
cfgFile.parentFile.mkdirs()
plugin.saveResource(C.configFilename, false);
throw Exception()
}

yamlCfg = YamlConfiguration()
yamlCfg.load(cfgFile)

yamlCfg.run {
isEnabled = getBoolean("enable", true)
logFromTGtoMC = getBoolean("logFromTGtoMC", true)
logFromMCtoTG = getBoolean("logFromMCtoTG", true)
telegramMessageFormat = getString("telegramMessageFormat", "<%username%>: %message%")!!
allowedChats = getLongList("chats")
serverStartMessage = getString("serverStartMessage")
serverStopMessage = getString("serverStopMessage")

botToken = getString("botToken") ?: throw Exception(C.WARN.noToken)
botUsername = getString("botUsername") ?: throw Exception(C.WARN.noUsername)

allowWebhook = getBoolean("useWebhook", false)
val whCfg = get("webhookConfig")
if (whCfg is Map<*, *>) {
@Suppress("UNCHECKED_CAST")
webhookConfig = whCfg as Map<String, Any>?
}

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")
logDeath = getBoolean("logPlayerDeath", false)
logPlayerAsleep = getBoolean("logPlayerAsleep", false)

}

commands = Commands(yamlCfg)
}
}
52 changes: 7 additions & 45 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,15 @@ package org.kraftwerk28.spigot_tg_bridge

object Constants {
const val configFilename = "config.yml"
// Config field names
object FIELDS {
const val ENABLE = "enable"
const val BOT_TOKEN = "botToken"
const val BOT_USERNAME = "botUsername"
const val ALLOWED_CHATS = "chats"
const val LOG_FROM_MC_TO_TG = "logFromMCtoTG"
const val LOG_FROM_TG_TO_MC = "logFromTGtoMC"
const val SERVER_START_MSG = "serverStartMessage"
const val SERVER_STOP_MSG = "serverStopMessage"
const val LOG_JOIN_LEAVE = "logJoinLeave"
const val LOG_PLAYER_DEATH = "logPlayerDeath"
const val LOG_PLAYER_ASLEEP = "logPlayerAsleep"
const val USE_WEBHOOK = "useWebhook"
const val WEBHOOK_CONFIG = "webhookConfig"
object STRINGS {
const val ONLINE = "strings.online"
const val OFFLINE = "strings.offline"
const val JOINED = "strings.joined"
const val LEFT = "strings.left"
}
object COMMANDS {
const val TIME = "commands.time"
const val ONLINE = "commands.online"
}
const val TELEGRAM_MESSAGE_FORMAT = "telegramMessageFormat"
}
object DEFS {
const val logFromMCtoTG = false
const val logFromTGtoMC = false
const val logJoinLeave = false
const val logPlayerDeath = false
const val logPlayerAsleep = false
object COMMANDS {
const val TIME = "time"
const val ONLINE = "online"
}
const val playersOnline = "Online"
const val nobodyOnline = "Nobody online"
const val playerJoined = "joined"
const val playerLeft = "left"
const val useWebhook = false
const val enable = true
const val telegramMessageFormat = "<%username%>: %message%"
}
object WARN {
const val noConfigWarning = "No config file found! Writing default config to config.yml."
const val noToken = "Bot token must be defined."
const val noUsername = "Bot username must be defined."
}
object INFO {
const val reloading = "Reloading plugin... This may take some time"
const val reloadComplete = "Reload completed."
}
object TIMES_OF_DAY {
const val day = "\uD83C\uDFDE Day"
const val sunset = "\uD83C\uDF06 Sunset"
Expand All @@ -60,4 +19,7 @@ object Constants {
}
const val USERNAME_PLACEHOLDER = "%username%"
const val MESSAGE_TEXT_PLACEHOLDER = "%message%"
object COMMANDS {
const val PLUGIN_RELOAD = "tgbridge_reload"
}
}
38 changes: 13 additions & 25 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,15 @@ 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

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)
}
}
class EventHandler(
private val plugin: Plugin,
private val config: Configuration
) : Listener {

@EventHandler
fun onPlayerChat(event: AsyncPlayerChatEvent) {
if (plugin.chatToTG) {
if (config.logFromMCtoTG) {
plugin.tgBot.sendMessageToTGFrom(
event.player.displayName, event.message
)
Expand All @@ -38,21 +24,23 @@ class EventHandler(private val plugin: Plugin) : Listener {

@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {
if (!logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> $joinStr."
if (!config.logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
"${config.joinString}."
plugin.tgBot.broadcastToTG(text)
}

@EventHandler
fun onPlayerLeave(event: PlayerQuitEvent) {
if (!logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> $leftStr."
if (!config.logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
"${config.leaveString}."
plugin.tgBot.broadcastToTG(text)
}

@EventHandler
fun onPlayerDied(event: PlayerDeathEvent) {
if (!logDeathMessage) return
if (!config.logDeath) return
event.deathMessage?.let {
val plName = event.entity.displayName
val text = it.replace(plName, "<b>$plName</b>")
Expand All @@ -62,7 +50,7 @@ class EventHandler(private val plugin: Plugin) : Listener {

@EventHandler
fun onPlayerAsleep(event: PlayerBedEnterEvent) {
if (!logPlayerAsleep) return
if (!config.logPlayerAsleep) return
if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return
val text = "<b>${event.player.displayName}</b> fell asleep."
plugin.tgBot.broadcastToTG(text)
Expand Down
Loading

0 comments on commit e98acd5

Please sign in to comment.