chat message manager: format rl-messages at chat build time

This allows different final messages being built for split chat vs
normal chat, fixing <colNORMAL> incorrectly applying the default chatbox
color to split chat.
This commit is contained in:
Adam
2022-01-11 23:50:53 -05:00
parent 9ff0538cf8
commit 5960a2f43e
14 changed files with 63 additions and 128 deletions

View File

@@ -31,9 +31,7 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -45,9 +43,7 @@ import net.runelite.api.MessageNode;
import net.runelite.api.Player;
import net.runelite.api.VarPlayer;
import net.runelite.api.Varbits;
import net.runelite.api.events.ResizeableChanged;
import net.runelite.api.events.ScriptCallbackEvent;
import net.runelite.api.events.VarbitChanged;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ChatColorConfig;
import net.runelite.client.eventbus.EventBus;
@@ -66,7 +62,6 @@ public class ChatMessageManager
private final Client client;
private final ChatColorConfig chatColorConfig;
private final ClientThread clientThread;
private int transparencyVarbit = -1;
private final Queue<QueuedMessage> queuedMessages = new ConcurrentLinkedQueue<>();
@Inject
@@ -83,40 +78,25 @@ public class ChatMessageManager
loadColors();
}
@Subscribe
public void onVarbitChanged(VarbitChanged event)
{
int setting = client.getVar(Varbits.TRANSPARENT_CHATBOX);
if (transparencyVarbit != setting)
{
transparencyVarbit = setting;
refreshAll();
}
}
@Subscribe
public void onResizeableChanged(ResizeableChanged event)
{
refreshAll();
}
@Subscribe
public void onConfigChanged(ConfigChanged event)
{
if (event.getGroup().equals("textrecolor"))
{
loadColors();
clientThread.invokeLater(this::refreshAll);
clientThread.invokeLater(client::refreshChat);
}
}
@VisibleForTesting
void colorChatMessage()
{
final int[] intStack = client.getIntStack();
final String[] stringStack = client.getStringStack();
final int size = client.getStringStackSize();
final int uid = client.getIntStack()[client.getIntStackSize() - 1];
final int isize = client.getIntStackSize();
final int uid = intStack[isize - 1];
final boolean splitpmbox = intStack[isize - 2] == 1;
final MessageNode messageNode = client.getMessages().get(uid);
assert messageNode != null : "chat message build for unknown message";
@@ -184,6 +164,12 @@ public class ChatMessageManager
stringStack[size - 4] = ColorUtil.wrapWithColorTag(channel, channelColor);
}
if (messageNode.getRuneLiteFormatMessage() != null)
{
stringStack[size - 2] = message = formatRuneLiteMessage(messageNode.getRuneLiteFormatMessage(),
chatMessageType, splitpmbox);
}
final Collection<ChatColor> chatColors = colorCache.get(chatMessageType);
for (ChatColor chatColor : chatColors)
{
@@ -824,13 +810,11 @@ public class ChatMessageManager
return;
}
final String formattedMessage = formatRuneLiteMessage(message.getRuneLiteFormattedMessage(), message.getType());
// this updates chat cycle
final MessageNode line = client.addChatMessage(
message.getType(),
MoreObjects.firstNonNull(message.getName(), ""),
MoreObjects.firstNonNull(formattedMessage, message.getValue()),
MoreObjects.firstNonNull(message.getRuneLiteFormattedMessage(), message.getValue()),
message.getSender());
// Update the message with RuneLite additions
@@ -843,64 +827,64 @@ public class ChatMessageManager
}
/**
* Rebuild the message node message from the RuneLite format message
* Rebuild the message node message from the RuneLite format message.
* DEPRECATED: no longer needs to be called.
*
* @param messageNode message node
*/
@Deprecated
public void update(final MessageNode messageNode)
{
String message = formatRuneLiteMessage(messageNode.getRuneLiteFormatMessage(), messageNode.getType());
if (message != null)
{
messageNode.setValue(message);
}
}
private String formatRuneLiteMessage(String runeLiteFormatMessage, ChatMessageType type)
@VisibleForTesting
String formatRuneLiteMessage(String runeLiteFormatMessage, ChatMessageType type, boolean pmbox)
{
if (Strings.isNullOrEmpty(runeLiteFormatMessage))
{
return null;
}
final boolean transparent = client.isResized() && transparencyVarbit != 0;
final boolean transparentChatbox = client.getVar(Varbits.TRANSPARENT_CHATBOX) != 0;
final boolean transparent = client.isResized() && transparentChatbox;
final Collection<ChatColor> chatColors = colorCache.get(type);
for (ChatColor chatColor : chatColors)
{
if (chatColor.isTransparent() == transparent)
{
Color color = chatColor.getColor();
String colstr;
VarPlayer varp = chatColor.getSetting();
if (varp != null)
if (pmbox && chatColor.getType() == ChatColorType.NORMAL)
{
// Apply configured color from game settings, if set
assert chatColor.isDefault();
int v = client.getVar(varp);
if (v != 0)
// The default ChatColors for private have the chatbox text color, not the split chat color,
// and the split chat color is set by widget color, so just use </col>. The in-game
// private chat color doesn't apply to split chat either so using that here also is incorrect.
//
// If we recolor the final message later we replace </col> with the desired color in
// colorChatMessage()
colstr = ColorUtil.CLOSING_COLOR_TAG;
}
else
{
Color color = chatColor.getColor();
VarPlayer varp = chatColor.getSetting();
if (varp != null)
{
color = new Color(v - 1);
// Apply configured color from game settings, if set
assert chatColor.isDefault();
int v = client.getVar(varp);
if (v != 0)
{
color = new Color(v - 1);
}
}
colstr = ColorUtil.colorTag(color);
}
// Replace custom formatting with actual colors
runeLiteFormatMessage = runeLiteFormatMessage.replaceAll(
"<col" + chatColor.getType().name() + ">",
ColorUtil.colorTag(color));
colstr);
}
}
return runeLiteFormatMessage;
}
private void refreshAll()
{
client.getChatLineMap().values().stream()
.filter(Objects::nonNull)
.flatMap(clb -> Arrays.stream(clb.getLines()))
.filter(Objects::nonNull)
.forEach(this::update);
client.refreshChat();
}
}

View File

@@ -77,7 +77,6 @@ import net.runelite.client.chat.ChatClient;
import net.runelite.client.chat.ChatColorType;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageBuilder;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.eventbus.Subscribe;
@@ -175,9 +174,6 @@ public class ChatCommandsPlugin extends Plugin
@Inject
private ItemManager itemManager;
@Inject
private ChatMessageManager chatMessageManager;
@Inject
private ChatCommandManager chatCommandManager;
@@ -840,7 +836,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -930,7 +925,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -974,7 +968,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -1061,7 +1054,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -1137,7 +1129,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -1231,7 +1222,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
@@ -1321,7 +1311,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
}
@@ -1403,7 +1392,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
catch (IOException ex)
@@ -1488,7 +1476,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
catch (IOException ex)
@@ -1616,7 +1603,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
catch (IOException ex)
@@ -1710,7 +1696,6 @@ public class ChatCommandsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}
catch (IOException ex)

View File

@@ -47,7 +47,6 @@ import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.Notifier;
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;
@@ -69,9 +68,6 @@ public class ChatNotificationsPlugin extends Plugin
@Inject
private ChatNotificationsConfig config;
@Inject
private ChatMessageManager chatMessageManager;
@Inject
private Notifier notifier;
@@ -324,7 +320,6 @@ public class ChatNotificationsPlugin extends Plugin
if (update)
{
messageNode.setRuneLiteFormatMessage(messageNode.getValue());
chatMessageManager.update(messageNode);
}
}

View File

@@ -830,7 +830,6 @@ public class RaidsPlugin extends Plugin
log.debug("Setting response {}", response);
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}

View File

@@ -79,7 +79,6 @@ import net.runelite.client.chat.ChatClient;
import net.runelite.client.chat.ChatColorType;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageBuilder;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ChatInput;
@@ -161,9 +160,6 @@ public class SlayerPlugin extends Plugin
@Inject
private TargetWeaknessOverlay targetWeaknessOverlay;
@Inject
private ChatMessageManager chatMessageManager;
@Inject
private ChatCommandManager chatCommandManager;
@@ -875,7 +871,6 @@ public class SlayerPlugin extends Plugin
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
chatMessageManager.update(messageNode);
client.refreshChat();
}

View File

@@ -69,7 +69,6 @@ import net.runelite.api.events.GameTick;
import net.runelite.api.events.ItemContainerChanged;
import net.runelite.api.events.VarbitChanged;
import net.runelite.client.Notifier;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin;
@@ -105,9 +104,6 @@ public class WintertodtPlugin extends Plugin
@Inject
private WintertodtConfig config;
@Inject
private ChatMessageManager chatMessageManager;
@Getter(AccessLevel.PACKAGE)
private WintertodtActivity currentActivity = WintertodtActivity.IDLE;
@@ -314,7 +310,6 @@ public class WintertodtPlugin extends Plugin
// Recolor message for damage notification
messageNode.setRuneLiteFormatMessage(ColorUtil.wrapWithColorTag(messageNode.getValue(), config.damageNotificationColor()));
chatMessageManager.update(messageNode);
client.refreshChat();
// all actions except woodcutting and idle are interrupted from damage

View File

@@ -35,7 +35,7 @@ public class JagexColors
* Colors of chat text when displayed on an opaque background.
*/
public static final Color CHAT_PUBLIC_TEXT_OPAQUE_BACKGROUND = Color.BLUE;
public static final Color CHAT_PRIVATE_MESSAGE_TEXT_OPAQUE_BACKGROUND = Color.CYAN;
public static final Color CHAT_PRIVATE_MESSAGE_TEXT_OPAQUE_BACKGROUND = new Color(0x7F0000); // in chatbox, not split chat
public static final Color CHAT_FC_TEXT_OPAQUE_BACKGROUND = new Color(127, 0, 0);
public static final Color CHAT_FC_NAME_OPAQUE_BACKGROUND = Color.BLUE;
public static final Color CHAT_GAME_EXAMINE_TEXT_OPAQUE_BACKGROUND = Color.BLACK;
@@ -45,7 +45,7 @@ public class JagexColors
* Colors of chat text when displayed on a transparent background.
*/
public static final Color CHAT_PUBLIC_TEXT_TRANSPARENT_BACKGROUND = new Color(144, 144, 255);
public static final Color CHAT_PRIVATE_MESSAGE_TEXT_TRANSPARENT_BACKGROUND = Color.CYAN;
public static final Color CHAT_PRIVATE_MESSAGE_TEXT_TRANSPARENT_BACKGROUND = new Color(0xBF2020); // in chatbox, not split chat
public static final Color CHAT_FC_TEXT_TRANSPARENT_BACKGROUND = new Color(239, 80, 80);
public static final Color CHAT_FC_NAME_TRANSPARENT_BACKGROUND = new Color(144, 112, 255);
public static final Color CHAT_GAME_EXAMINE_TEXT_TRANSPARENT_BACKGROUND = Color.WHITE;

View File

@@ -440,14 +440,16 @@ LABEL391:
if_icmpeq LABEL406
jump LABEL1751
LABEL406:
iconst 0 ; splitpmbox
iload 10 ; message uid
sload 17 ; message channel
sload 16 ; message name
sload 18 ; message
sload 21 ; message timestamp
sconst "chatMessageBuilding"
runelite_callback
runelite_callback
pop_int ; pop uid
pop_int ; splitpmbox
sstore 21 ; message timestamp
sstore 18 ; message
sstore 16 ; message name

View File

@@ -413,14 +413,16 @@ CHAT_FILTER:
if_icmpeq LABEL369 ; Check if we are building this message
jump LABEL555
LABEL369:
iconst 1 ; splitpmbox
iload 12 ; message uid
sconst "" ; message channel
sload 1 ; message name
sload 0 ; message
sload 2 ; message timestamp
sconst "chatMessageBuilding"
runelite_callback
runelite_callback
pop_int ; uid
pop_int ; splitpmbox
sstore 2 ; message timestamp
sstore 0 ; message
sstore 1 ; message name

View File

@@ -43,7 +43,6 @@ import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.anyLong;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
@@ -86,6 +85,7 @@ public class ChatMessageManagerTest
""
};
istack = new int[]{
0, // splitpmbox
1
};
when(client.getStringStack()).thenReturn(sstack);
@@ -183,12 +183,8 @@ public class ChatMessageManagerTest
.append("%)")
.build();
MessageNode messageNode = mock(MessageNode.class);
when(messageNode.getType()).thenReturn(ChatMessageType.FRIENDSCHATNOTIFICATION);
when(messageNode.getRuneLiteFormatMessage()).thenReturn(chatMessage);
String formattedMessage = chatMessageManager.formatRuneLiteMessage(chatMessage, ChatMessageType.FRIENDSCHATNOTIFICATION, false);
chatMessageManager.update(messageNode);
verify(messageNode).setValue("<col=000000>Total points: <col=ff0000>42<col=000000>, Personal points: <col=ff0000>43<col=000000> (<col=ff0000>44<col=000000>%)");
assertEquals("<col=000000>Total points: <col=ff0000>42<col=000000>, Personal points: <col=ff0000>43<col=000000> (<col=ff0000>44<col=000000>%)", formattedMessage);
}
}

View File

@@ -51,7 +51,6 @@ import static net.runelite.api.widgets.WidgetID.DIARY_QUEST_GROUP_ID;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.chat.ChatClient;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ChatColorConfig;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.config.RuneLiteConfig;
@@ -105,10 +104,6 @@ public class ChatCommandsPluginTest
@Bind
HiscoreClient hiscoreClient;
@Mock
@Bind
ChatMessageManager chatMessageManager;
@Mock
@Bind
ChatClient chatClient;

View File

@@ -37,14 +37,13 @@ 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;
import net.runelite.client.util.Text;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mock;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -63,10 +62,6 @@ public class ChatNotificationsPluginTest
@Bind
private ChatNotificationsConfig config;
@Mock
@Bind
private ChatMessageManager chatMessageManager;
@Mock
@Bind
private Notifier notifier;

View File

@@ -54,7 +54,6 @@ import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.Notifier;
import net.runelite.client.chat.ChatClient;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.game.ItemManager;
import net.runelite.client.game.npcoverlay.NpcOverlayService;
@@ -158,10 +157,6 @@ public class SlayerPluginTest
@Bind
Notifier notifier;
@Mock
@Bind
ChatMessageManager chatMessageManager;
@Mock
@Bind
ChatCommandManager chatCommandManager;
@@ -788,14 +783,15 @@ public class SlayerPluginTest
when(slayerConfig.taskCommand()).thenReturn(true);
when(chatClient.getTask(anyString())).thenReturn(task);
MessageNode messageNode = mock(MessageNode.class);
ChatMessage setMessage = new ChatMessage();
setMessage.setType(ChatMessageType.PUBLICCHAT);
setMessage.setName("Adam");
setMessage.setMessageNode(mock(MessageNode.class));
setMessage.setMessageNode(messageNode);
slayerPlugin.taskLookup(setMessage, "!task");
verify(chatMessageManager).update(any(MessageNode.class));
verify(messageNode).setRuneLiteFormatMessage(anyString());
}
@Test
@@ -810,14 +806,15 @@ public class SlayerPluginTest
when(slayerConfig.taskCommand()).thenReturn(true);
when(chatClient.getTask(anyString())).thenReturn(task);
MessageNode messageNode = mock(MessageNode.class);
ChatMessage chatMessage = new ChatMessage();
chatMessage.setType(ChatMessageType.PUBLICCHAT);
chatMessage.setName("Adam");
chatMessage.setMessageNode(mock(MessageNode.class));
chatMessage.setMessageNode(messageNode);
slayerPlugin.taskLookup(chatMessage, "!task");
verify(chatMessageManager, never()).update(any(MessageNode.class));
verify(messageNode, never()).setRuneLiteFormatMessage(anyString());
}
@Test

View File

@@ -33,7 +33,6 @@ import net.runelite.api.Client;
import net.runelite.api.Varbits;
import net.runelite.api.events.VarbitChanged;
import net.runelite.client.Notifier;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.ui.overlay.OverlayManager;
import org.junit.Before;
import org.junit.Test;
@@ -62,10 +61,6 @@ public class WintertodtPluginTest
@Bind
OverlayManager overlayManager;
@Mock
@Bind
ChatMessageManager chatMessageManager;
@Mock
@Bind
Notifier notifier;