WoWInterface - View Single Post - Code optimization and issue with hyperlinks in chat
View Single Post
10-11-18, 06:50 PM   #1
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
Code optimization and issue with hyperlinks in chat

Good evening everyone,

I'm playing around with how the chat appears. I've learned how incredibly complex the chat frame is, but I'm running into an issue I can't yet figure out.

The following code modifies how the chat appears to my liking. It works in all regards except one, if I link an item or achievement or someother hyperlink into chat, and have string(s) after the hyperlink, the color of the strings after the hyperlink turns white.

To put this in perspective, I would type "Wow look at this item: (shift click item). It's got amazing stats." and this would appear: Wow look at this item: [Sick DPS Weapon]. It's got amazing stats.

I can't for the life of me figure out why a hyperlink would cause an escape from the color sequence, or how I could get around it. If anyone has a suggestion on how to fix this, or sees a way to better optimize my code, please let me know:

Lua Code:
  1. local function RGBPercToHex(r, g, b)
  2.     r = r <= 1 and r >= 0 and r or 0
  3.     g = g <= 1 and g >= 0 and g or 0
  4.     b = b <= 1 and b >= 0 and b or 0
  5.     return string.format('%02x%02x%02x', r*255, g*255, b*255)
  6. end
  8. ChatFrame_AddMessageEventFilter('CHAT_MSG_CHANNEL', function() return true end)
  9. local f = CreateFrame('frame')
  10. f:RegisterEvent('CHAT_MSG_CHANNEL')
  11. f:SetScript('OnEvent', function(self, event, ...)
  12.     local type = strsub(event, 10)
  13.     local info = ChatTypeInfo[type]
  14.     local timeStamp
  15.     local chatMessage, sender, senderLanguage, fullChannelName, _, senderFlags, _, channelNumber, _, _, chatLineID, senderGUID = ...
  16.     local senderLinkDisplay = GetColoredName(event, ...)
  17.     local playerLink = GetPlayerLink(sender, senderLinkDisplay, chatLineID, Chat_GetChatCategory(type), tostring(channelNumber))
  19.     --first we get the timestamp with time(), then we format it with blizz's style with BetterDate
  20.     --then we change the text color to a grey and add brackets and finally remove the annoying space that gets between the string and ]
  21.     timeStamp = gsub(string.format('|cff'..RGBPercToHex(0.5, 0.5, 0.5)..'[%s]|r', BetterDate(CHAT_TIMESTAMP_FORMAT, time())), ' ', '')
  23.     --properly color our channel number and following .
  24.     channelNumber = string.format('|cff'..RGBPercToHex(info.r, info.g, info.b)..'%s|r', channelNumber)
  26.     --properly color our chat message in whatever channel it is
  27.     chatMessage = string.format('|cff'..RGBPercToHex(info.r, info.g, info.b)..'%s|r', chatMessage)
  29.     --this block of code will search the chat for mentions of character's full name and change it to their class color
  30.     local test = chatMessage:gsub('[^a-zA-Z%s]', '')
  31.     local words = {strsplit(' ', test)}
  32.     for i = 1, #words do
  33.         --since we technically just changed our sentence or word to ff000000wordr or ff000000this is my sentencer
  34.         --we need to truncate the last word's trailing r and first word's hexcode
  35.         if i == 1 then
  36.             words[i] = strsub(words[i], 8) --cut off the opening timestamp color hexcode
  37.             if i == #words then
  38.                 words[i] = words[i]:sub(1, -1) --a one word message with a player name was said, now cut off the closing r, too
  39.             end
  40.         elseif i == #words then
  41.             words[i] = words[i]:sub(1, -1) --cut off the closing r
  42.         end
  43.         local w = words[i]
  44.         if (w and not (w == 'player' or w == 'target') and UnitName(w) and UnitIsPlayer(w)) then
  45.             local _, class = UnitClass(w)
  46.             local colors = RAID_CLASS_COLORS[class]
  47.             if (colors) then --replace the words that match player names with class colors, return back to the channel color for the next word(s)
  48.                 chatMessage = gsub(chatMessage, w, '|cff'..RGBPercToHex(colors.r, colors.g, colors.b)..'%1|r|cff'..RGBPercToHex(info.r, info.g, info.b))
  49.             end
  50.         end
  51.     end
  52.     chatMessage = timeStamp..' '..channelNumber..'. '..playerLink..': '..chatMessage
  54.     ChatFrame1:AddMessage(chatMessage)
  55. end)

Last edited by Terenna : 10-11-18 at 07:36 PM.
  Reply With Quote