I'm nearing completion on this addon, but there's a function that I feel is just not very optimized.
Lua Code:
local function hexColorNames(chatMessage, modChatColor) --this will churn through a message and find names that match players and color it by their class color
local words = {strsplit(' ', chatMessage)}
local name = UnitName('player')
local combinedName = (name..'-'..GetRealmName())
for i = 1, #words do
local w = words[i]
if (w and not (w == 'player' or w == 'target') and UnitName(w) and UnitIsPlayer(w)) then
local _, class = UnitClass(w)
local colors = RAID_CLASS_COLORS[class] --color the words that match player names with class colors, return back to the channel color for the next word(s)
if strfind(w, '-') then --this weird thing is because - is found in hyphenated names, - is also a magic character, if we simply use gsub, it never finds the full name because it gets stuck on '-'
local hyphenatedName = w --hold the old hyphenated name so we can replace it
local a, b = strsplit('-', w) --split the hyphenated name into two strings
w = a..'%-'..b --rejoin the strings with a % before the - so our gsub can actually look for it
chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..hyphenatedName..modChatColor)
else
chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..w..modChatColor)
end
elseif combinedName:lower() == w:lower() then --if for whatever reason someone types your name and realm unitname(w) AND unitisplayer(w) would be false for the whole name and realm
local _, class = UnitClass('player')
local colors = RAID_CLASS_COLORS[class]
local hyphenatedName = w --hold the old hyphenated name so we can replace it
local a, b = strsplit('-', w) --split the hyphenated name into two strings
w = a..'%-'..b --rejoin the strings with a % before the - so our gsub can actually look for it
chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..hyphenatedName..modChatColor)
end
end
return chatMessage
end
The goal of this function is to split any message you send to it by words. Since these chatMessage strings can have non-ascii characters via player names and the like, I just use the blizzard strsplit function to break the statement up into individual words. Each word is checked to see if it is a player's name, and if it is, it's colored by their class. I ran into particular issues with hyphenated names for off-realm players, since '-' is a magic character, typical gsub functions would get caught up with that. So I have to do a lot of string manipulation.
Furthering this issue, if you type in your full charactername-realmname, this returns false on your server, but true on a client off-server, so I had to write a second part to the if statement to get around this weird issue (if you don't understand what I'm saying, type /script print(UnitName('yourCharacterName-yourRealmName')) on your realm and on another realm. It will return false and true, respectively.
If you can see someway to better optimize this function, I'd greatly appreciate it. The function works through every scenario currently, I just don't know (but likely assume) if it can be written better. Thank you!