Skip to content

Commit

Permalink
Replace incorrect calls to replaceAll. Fixes manuelgu#33
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCRic committed Dec 22, 2018
1 parent b8db037 commit 19732f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/main/java/eu/manuelgu/discordmc/MessageAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))));
}

/**
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/eu/manuelgu/discordmc/listener/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}


Expand All @@ -71,7 +73,7 @@ public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
List<IUser> 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() + ">");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 19732f3

Please sign in to comment.