From ee0867d5cca2d5440681b58841d70fbb72861d47 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Sat, 5 Oct 2019 04:22:50 +0200 Subject: [PATCH] Add escape for Discord syntax (#41) --- .../listeners/AsyncPlayerChatListener.java | 59 ++++++++++++------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java index 08c7e2d..ebd3849 100644 --- a/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java +++ b/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java @@ -45,30 +45,45 @@ public class AsyncPlayerChatListener implements Listener { // Discord syntax for font emphasis if (player.hasPermission("qol.discord")) { String chat = event.getMessage(); - Pattern syntax; - Matcher matcher; - for (int i = 0; i < discordSyntax.size(); i++) { - syntax = discordSyntax.get(i); - matcher = syntax.matcher(chat); - switch (i) { - case 0: - chat = matcher.replaceAll("&n$1&r"); - break; - case 1: - case 3: - chat = matcher.replaceAll("&o$1&r"); - break; - case 2: - chat = matcher.replaceAll("&l$1&r"); - break; - case 4: - chat = matcher.replaceAll("&m$1&r"); - break; - default: - break; + boolean escape = false; + + // Escape + if (chat.startsWith("\\")) { + escape = true; + event.setMessage(chat.substring(1)); + + // Escape the escape, this monster wants to start their message with a backslash + if (chat.startsWith("\\\\")) { + escape = false; } } - event.setMessage(ChatColor.translateAlternateColorCodes('&', chat)); + + if (!escape) { + Pattern syntax; + Matcher matcher; + for (int i = 0; i < discordSyntax.size(); i++) { + syntax = discordSyntax.get(i); + matcher = syntax.matcher(chat); + switch (i) { + case 0: + chat = matcher.replaceAll("&n$1&r"); + break; + case 1: + case 3: + chat = matcher.replaceAll("&o$1&r"); + break; + case 2: + chat = matcher.replaceAll("&l$1&r"); + break; + case 4: + chat = matcher.replaceAll("&m$1&r"); + break; + default: + break; + } + } + event.setMessage(ChatColor.translateAlternateColorCodes('&', chat)); + } } }