-
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.
Add message templates and join/leave messages
- Loading branch information
1 parent
27d8fe4
commit b96fdea
Showing
5 changed files
with
93 additions
and
31 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/gg/obsidian/discordbridge/Configuration.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,31 @@ | ||
package gg.obsidian.discordbridge | ||
|
||
class Configuration(val plugin: Plugin) { | ||
|
||
var SERVER_ID: String = "" | ||
var CHANNEL: String = "" | ||
var USERNAME: String = "" | ||
var EMAIL: String = "" | ||
var PASSWORD: String = "" | ||
var DEBUG: Boolean = false | ||
var TEMPLATES_DISCORD_CHAT_MESSAGE = "" | ||
var TEMPLATES_DISCORD_PLAYER_JOIN = "" | ||
var TEMPLATES_DISCORD_PLAYER_LEAVE = "" | ||
var TEMPLATES_MINECRAFT_CHAT_MESSAGE = "" | ||
|
||
fun load() { | ||
plugin.reloadConfig() | ||
|
||
SERVER_ID = plugin.config.getString("settings.server-id") | ||
CHANNEL = plugin.config.getString("settings.channel") | ||
USERNAME = plugin.config.getString("settings.username") | ||
EMAIL = plugin.config.getString("settings.email") | ||
PASSWORD = plugin.config.getString("settings.password") | ||
DEBUG = plugin.config.getBoolean("settings.debug", false) | ||
|
||
TEMPLATES_DISCORD_CHAT_MESSAGE = plugin.config.getString("settings.templates.discord.chat_message", "<%u> %m") | ||
TEMPLATES_DISCORD_PLAYER_JOIN = plugin.config.getString("settings.templates.discord.player_join", "%u joined the server") | ||
TEMPLATES_DISCORD_PLAYER_LEAVE = plugin.config.getString("settings.templates.discord.player_leave", "%u left the server") | ||
TEMPLATES_MINECRAFT_CHAT_MESSAGE = plugin.config.getString("settings.templates.minecraft.chat_message", "<%u&b(discord)&r> %m") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,85 @@ | ||
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.player.AsyncPlayerChatEvent | ||
import org.bukkit.event.player.PlayerJoinEvent | ||
import org.bukkit.event.player.PlayerQuitEvent | ||
import org.bukkit.plugin.java.JavaPlugin | ||
|
||
class Plugin : JavaPlugin(), Listener { | ||
|
||
var serverID: String = "" | ||
var channel: String = "" | ||
var username: String = "" | ||
var email: String = "" | ||
var password: String = "" | ||
var debug: Boolean = false | ||
|
||
val configuration = Configuration(this) | ||
var connection: DiscordConnection? = null | ||
|
||
override fun onEnable() { | ||
updateConfig(description.version) | ||
|
||
this.serverID = config.getString("settings.server-id") | ||
this.channel = config.getString("settings.channel") | ||
this.username = config.getString("settings.username") | ||
this.email = config.getString("settings.email") | ||
this.password = config.getString("settings.password") | ||
this.debug = config.getBoolean("settings.debug", false) | ||
|
||
this.connection = DiscordConnection(this) | ||
|
||
server.scheduler.runTaskAsynchronously(this, connection) | ||
server.pluginManager.registerEvents(this, this) | ||
} | ||
|
||
// Event Handlers | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
fun onChat(event: AsyncPlayerChatEvent) { | ||
logDebug("Received a chat event from ${event.player.name}: ${event.message}") | ||
send(event.player.name, event.message) | ||
val username = ChatColor.stripColor(event.player.name) | ||
val formattedMessage = configuration.TEMPLATES_DISCORD_CHAT_MESSAGE | ||
.replace("%u", username) | ||
.replace("%m", event.message) | ||
sendToDiscord(formattedMessage) | ||
} | ||
|
||
fun send(name: String, message: String) { | ||
logDebug("Sending chat message to Discord - $name: $message") | ||
connection!!.send(name, message) | ||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
fun onPlayerJoin(event: PlayerJoinEvent) { | ||
val username = ChatColor.stripColor(event.player.name) | ||
logDebug("Received a join event for $username") | ||
val formattedMessage = configuration.TEMPLATES_DISCORD_PLAYER_JOIN | ||
.replace("%u", username) | ||
sendToDiscord(formattedMessage) | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
fun onPlayerQuit(event: PlayerQuitEvent) { | ||
val username = ChatColor.stripColor(event.player.name) | ||
logDebug("Received a leave event for $username") | ||
val formattedMessage = configuration.TEMPLATES_DISCORD_PLAYER_LEAVE | ||
.replace("%u", username) | ||
sendToDiscord(formattedMessage) | ||
} | ||
|
||
// Message senders | ||
|
||
fun sendToDiscord(message: String) { | ||
logDebug("Sending message to Discord - $message") | ||
connection!!.send(message) | ||
} | ||
|
||
fun sendToMinecraft(username: String, message: String) { | ||
val formattedMessage = ChatColor.translateAlternateColorCodes('&', | ||
configuration.TEMPLATES_MINECRAFT_CHAT_MESSAGE | ||
.replace("%u", username) | ||
.replace("%m", message)) | ||
server.broadcastMessage(formattedMessage) | ||
} | ||
|
||
// Utilities | ||
|
||
fun updateConfig(version: String) { | ||
this.saveDefaultConfig() | ||
config.options().copyDefaults(true) | ||
config.set("version", version) | ||
saveConfig() | ||
configuration.load() | ||
} | ||
|
||
fun logDebug(msg: String) { | ||
if (!debug) return; | ||
if (!configuration.DEBUG) return; | ||
logger.info(msg) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,10 @@ settings: | |
email: '[email protected]' | ||
password: 'password' | ||
debug: false | ||
templates: | ||
discord: | ||
chat_message: '<%u> %m' | ||
player_join: '%u joined the server' | ||
player_leave: '%u left the server' | ||
minecraft: | ||
chat_message: '<%u&b(discord)&r> %m' |