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 139 lines (102 sloc) 3.682 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
------------------------------
-- Are you local? --
------------------------------
 
local colors = {}
local nameclass = {}
local namesnobracket = setmetatable({}, {
__index = function(t, k)
local nc = k and nameclass[k]
local c = nc and colors[nc]
if not c then return end
 
local v = "|cff".. c.. k.. "|r"
t[k] = v
return v
end,
})
local x = setmetatable({}, {
__index = function(t, k)
local nc = k and nameclass[k]
local c = nc and colors[nc]
if not c then return end
 
local v = string.format("[|cff%s%s|r]", c, k)
t[k] = v
return v
end,
})
local names = setmetatable({}, {
__index = function(t, k) return x[k] end,
__newindex = function(t, k, v) if colors[v] then nameclass[k] = v end end,
})
 
 
local function SetColors()
for i in pairs(x) do x[i] = nil end
for i in pairs(namesnobracket) do namesnobracket[i] = nil end
local cc = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
for token,loc_male in pairs(LOCALIZED_CLASS_NAMES_MALE) do
local loc_female = LOCALIZED_CLASS_NAMES_FEMALE[token]
local c = cc[token]
if c then
local hex = string.format("%02x%02x%02x", c.r*255, c.g*255, c.b*255)
colors[loc_male], colors[loc_female], colors[token] = hex, hex, hex
end
end
end
SetColors()
if CUSTOM_CLASS_COLORS then CUSTOM_CLASS_COLORS:RegisterCallback(SetColors) end
SetColors = nil
 
 
 
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()
f:RegisterEvent("FRIENDLIST_UPDATE")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
 
if IsInGuild() then GuildRoster() end
if GetNumFriends() > 0 then ShowFriends() end
 
f:UnregisterEvent("PLAYER_LOGIN")
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
 
 
----------------------------------
-- Chatframe coloring --
----------------------------------
 
local OFFLINE_MATCH = ERR_FRIEND_OFFLINE_S:gsub("%%s", "(%%S+)")
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", function(self, event, msg, ...)
local pname = msg:match(OFFLINE_MATCH)
if pname and namesnobracket[pname] then return false, string.format(ERR_FRIEND_OFFLINE_S, namesnobracket[pname]), ... end
 
local name = msg:match("|h%[(.+)%]|h")
if name and names[name] then return false, msg:gsub("|h%["..name.."%]|h", "|h"..names[name].."|h"), ... end
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