WoWInterface - View Single Post - Code optimization and issue with hyperlinks in chat
View Single Post
01-10-19, 10:33 PM   #18
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
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:
  1. local function getName(s) --Gets a name from the given string after stripping out possessives and punctuation.
  2.     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
  3.     local name, realmName = UnitName(stripped) --get the name's name and realm if applicable
  4.     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
  5.     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
  6.         return fullName, name, realmName
  7.     end
  9.     return nil --the word passed to the function was not a player's name
  10. end
  12. local function extractFullName(s) --will either return a name-realm or just the name, depending on if the player is on the same server
  13.     local fullName, name = getName(s)
  14.     return fullName, name
  15. end
  17. 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
  18.     local fullName, name = getName(s)
  19.     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
  20.         return fullName, name
  21.     end
  23.     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
  24.     local lowered = stripped:lower()
  25.     if lowered == 'you' or lowered == 'your' then
  26.         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
  27.     end
  29.     return nil --the word passed to the function was neither a player's name or you or your
  30. end
  32. local coloredMessage = {} --table to hold our words for concatenation
  33. local function hexColorNames(chatMessage, modChatColor, extractShortName)
  34.     wipe(coloredMessage) --clear our table to make sure we're not adding to a previous entry
  35.     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
  36.     for word in gmatch(chatMessage, '%S+') do --splits up a string into words by splitting spaces
  37.         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
  38.         if (unit and name) then
  39.             local _, class = UnitClass(unit)
  40.             if class then
  41.                 local coloredName = '|c'..RAID_CLASS_COLORS[class].colorStr..name..modChatColor
  42.                 word = word:gsub(name, coloredName)
  43.             end
  44.         end
  45.         table.insert(coloredMessage, word) --insert the word into our message table irrespective of whether it was a "special" word that requires coloring
  46.     end
  47.     return table.concat(coloredMessage, ' ') --return the phrase re-separated by spaces
  48. end
  50. local function modHexColorNames(chatMessage, modChatColor)
  51.     return hexColorNames(chatMessage, modChatColor, extractShortName) --passes the 3rd argument to hexColorNames so it knows to extractShortName
  52. end

Last edited by Terenna : 01-10-19 at 11:10 PM.
  Reply With Quote