From 840f4a86f8443b087f39674c8f3d69bbf2548b4a Mon Sep 17 00:00:00 2001 From: ceikry Date: Tue, 21 Feb 2023 12:46:17 -0600 Subject: [PATCH] Global chat work --- Server/src/main/core/ServerConstants.kt | 3 +++ .../game/system/communication/GlobalChat.kt | 19 ++++++++++++++++++ .../game/system/config/ServerConfigParser.kt | 1 + .../main/core/net/packet/PacketProcessor.kt | 7 ++++++- Server/src/main/core/tools/Globals.kt | 20 +++++++++++-------- 5 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 Server/src/main/core/game/system/communication/GlobalChat.kt diff --git a/Server/src/main/core/ServerConstants.kt b/Server/src/main/core/ServerConstants.kt index 5823be465..d060ff29a 100644 --- a/Server/src/main/core/ServerConstants.kt +++ b/Server/src/main/core/ServerConstants.kt @@ -264,5 +264,8 @@ class ServerConstants { @JvmField var DRAGON_AXE_USE_OSRS_SPEC = false + + @JvmField + var ENABLE_GLOBALCHAT = false } } diff --git a/Server/src/main/core/game/system/communication/GlobalChat.kt b/Server/src/main/core/game/system/communication/GlobalChat.kt new file mode 100644 index 000000000..bff657636 --- /dev/null +++ b/Server/src/main/core/game/system/communication/GlobalChat.kt @@ -0,0 +1,19 @@ +package core.game.system.communication + +import core.api.getAttribute +import core.api.sendMessage +import core.game.world.repository.Repository +import core.tools.colorize + +object GlobalChat { + private val ATTR_GLOBAL_MUTE = "/save:globalmute" + fun process(sender: String, message: String) { + val msg = prepare(sender, message) + for (player in Repository.players.filter { !getAttribute(it, ATTR_GLOBAL_MUTE, false) }) + sendMessage(player, msg) + } + + private fun prepare(sender: String, message: String) : String { + return colorize("%44e3e0[%GG%44e3e0] %ffffff$sender: $message") + } +} \ No newline at end of file diff --git a/Server/src/main/core/game/system/config/ServerConfigParser.kt b/Server/src/main/core/game/system/config/ServerConfigParser.kt index a486db759..413e55cf7 100644 --- a/Server/src/main/core/game/system/config/ServerConfigParser.kt +++ b/Server/src/main/core/game/system/config/ServerConfigParser.kt @@ -133,6 +133,7 @@ object ServerConfigParser { ServerConstants.DISCORD_MOD_WEBHOOK = data.getString("server.moderation_webhook", "") ServerConstants.NOAUTH_DEFAULT_ADMIN = data.getBoolean("server.noauth_default_admin", false) ServerConstants.DRAGON_AXE_USE_OSRS_SPEC = data.getBoolean("world.dragon_axe_use_osrs_spec", false) + ServerConstants.ENABLE_GLOBALCHAT = data.getBoolean("world.enable_globalchat", true) } diff --git a/Server/src/main/core/net/packet/PacketProcessor.kt b/Server/src/main/core/net/packet/PacketProcessor.kt index 0ba9d3494..9b59eafe2 100644 --- a/Server/src/main/core/net/packet/PacketProcessor.kt +++ b/Server/src/main/core/net/packet/PacketProcessor.kt @@ -55,6 +55,7 @@ import core.game.node.entity.player.info.LogType import core.game.node.entity.player.info.PlayerMonitor import core.tools.SystemLogger import core.game.system.command.CommandSystem +import core.game.system.communication.GlobalChat import core.game.world.GameWorld import core.game.world.repository.Repository import core.net.packet.`in`.Packet @@ -202,7 +203,11 @@ object PacketProcessor { if (pkt.player.details.isMuted) pkt.player.sendMessage("You have been muted due to breaking a rule.") else { - if (pkt.message.startsWith("/") && pkt.player.communication.clan != null) { + if (ServerConstants.ENABLE_GLOBALCHAT && pkt.message.startsWith("//")) { + GlobalChat.process(pkt.player.username, pkt.message.substring(2)) + return + } + else if (pkt.message.startsWith("/") && pkt.player.communication.clan != null) { val builder = ClanMessage.newBuilder() builder.sender = pkt.player.name builder.clanName = pkt.player.communication.clan.owner.lowercase().replace(" ", "_") diff --git a/Server/src/main/core/tools/Globals.kt b/Server/src/main/core/tools/Globals.kt index ed2f85f40..cb070f762 100644 --- a/Server/src/main/core/tools/Globals.kt +++ b/Server/src/main/core/tools/Globals.kt @@ -7,14 +7,18 @@ const val GREEN = "" const val BLUE = "" const val PURPLE = "" -fun colorize(line: String): String{ - val new = line.replace("%R", RED) - .replace("%O", ORANGE) - .replace("%Y", YELLOW) - .replace("%G", GREEN) - .replace("%B", BLUE) - .replace("%P", PURPLE).append("") + " " - return new +private val pattern = Regex("%[0-9a-fA-F]{6}") +private val testData = arrayOf("This is a string with no colors.", "This %R is a string with one color.", "This %R %G %B is a string with multiple colors.", "This %ffffff is an arbitrary hex string.") + +fun colorize(line: String): String { + return line.replace("%R", RED) + .replace("%O", ORANGE) + .replace("%Y", YELLOW) + .replace("%G", GREEN) + .replace("%B", BLUE) + .replace("%P", PURPLE) + .replace(pattern) { matchResult -> "" } + .append("") + " " } fun colorize(line: String, hexColor: String): String{