The code currently does not work with off-realm people due to the nature of getName() stripping punctuation (- and ') from realm names, thereby leaving stripped with strings like nameKelthuzad which is then needed to be compared and obviously isn't a name. I'm working on fixing it now.
The code also needed to be edited slightly to function (not properly passing and handing off key arguments mainly). I edited the code as found here:
Lua Code:
local function getName(s) --Gets a name from the given string after stripping out possessives and punctuation.
local stripped = s:gsub("%'s$", ''):gsub('%b{}', ''):gsub('%p', '') --strip possessive, then phrases inside of {} that we commonly see with addons putting target markers in chat then punctuation
local name, realmName = UnitName(stripped) --get the name's name and realm if applicable
local fullName = (realmName and realmName ~= '') and name..'-'..realmName or name --if the player has a realmName, their full name is name..'-'..realmName, if not, it's just the name
if fullName == stripped then --check to see if their full name is the same as the stripped word (sans possessive and punctuation), this also removes unitNames like 'player' or 'focus' etc
return fullName, name, realmName
end
return nil --the word passed to the function was not a player's name
end
local function extractFullName(s) --will either return a name-realm or just the name, depending on if the player is on the same server
local fullName, name = getName(s)
return fullName, name
end
local function extractShortName(s) --will only return a name, removes -realmName; in addition, it colors the words you and your by the player's class color
local fullName, name = getName(s)
if fullName then --this is only true if the word is a name, this might be a name-realm, however, we use the name that is passed, not the fullName later
return fullName, name
end
local stripped = s:gsub("%'s$", ''):gsub('%b{}', ''):gsub('%p', '') --strip possessive, then phrases inside of {} that we commonly see with addons putting target markers in chat then punctuation
local lowered = stripped:lower()
if lowered == 'you' or lowered == 'your' then
return playersName, stripped --we use playersName just so when we check for (unit and name) below we pass the logic test, we don't actually substitute the word you or your with the player's name
end
return nil --the word passed to the function was neither a player's name or you or your
end
local coloredMessage = {} --table to hold our words for concatenation
local function hexColorNames(chatMessage, modChatColor, extractShortName)
wipe(coloredMessage) --clear our table to make sure we're not adding to a previous entry
local extractNameFunc = extractShortName or extractFullName --decide if we want do a short or a long based on whether we run modHexColorNames which passes the extractShortName argument
for word in gmatch(chatMessage, '%S+') do --splits up a string into words by splitting spaces
local unit, name = extractNameFunc(word) --will either pass it to extractShortName if we're calling this function from modHexColorNames, or extractFullName if we're calling this function from hexColorNames
if (unit and name) then
local _, class = UnitClass(unit)
if class then
local coloredName = '|c'..RAID_CLASS_COLORS[class].colorStr..name..modChatColor
word = word:gsub(name, coloredName)
end
end
table.insert(coloredMessage, word) --insert the word into our message table irrespective of whether it was a "special" word that requires coloring
end
return table.concat(coloredMessage, ' ') --return the phrase re-separated by spaces
end
local function modHexColorNames(chatMessage, modChatColor)
return hexColorNames(chatMessage, modChatColor, extractShortName) --passes the 3rd argument to hexColorNames so it knows to extractShortName
end