chat notifier: fix highlight own name matching player names with spaces

We internally rewrite nbsp to space in Player.getName(), but the in-game
drop messages which include players names still have nbsp. Modify the
generated username matcher pattern to match either nbsp or space.
This commit is contained in:
Adam
2020-04-30 14:57:53 -04:00
parent f9ad57625c
commit f60d818558
2 changed files with 42 additions and 3 deletions

View File

@@ -28,16 +28,15 @@ package net.runelite.client.plugins.chatnotifications;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.inject.Provides;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.quote;
import java.util.stream.Collectors;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.MessageNode;
import net.runelite.api.events.ChatMessage;
import net.runelite.client.events.ConfigChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.Notifier;
import net.runelite.client.RuneLiteProperties;
@@ -45,6 +44,7 @@ import net.runelite.client.chat.ChatColorType;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.util.Text;
@@ -156,7 +156,10 @@ public class ChatNotificationsPlugin extends Plugin
if (usernameMatcher == null && client.getLocalPlayer() != null && client.getLocalPlayer().getName() != null)
{
String username = client.getLocalPlayer().getName();
usernameMatcher = Pattern.compile("\\b(" + quote(username) + ")\\b", Pattern.CASE_INSENSITIVE);
String pattern = Arrays.stream(username.split(" "))
.map(s -> s.isEmpty() ? "" : Pattern.quote(s))
.collect(Collectors.joining("[\u00a0\u0020]")); // space or nbsp
usernameMatcher = Pattern.compile("\\b" + pattern + "\\b", Pattern.CASE_INSENSITIVE);
usernameReplacer = "<col" + ChatColorType.HIGHLIGHT.name() + "><u>" + username + "</u><col" + ChatColorType.NORMAL.name() + ">";
}

View File

@@ -33,6 +33,7 @@ import javax.inject.Inject;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.MessageNode;
import net.runelite.api.Player;
import net.runelite.api.events.ChatMessage;
import net.runelite.client.Notifier;
import net.runelite.client.chat.ChatMessageManager;
@@ -226,4 +227,39 @@ public class ChatNotificationsPluginTest
{
assertEquals("you. It", ChatNotificationsPlugin.stripColor("you. <col=ff0000>It"));
}
@Test
public void testHighlightOwnName()
{
Player player = mock(Player.class);
when(player.getName()).thenReturn("Logic Knot");
when(client.getLocalPlayer()).thenReturn(player);
when(config.highlightOwnName()).thenReturn(true);
MessageNode messageNode = mock(MessageNode.class);
when(messageNode.getValue()).thenReturn("<col=005f00>Logic Knot received a drop: Adamant longsword</col>");
ChatMessage chatMessage = new ChatMessage(messageNode, ChatMessageType.GAMEMESSAGE, "", "", "", 0);
chatNotificationsPlugin.onChatMessage(chatMessage);
verify(messageNode).setValue("<col=005f00><colHIGHLIGHT><u>Logic Knot</u><colNORMAL> received a drop: Adamant longsword</col>");
}
@Test
public void testHighlightOwnNameNbsp()
{
Player player = mock(Player.class);
when(player.getName()).thenReturn("Logic Knot");
when(client.getLocalPlayer()).thenReturn(player);
when(config.highlightOwnName()).thenReturn(true);
MessageNode messageNode = mock(MessageNode.class);
when(messageNode.getValue()).thenReturn("<col=005f00>Logic\u00a0Knot received a drop: Adamant longsword</col>");
ChatMessage chatMessage = new ChatMessage(messageNode, ChatMessageType.GAMEMESSAGE, "", "", "", 0);
chatNotificationsPlugin.onChatMessage(chatMessage);
// set value uses our player name, which has nbsp replaced
verify(messageNode).setValue("<col=005f00><colHIGHLIGHT><u>Logic Knot</u><colNORMAL> received a drop: Adamant longsword</col>");
}
}