From 19732f33c57e456f9b65cc85a12c249f9b66a40e Mon Sep 17 00:00:00 2001 From: ABCRic Date: Sat, 22 Dec 2018 02:57:47 +0000 Subject: [PATCH] Replace incorrect calls to replaceAll. Fixes manuelgu/DiscordMC#33 --- src/main/java/eu/manuelgu/discordmc/MessageAPI.java | 8 +++++--- .../discordmc/listener/BukkitEventListener.java | 4 ++-- .../eu/manuelgu/discordmc/listener/ChatListener.java | 10 ++++++---- .../discordmc/listener/DiscordEventListener.java | 6 +++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/manuelgu/discordmc/MessageAPI.java b/src/main/java/eu/manuelgu/discordmc/MessageAPI.java index 16e8230..b4cdf6a 100644 --- a/src/main/java/eu/manuelgu/discordmc/MessageAPI.java +++ b/src/main/java/eu/manuelgu/discordmc/MessageAPI.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Objects; +import java.util.regex.Matcher; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -33,7 +34,7 @@ public static void sendToMinecraft(IChannel origin, String username, String mess DiscordMC.getSubscribedPlayers().forEach(uuid -> Bukkit.getPlayer(uuid).sendMessage( EmojiParser.parseToAliases(formattedMessage - .replaceAll("%message", ChatColor.stripColor(message))))); + .replace("%message", ChatColor.stripColor(message))))); } /** @@ -47,7 +48,7 @@ public static void sendToMinecraftConsole(IChannel origin, String username, Stri String formattedMessage = getFormattedMessage(origin, username); Bukkit.getConsoleSender().sendMessage( - EmojiParser.parseToAliases(formattedMessage.replaceAll("%message", ChatColor.stripColor(message)))); + EmojiParser.parseToAliases(formattedMessage.replace("%message", ChatColor.stripColor(message)))); } private static String getFormattedMessage(final IChannel origin, final String username) { @@ -59,7 +60,8 @@ private static String getFormattedMessage(final IChannel origin, final String us .replace("%user", username) .replace("%channel", origin.getName())); } else { - formattedMessage = format.replace("%1$s", username).replace("%2$s", "%message"); + formattedMessage = format.replaceAll("%1$s", Matcher.quoteReplacement(username)) + .replaceAll("%2$s", "%message"); } return formattedMessage; } diff --git a/src/main/java/eu/manuelgu/discordmc/listener/BukkitEventListener.java b/src/main/java/eu/manuelgu/discordmc/listener/BukkitEventListener.java index 68a7b93..6876d3f 100644 --- a/src/main/java/eu/manuelgu/discordmc/listener/BukkitEventListener.java +++ b/src/main/java/eu/manuelgu/discordmc/listener/BukkitEventListener.java @@ -36,7 +36,7 @@ public void onPlayerJoin(PlayerJoinEvent event) { } final String username = event.getPlayer().getName(); final String formattedMessage = getPlugin().getConfig().getString("settings.templates.player_join_minecraft") - .replaceAll("%user", username); + .replace("%user", username); MessageAPI.sendToDiscord(formattedMessage); } @@ -54,7 +54,7 @@ public void onPlayerQuit(PlayerQuitEvent event) { } final String username = event.getPlayer().getName(); final String formattedMessage = getPlugin().getConfig().getString("settings.templates.player_leave_minecraft") - .replaceAll("%user", username); + .replace("%user", username); MessageAPI.sendToDiscord(formattedMessage); } diff --git a/src/main/java/eu/manuelgu/discordmc/listener/ChatListener.java b/src/main/java/eu/manuelgu/discordmc/listener/ChatListener.java index 22caa18..38c4812 100644 --- a/src/main/java/eu/manuelgu/discordmc/listener/ChatListener.java +++ b/src/main/java/eu/manuelgu/discordmc/listener/ChatListener.java @@ -14,6 +14,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.regex.Matcher; public class ChatListener implements Listener { @Getter @@ -55,10 +56,11 @@ public void onAsyncPlayerChat(AsyncPlayerChatEvent event) { String format = DiscordMC.getUserFormats().getString(username, "-"); if (!useIngameFormat || Objects.equals(format, "-")) { formattedMessage = getPlugin().getConfig().getString("settings.templates.chat_message_discord") - .replaceAll("%user", username) - .replaceAll("%message", message); + .replace("%user", username) + .replace("%message", message); } else { - formattedMessage = format.replace("%1$s", "**" + username + "**").replace("%2$s", message); + formattedMessage = format.replaceAll("%1$s", Matcher.quoteReplacement("**" + username + "**")) + .replaceAll("%2$s", Matcher.quoteReplacement(message)); } @@ -71,7 +73,7 @@ public void onAsyncPlayerChat(AsyncPlayerChatEvent event) { List users = DiscordMC.getClient().getGuilds().get(0).getUsersByName(s, true); if (!users.isEmpty()) { - formattedMessage = formattedMessage.replaceAll("@" + s, "<@" + users.get(0).getStringID() + ">"); + formattedMessage = formattedMessage.replace("@" + s, "<@" + users.get(0).getStringID() + ">"); } } } diff --git a/src/main/java/eu/manuelgu/discordmc/listener/DiscordEventListener.java b/src/main/java/eu/manuelgu/discordmc/listener/DiscordEventListener.java index d78a7d5..04a5949 100644 --- a/src/main/java/eu/manuelgu/discordmc/listener/DiscordEventListener.java +++ b/src/main/java/eu/manuelgu/discordmc/listener/DiscordEventListener.java @@ -67,16 +67,16 @@ public void userChat(MessageReceivedEvent event) { String id = u.getStringID(); // User name - content = content.replaceAll("<@" + id + ">", "@" + name); + content = content.replace("<@" + id + ">", "@" + name); // Nick name - content = content.replaceAll("<@!" + id + ">", "@" + name); + content = content.replace("<@!" + id + ">", "@" + name); } for (IRole r : roleMentions) { String roleName = r.getName(); String roleId = r.getStringID(); - content = content.replaceAll("<@&" + roleId + ">", "@" + roleName); + content = content.replace("<@&" + roleId + ">", "@" + roleName); } final String finalContent = content;