public
Description: WoW Addon - Color player names in chat by their corresponding raid class colors
Homepage: http://www.tekkub.net
Clone URL: git://github.com/tekkub/teknicolor.git
Click here to lend your support to: teknicolor and make a donation at www.pledgie.com !
teknicolor / teknicolor.lua
100644 232 lines (179 sloc) 6.121 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
----------------------------
-- Localization --
----------------------------
 
local locale = GetLocale()
local classnames = locale == "deDE" and {
  ["Hexenmeister"] = "Warlock",
  ["Hexenmeisterin"] = "Warlock",
  ["Krieger"] = "Warrior",
  ["Kriegerin"] = "Warrior",
  ["J\195\164ger"] = "Hunter",
  ["J\195\164gerin"] = "Hunter",
  ["Magier"] = "Mage",
  ["Magierin"] = "Mage",
  ["Priester"] = "Priest",
  ["Priesterin"] = "Priest",
  ["Druide"] = "Druid",
  ["Druidin"] = "Druid",
  ["Paladin"] = "Paladin",
  ["Schamane"] = "Shaman",
  ["Schamanin"] = "Shaman",
  ["Schurke"] = "Rogue",
  ["Schurkin"] = "Rogue",
  ["Todesritter"] = "Death Knight",
  ["Todesritterin"] = "Death Knight",
} or locale == "frFR" and {
  ["D\195\169moniste"] = "Warlock",
  ["Guerrier"] = "Warrior",
  ["Guerri\195\168re"] = "Warrior",
  ["Chasseur"] = "Hunter",
  ["Chasseresse"] = "Hunter",
  ["Mage"] = "Mage",
  ["Pr\195\170tre"] = "Priest",
  ["Pr\195\170tresse"] = "Priest",
  ["Druide"] = "Druid",
  ["Druidesse"] = "Druid",
  ["Paladin"] = "Paladin",
  ["Chaman"] = "Shaman",
  ["Chamane"] = "Shaman",
  ["Voleur"] = "Rogue",
  ["Voleuse"] = "Rogue",
  ["Death Knight"] = "Death Knight",
} or {
  ["Warlock"] = "Warlock",
  ["Warrior"] = "Warrior",
  ["Hunter"] = "Hunter",
  ["Mage"] = "Mage",
  ["Priest"] = "Priest",
  ["Druid"] = "Druid",
  ["Paladin"] = "Paladin",
  ["Shaman"] = "Shaman",
  ["Rogue"] = "Rogue",
  ["Death Knight"] = "Death Knight",
}
 
 
------------------------------
-- Are you local? --
------------------------------
 
local colors = {}
for class,eng in pairs(classnames) do
  local token = eng:gsub(" ", ""):upper()
  local c = RAID_CLASS_COLORS[token]
  if c then
    local hex = string.format("%02x%02x%02x", c.r*255, c.g*255, c.b*255)
    colors[eng] = hex
    colors[class] = hex
    colors[token] = hex
  end
end
 
 
local x, namesnobracket = {}, {}
local names = setmetatable({}, {
  __index = function(t, k) return x[k] end,
  __newindex = function(t, k, v)
    if not v or not k or x[k] or not colors[v] then return end
    x[k] = string.format("|cff%s[%s]|r", colors[v], k)
    namesnobracket[k] = "|cff".. colors[v].. k.. "|r"
  end,
})
 
 
teknicolor = {}
teknicolor.nametable = names
 
 
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, ...) if teknicolor[event] then teknicolor[event](teknicolor, event, ...) end end)
 
 
function teknicolor:PLAYER_LOGIN()
  names[UnitName("player")] = select(2, UnitClass("player"))
 
--~   f:RegisterEvent("VARIABLES_LOADED")
  f:RegisterEvent("FRIENDLIST_UPDATE")
  f:RegisterEvent("GUILD_ROSTER_UPDATE")
  f:RegisterEvent("RAID_ROSTER_UPDATE")
  f:RegisterEvent("PARTY_MEMBERS_CHANGED")
  f:RegisterEvent("PLAYER_TARGET_CHANGED")
  f:RegisterEvent("WHO_LIST_UPDATE")
  f:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
  f:RegisterEvent("CHAT_MSG_SYSTEM")
 
  if IsInGuild() then GuildRoster() end
  if GetNumFriends() > 0 then ShowFriends() end
  self.PLAYER_LOGIN = nil
end
 
 
if IsLoggedIn() then teknicolor:PLAYER_LOGIN() else f:RegisterEvent("PLAYER_LOGIN") end
 
 
------------------------------------
-- Class caching events --
------------------------------------
 
function teknicolor:FRIENDLIST_UPDATE()
  for i=1,GetNumFriends() do
    local name, _, class = GetFriendInfo(i)
    if name then names[name] = class end
  end
end
 
 
function teknicolor:GUILD_ROSTER_UPDATE()
  for i=1,GetNumGuildMembers(true) do
    local name, _, _, _, _, _, _, _, _, _, engclass = GetGuildRosterInfo(i)
    if name then names[name] = engclass end
  end
end
 
 
function teknicolor:RAID_ROSTER_UPDATE()
  for i=1,GetNumRaidMembers() do
    local name, _, _, _, _, class = GetRaidRosterInfo(i)
    names[name] = class
  end
end
 
 
function teknicolor:PARTY_MEMBERS_CHANGED()
  for i=1,GetNumPartyMembers() do
    local unit = "party".. i
    local _, class = UnitClass(unit)
    names[UnitName(unit)] = class
  end
end
 
 
function teknicolor:PLAYER_TARGET_CHANGED()
  if not UnitIsPlayer("target") or not UnitIsFriend("player", "target") then return end
  local _, class = UnitClass("target")
  names[UnitName("target")] = class
end
 
 
function teknicolor:WHO_LIST_UPDATE()
  for i=1,GetNumWhoResults() do
    local name, _, _, _, class = GetWhoInfo(i)
    names[name] = class
  end
end
 
 
function teknicolor:UPDATE_MOUSEOVER_UNIT()
  if not UnitIsPlayer("mouseover") or not UnitIsFriend("player", "mouseover") then return end
  local _, class = UnitClass("mouseover")
  names[UnitName("mouseover")] = class
end
 
 
function teknicolor:CHAT_MSG_SYSTEM()
  local _, _, name, class = string.find(arg1, "^|Hplayer:%w+|h%[(%w+)%]|h: Level %d+ %w+ (%w+) %- .+$")
  if name and class then names[name] = class end
end
 
 
----------------------------------
-- Chatframe coloring --
----------------------------------
 
local origadds = {}
 
 
local function NewAddMessage(frame, text, ...)
  local name = arg2
  if event == "CHAT_MSG_SYSTEM" then
    local pname = text:match("^(%S+) has gone offline.$")
    if pname and namesnobracket[pname] then text = namesnobracket[pname].." has gone offline."
    else name = select(3, string.find(text, "|h%[(.+)%]|h")) end
  end
  if name and names[name] then text = string.gsub(text, "|h%["..name.."%]|h", "|h"..names[name].."|h") end
  return origadds[frame](frame, text, ...)
end
 
 
ChatFrame1.AddMessage, origadds[ChatFrame1] = NewAddMessage, ChatFrame1.AddMessage
for i=3,7 do _G["ChatFrame"..i].AddMessage, origadds[_G["ChatFrame"..i]] = NewAddMessage, _G["ChatFrame"..i].AddMessage end
 
 
------------------------------------
-- Friend List Coloring --
------------------------------------
 
local origs, frameindexes = {}, {}
 
 
local inhook
local function NewSetText(frame, name, ...)
  if inhook then return end -- Failsafe to avoid the great infinity
  inhook = true
 
  local i = frameindexes[frame]
  local _, _, class = GetFriendInfo(FauxScrollFrame_GetOffset(FriendsFrameFriendsScrollFrame) + i)
  if name and class and colors[class] then origs[frame](frame, "|cff"..colors[class]..name.."|r", ...) end
 
  inhook = nil
end
 
 
for i=1,FRIENDS_TO_DISPLAY do
  local f = _G["FriendsFrameFriendButton"..i.."ButtonTextName"]
  frameindexes[f] = i
  origs[f] = f.SetText
  hooksecurefunc(f, "SetText", NewSetText)
end