chat notifier: restore original color when highlighting own name
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
package net.runelite.client.plugins.chatnotifications;
|
package net.runelite.client.plugins.chatnotifications;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.MoreObjects;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -76,7 +77,6 @@ public class ChatNotificationsPlugin extends Plugin
|
|||||||
|
|
||||||
//Custom Highlights
|
//Custom Highlights
|
||||||
private Pattern usernameMatcher = null;
|
private Pattern usernameMatcher = null;
|
||||||
private String usernameReplacer = "";
|
|
||||||
private Pattern highlightMatcher = null;
|
private Pattern highlightMatcher = null;
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@@ -181,15 +181,19 @@ public class ChatNotificationsPlugin extends Plugin
|
|||||||
.map(s -> s.isEmpty() ? "" : Pattern.quote(s))
|
.map(s -> s.isEmpty() ? "" : Pattern.quote(s))
|
||||||
.collect(Collectors.joining("[\u00a0\u0020]")); // space or nbsp
|
.collect(Collectors.joining("[\u00a0\u0020]")); // space or nbsp
|
||||||
usernameMatcher = Pattern.compile("\\b" + pattern + "\\b", Pattern.CASE_INSENSITIVE);
|
usernameMatcher = Pattern.compile("\\b" + pattern + "\\b", Pattern.CASE_INSENSITIVE);
|
||||||
usernameReplacer = "<col" + ChatColorType.HIGHLIGHT.name() + "><u>" + username + "</u><col" + ChatColorType.NORMAL.name() + ">";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.highlightOwnName() && usernameMatcher != null)
|
if (config.highlightOwnName() && usernameMatcher != null)
|
||||||
{
|
{
|
||||||
Matcher matcher = usernameMatcher.matcher(messageNode.getValue());
|
final String message = messageNode.getValue();
|
||||||
|
Matcher matcher = usernameMatcher.matcher(message);
|
||||||
if (matcher.find())
|
if (matcher.find())
|
||||||
{
|
{
|
||||||
messageNode.setValue(matcher.replaceAll(usernameReplacer));
|
final int start = matcher.start();
|
||||||
|
final String username = client.getLocalPlayer().getName();
|
||||||
|
final String closeColor = MoreObjects.firstNonNull(getLastColor(message.substring(0, start)), "</col>");
|
||||||
|
final String replacement = "<col" + ChatColorType.HIGHLIGHT.name() + "><u>" + username + "</u>" + closeColor;
|
||||||
|
messageNode.setValue(matcher.replaceAll(replacement));
|
||||||
update = true;
|
update = true;
|
||||||
if (config.notifyOnOwnName() && (chatMessage.getType() == ChatMessageType.PUBLICCHAT
|
if (config.notifyOnOwnName() && (chatMessage.getType() == ChatMessageType.PUBLICCHAT
|
||||||
|| chatMessage.getType() == ChatMessageType.PRIVATECHAT
|
|| chatMessage.getType() == ChatMessageType.PRIVATECHAT
|
||||||
|
|||||||
@@ -262,11 +262,11 @@ public class ChatNotificationsPluginTest
|
|||||||
when(config.highlightOwnName()).thenReturn(true);
|
when(config.highlightOwnName()).thenReturn(true);
|
||||||
|
|
||||||
MessageNode messageNode = mock(MessageNode.class);
|
MessageNode messageNode = mock(MessageNode.class);
|
||||||
when(messageNode.getValue()).thenReturn("<col=005f00>Logic Knot received a drop: Adamant longsword</col>");
|
when(messageNode.getValue()).thenReturn("Logic Knot received a drop: Adamant longsword");
|
||||||
ChatMessage chatMessage = new ChatMessage(messageNode, ChatMessageType.GAMEMESSAGE, "", "", "", 0);
|
ChatMessage chatMessage = new ChatMessage(messageNode, ChatMessageType.GAMEMESSAGE, "", "", "", 0);
|
||||||
chatNotificationsPlugin.onChatMessage(chatMessage);
|
chatNotificationsPlugin.onChatMessage(chatMessage);
|
||||||
|
|
||||||
verify(messageNode).setValue("<col=005f00><colHIGHLIGHT><u>Logic Knot</u><colNORMAL> received a drop: Adamant longsword</col>");
|
verify(messageNode).setValue("<colHIGHLIGHT><u>Logic Knot</u></col> received a drop: Adamant longsword");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -284,6 +284,6 @@ public class ChatNotificationsPluginTest
|
|||||||
chatNotificationsPlugin.onChatMessage(chatMessage);
|
chatNotificationsPlugin.onChatMessage(chatMessage);
|
||||||
|
|
||||||
// set value uses our player name, which has nbsp replaced
|
// 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>");
|
verify(messageNode).setValue("<col=005f00><colHIGHLIGHT><u>Logic Knot</u><col=005f00> received a drop: Adamant longsword</col>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user