ChatNotificationsPlugin extends Plugin { @Subscribe public void onChatMessage(ChatMessage chatMessage) { MessageNode messageNode = chatMessage.getMessageNode(); boolean update = false; switch (chatMessage.getType()) { case TRADEREQ: if (chatMessage.getMessage().contains("wishes to trade with you.") && config.notifyOnTrade()) { notifier.notify(chatMessage.getMessage()); } break; case CHALREQ_TRADE: if (chatMessage.getMessage().contains("wishes to duel with you.") && config.notifyOnDuel()) { notifier.notify(chatMessage.getMessage()); } break; case BROADCAST: if (config.notifyOnBroadcast()) { String broadcast = chatMessage.getMessage(); int urlTokenIndex = broadcast.lastIndexOf('|'); if (urlTokenIndex != -1) { broadcast = broadcast.substring(0, urlTokenIndex); } notifier.notify(Text.removeFormattingTags(broadcast)); } break; case CONSOLE: if (chatMessage.getName().equals(RuneLiteProperties.getTitle())) { return; } break; } if (usernameMatcher == null && client.getLocalPlayer() != null && client.getLocalPlayer().getName() != null) { String username = client.getLocalPlayer().getName(); String pattern = Arrays.stream(username.split(" ")) .map(s -> s.isEmpty() ? "" : Pattern.quote(s)) .collect(Collectors.joining("[\u00a0\u0020]")); 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) { Matcher matcher = usernameMatcher.matcher(messageNode.getValue()); if (matcher.find()) { messageNode.setValue(matcher.replaceAll(usernameReplacer)); update = true; if (config.notifyOnOwnName() && (chatMessage.getType() == ChatMessageType.PUBLICCHAT || chatMessage.getType() == ChatMessageType.PRIVATECHAT || chatMessage.getType() == ChatMessageType.FRIENDSCHAT || chatMessage.getType() == ChatMessageType.MODCHAT || chatMessage.getType() == ChatMessageType.MODPRIVATECHAT)) { sendNotification(chatMessage); } } } if (highlightMatcher != null) { String nodeValue = messageNode.getValue(); Matcher matcher = highlightMatcher.matcher(nodeValue); boolean found = false; StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()) { String value = matcher.group(); String endColor = getLastColor(value); value = stripColor(value); matcher.appendReplacement(stringBuffer, "<col" + ChatColorType.HIGHLIGHT + '>' + value); if (endColor == null) { endColor = getLastColor(stringBuffer.toString()); } stringBuffer.append(endColor == null ? "<col" + ChatColorType.NORMAL + ">" : endColor); update = true; found = true; } if (found) { matcher.appendTail(stringBuffer); messageNode.setValue(stringBuffer.toString()); if (config.notifyOnHighlight()) { sendNotification(chatMessage); } } } if (update) { messageNode.setRuneLiteFormatMessage(messageNode.getValue()); chatMessageManager.update(messageNode); } }  @Override void startUp(); @Subscribe void onGameStateChanged(GameStateChanged event); @Subscribe void onConfigChanged(ConfigChanged event); @Subscribe void onChatMessage(ChatMessage chatMessage);  }
@Test public void onChatMessage() { when(config.highlightWordsString()).thenReturn("Deathbeam, Deathbeam OSRS , test"); MessageNode messageNode = mock(MessageNode.class); when(messageNode.getValue()).thenReturn("Deathbeam, Deathbeam OSRS"); ChatMessage chatMessage = new ChatMessage(); chatMessage.setType(ChatMessageType.PUBLICCHAT); chatMessage.setMessageNode(messageNode); chatNotificationsPlugin.startUp(); chatNotificationsPlugin.onChatMessage(chatMessage); verify(messageNode).setValue("<colHIGHLIGHT>Deathbeam<colNORMAL>, <colHIGHLIGHT>Deathbeam<colNORMAL> OSRS"); }
@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); verify(messageNode).setValue("<col=005f00><colHIGHLIGHT><u>Logic Knot</u><colNORMAL> received a drop: Adamant longsword</col>"); }