Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

fix: do not crash with broken chat mention regex #677

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ChatConfig extends SettingsClass {
@Setting(displayName = "Chat Mentions", description = "Should a sound play when your username is mentioned in a message?", order = 3)
public boolean allowChatMentions = true;

@Setting(displayName = "Chat Mentions Nicknames", description = "Besides your username, what other names should trigger Chat Mentions?\n\n§8Multiple nicknames can be added by using commas as separators.", order = 4)
@Setting(displayName = "Chat Mentions Nicknames", description = "Besides your username, what other names should trigger Chat Mentions?\n\n§8Multiple nicknames can be added by using commas as separators. This is a wrapped RegEx field.", order = 4)
public String mentionNames = "";

@Setting(displayName = "Chat Spam Filter", description = "Should repeating messages stack?", order = 7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

public class ChatManager {
Expand Down Expand Up @@ -88,6 +89,8 @@ public class ChatManager {

private static boolean discoveriesLoaded = false;

private static String failedRegex = "";

public static Pair<ITextComponent, Pair<Supplier<Boolean>, Function<ITextComponent, ITextComponent>>> processRealMessage(ITextComponent in) {
if (in == null) return new Pair<>(in, null);
ITextComponent original = in.createCopy();
Expand Down Expand Up @@ -625,7 +628,23 @@ public static ITextComponent renderMessage(ITextComponent in) {
public static boolean processUserMention(ITextComponent in, ITextComponent original) {
if (ChatConfig.INSTANCE.allowChatMentions && in != null && McIf.player() != null) {
String match = "\\b(" + McIf.player().getName() + (ChatConfig.INSTANCE.mentionNames.length() > 0 ? "|" + ChatConfig.INSTANCE.mentionNames.replace(",", "|") : "") + ")\\b";
Pattern pattern = Pattern.compile(match, Pattern.CASE_INSENSITIVE);
Pattern pattern;
try {
pattern = Pattern.compile(match, Pattern.CASE_INSENSITIVE);
if (!failedRegex.equals("")) {
failedRegex = "";
}
} catch (PatternSyntaxException e) {
if (failedRegex.equals("") || !ChatConfig.INSTANCE.mentionNames.equals(failedRegex)) {
// only run these once per config change
failedRegex = ChatConfig.INSTANCE.mentionNames;
// Also maybe notify the player
McIf.player().sendMessage(new TextComponentString("Wynntils custom mention regex failed, check your config! Reverting to default username mentions."));
McIf.player().sendMessage(new TextComponentString(e.getMessage()));
}
// Failed manual regexes will be ignored, only check the playername
pattern = Pattern.compile("\\b(" + McIf.player().getName() + ")\\b", Pattern.CASE_INSENSITIVE);
}

Matcher looseMatcher = pattern.matcher(McIf.getUnformattedText(in));

Expand Down