From 590726fde1758345a73e20c1126e52c24f4eade1 Mon Sep 17 00:00:00 2001 From: Ryan <57310593+DonkeyBlaster@users.noreply.github.com> Date: Sat, 16 Sep 2023 01:51:56 -0700 Subject: [PATCH] fix: do not crash with broken chat mention regex (#677) --- .../modules/chat/configs/ChatConfig.java | 2 +- .../modules/chat/managers/ChatManager.java | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/wynntils/modules/chat/configs/ChatConfig.java b/src/main/java/com/wynntils/modules/chat/configs/ChatConfig.java index ae84989cb..6bbe08605 100644 --- a/src/main/java/com/wynntils/modules/chat/configs/ChatConfig.java +++ b/src/main/java/com/wynntils/modules/chat/configs/ChatConfig.java @@ -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) diff --git a/src/main/java/com/wynntils/modules/chat/managers/ChatManager.java b/src/main/java/com/wynntils/modules/chat/managers/ChatManager.java index 222b2b67d..926a8b373 100644 --- a/src/main/java/com/wynntils/modules/chat/managers/ChatManager.java +++ b/src/main/java/com/wynntils/modules/chat/managers/ChatManager.java @@ -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 { @@ -88,6 +89,8 @@ public class ChatManager { private static boolean discoveriesLoaded = false; + private static String failedRegex = ""; + public static Pair, Function>> processRealMessage(ITextComponent in) { if (in == null) return new Pair<>(in, null); ITextComponent original = in.createCopy(); @@ -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));