chat notifications: fix restoring last color on own name highlight

This was an issue with multiple name matches, since it was applying the
replacement with its close color to all matches, even though only
scanning the message for the last color up until the first match
This commit is contained in:
Adam
2022-01-11 21:30:18 -05:00
parent d5fe57865d
commit c7e6ffb4de

View File

@@ -243,12 +243,24 @@ public class ChatNotificationsPlugin extends Plugin
Matcher matcher = usernameMatcher.matcher(message);
if (matcher.find())
{
final int start = matcher.start();
final String username = client.getLocalPlayer().getName();
final String closeColor = MoreObjects.firstNonNull(getLastColor(message.substring(0, start)), "<col" + ChatColorType.NORMAL + '>');
final String replacement = "<col" + ChatColorType.HIGHLIGHT.name() + "><u>" + username + "</u>" + closeColor;
messageNode.setValue(matcher.replaceAll(replacement));
StringBuffer stringBuffer = new StringBuffer();
do
{
final int start = matcher.start();
final String closeColor = MoreObjects.firstNonNull(
getLastColor(message.substring(0, start)),
"<col" + ChatColorType.NORMAL + '>');
final String replacement = "<col" + ChatColorType.HIGHLIGHT.name() + "><u>" + username + "</u>" + closeColor;
matcher.appendReplacement(stringBuffer, replacement);
}
while (matcher.find());
matcher.appendTail(stringBuffer);
messageNode.setValue(stringBuffer.toString());
update = true;
if (config.notifyOnOwnName() && (chatMessage.getType() == ChatMessageType.PUBLICCHAT
|| chatMessage.getType() == ChatMessageType.PRIVATECHAT
|| chatMessage.getType() == ChatMessageType.FRIENDSCHAT