-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds ability to reload the plugin configuration and reconnect to Discord (fixes #15) - automatically reconnects to Discord on disconnect (fixes #9) - strips colors from messages when sending to Discord (fixes #4) - optionally sends death messages to Discord (fixes #12) - adds option to use display name in message templates (fixes #13) - adds option to use world name in message templates (fixes #14) - changes backing API library to fix changes on Discord's end (fixes #18)
- Loading branch information
1 parent
9d698f2
commit 818dc33
Showing
12 changed files
with
241 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/gg/obsidian/discordbridge/CommandHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package gg.obsidian.discordbridge | ||
|
||
import org.bukkit.ChatColor | ||
import org.bukkit.command.Command | ||
import org.bukkit.command.CommandExecutor | ||
import org.bukkit.command.CommandSender | ||
import org.bukkit.entity.Player | ||
|
||
class CommandHandler(val plugin: Plugin): CommandExecutor { | ||
|
||
override fun onCommand(player: CommandSender, cmd: Command, alias: String?, args: Array<out String>?): Boolean { | ||
if (player is Player && !Permissions.reload.has(player)) return true | ||
|
||
val isConsole = (player is Player) | ||
|
||
if (cmd.name != "discord") return true | ||
|
||
if (args == null || args.size != 1 || !args[0].equals("reload")) { | ||
sendMessage("&eUsage: /discord reload", player, isConsole) | ||
return true | ||
} | ||
|
||
sendMessage("&eReloading Discord Bridge...", player, isConsole) | ||
plugin.reload() | ||
return true | ||
} | ||
|
||
private fun sendMessage(message: String, player: CommandSender, isConsole: Boolean) { | ||
val formattedMessage = ChatColor.translateAlternateColorCodes('&', message) | ||
if (isConsole) { | ||
plugin.server.consoleSender.sendMessage(formattedMessage) | ||
} else { | ||
player.sendMessage(formattedMessage) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/kotlin/gg/obsidian/discordbridge/EventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package gg.obsidian.discordbridge | ||
|
||
import org.bukkit.ChatColor | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.EventPriority | ||
import org.bukkit.event.Listener | ||
import org.bukkit.event.entity.PlayerDeathEvent | ||
import org.bukkit.event.player.AsyncPlayerChatEvent | ||
import org.bukkit.event.player.PlayerJoinEvent | ||
import org.bukkit.event.player.PlayerQuitEvent | ||
|
||
class EventListener(val plugin: Plugin): Listener { | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
fun onChat(event: AsyncPlayerChatEvent) { | ||
plugin.logDebug("Received a chat event from ${event.player.name}: ${event.message}") | ||
if (!event.isCancelled || plugin.configuration.RELAY_CANCELLED_MESSAGES) { | ||
val username = ChatColor.stripColor(event.player.name) | ||
val formattedMessage = Util.formatMessage( | ||
plugin.configuration.TEMPLATES_DISCORD_CHAT_MESSAGE, | ||
mapOf( | ||
"%u" to username, | ||
"%m" to ChatColor.stripColor(event.message), | ||
"%d" to ChatColor.stripColor(event.player.displayName), | ||
"%w" to event.player.world.name | ||
) | ||
) | ||
|
||
plugin.sendToDiscord(formattedMessage) | ||
} | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
fun onPlayerJoin(event: PlayerJoinEvent) { | ||
if (!plugin.configuration.MESSAGES_JOIN) return | ||
|
||
val username = ChatColor.stripColor(event.player.name) | ||
plugin.logDebug("Received a join event for $username") | ||
|
||
val formattedMessage = Util.formatMessage( | ||
plugin.configuration.TEMPLATES_DISCORD_PLAYER_JOIN, | ||
mapOf( | ||
"%u" to username, | ||
"%d" to ChatColor.stripColor(event.player.displayName) | ||
) | ||
) | ||
|
||
plugin.sendToDiscord(formattedMessage) | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
fun onPlayerQuit(event: PlayerQuitEvent) { | ||
if (!plugin.configuration.MESSAGES_LEAVE) return | ||
|
||
val username = ChatColor.stripColor(event.player.name) | ||
plugin.logDebug("Received a leave event for $username") | ||
|
||
val formattedMessage = Util.formatMessage( | ||
plugin.configuration.TEMPLATES_DISCORD_PLAYER_LEAVE, | ||
mapOf( | ||
"%u" to username, | ||
"%d" to ChatColor.stripColor(event.player.displayName) | ||
) | ||
) | ||
|
||
plugin.sendToDiscord(formattedMessage) | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
fun onPlayerDeath(event: PlayerDeathEvent) { | ||
if (!plugin.configuration.MESSAGES_DEATH) return | ||
|
||
val username = ChatColor.stripColor(event.entity.name) | ||
plugin.logDebug("Received a death event for $username") | ||
|
||
val formattedMessage = Util.formatMessage( | ||
plugin.configuration.TEMPLATES_DISCORD_PLAYER_DEATH, | ||
mapOf( | ||
"%u" to username, | ||
"%d" to ChatColor.stripColor(event.entity.displayName), | ||
"%r" to event.deathMessage, | ||
"%w" to event.entity.world.name | ||
) | ||
) | ||
|
||
plugin.sendToDiscord(formattedMessage) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package gg.obsidian.discordbridge | ||
|
||
import org.bukkit.entity.Player | ||
|
||
enum class Permissions(val node: String) { | ||
reload("discordbridge.reload"); | ||
|
||
fun has(player: Player): Boolean { | ||
return player.hasPermission(node) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package gg.obsidian.discordbridge | ||
|
||
import org.bukkit.ChatColor | ||
|
||
object Util { | ||
fun formatMessage(message: String, replacements: Map<String, String>, colors: Boolean = false): String { | ||
var formattedString = message | ||
for ((token, replacement) in replacements) { | ||
formattedString = formattedString.replace(token, replacement) | ||
} | ||
|
||
if (colors) formattedString = ChatColor.translateAlternateColorCodes('&', formattedString) | ||
|
||
return formattedString | ||
} | ||
} |
Oops, something went wrong.