Repository navigation
Expand file tree
/
Copy pathCustomNames.lua
More file actions
158 lines (145 loc) · 5.96 KB
/
Copy pathCustomNames.lua
File metadata and controls
158 lines (145 loc) · 5.96 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
local appName, private = ...
local lib = LibStub("CustomNames")
-- the ui collects these messages and prints a single line, see private.BeginBatch
lib:RegisterCallback("Name_Added", function(event, name, customName)
private.ChatOutput(private.getLocalisation("CallbackAdded"):format(name, customName))
end)
lib:RegisterCallback("Name_Removed", function(event, name)
private.ChatOutput(private.getLocalisation("CallbackRemoved"):format(name))
end)
lib:RegisterCallback("Name_Update", function(event, name, customName, oldCustomName)
private.ChatOutput(private.getLocalisation("CallbackUpdated"):format(name, customName, oldCustomName))
end)
--- links a character of a bnet friend to their btag and remembers its class (the ui colors the character by it)
---@param battleTag string
---@param gameAccountInfo table? -- BNetGameAccountInfo
local function ProcessGameAccount(battleTag, gameAccountInfo)
if not gameAccountInfo or not gameAccountInfo.wowProjectID or gameAccountInfo.wowProjectID ~= WOW_PROJECT_ID
or not gameAccountInfo.characterName or not gameAccountInfo.realmName then return end
local Character = gameAccountInfo.characterName.."-"..gameAccountInfo.realmName
if private.IsForever() then
Character = gameAccountInfo.characterName
Character = private.FixNameForever(Character)
end
if not Character then return end
if not lib.IsInBnetDatabase(Character) and not lib.IsCharUnlinked(Character, battleTag) then
lib.AddCharToBtag(Character,battleTag)
end
private.RememberClassID(Character, gameAccountInfo.classID)
end
--- looks at every character a bnet friend is online on, not only the one the friendlist shows
---@param friendIndex number
local function ProcessFriend(friendIndex)
local number = C_BattleNet.GetFriendNumGameAccounts(friendIndex)
if not number then return end
local bnetAccountInfo = C_BattleNet.GetFriendAccountInfo(friendIndex)
if not bnetAccountInfo or not bnetAccountInfo.battleTag then return end
ProcessGameAccount(bnetAccountInfo.battleTag, bnetAccountInfo.gameAccountInfo)
for accountIndex = 1, number do
ProcessGameAccount(bnetAccountInfo.battleTag, C_BattleNet.GetFriendGameAccountInfo(friendIndex, accountIndex))
end
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("BN_FRIEND_ACCOUNT_ONLINE")
frame:RegisterEvent("BN_FRIEND_INFO_CHANGED")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:RegisterEvent("GROUP_ROSTER_UPDATE")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:SetScript("OnEvent",function(self,...)
local event, friendId, isCompanionApp = ...
if event == "PLAYER_TARGET_CHANGED" then
private.RememberUnitClass("target")
return
elseif event == "BN_FRIEND_ACCOUNT_ONLINE" or event == "BN_FRIEND_INFO_CHANGED" then
if friendId then
ProcessFriend(friendId)
end
elseif event == "PLAYER_ENTERING_WORLD" then
local name,realm = UnitFullName("player")
local fullName = name.."-"..realm
if not lib.IsInBnetDatabase(fullName) and not lib.IsCharUnlinked(fullName, "self") then
lib.AddCharToBtag(fullName, "self")
end
private.RememberPlayerAndGroupClass()
elseif event=='GROUP_ROSTER_UPDATE' then
private.RememberGroupClass()
end
end)
--- Since GetNormalizedRealmName can return nil we need to gsub GetRealmName ourselfs if need be
---@return string Realm
local function NormalizedRealmName()
return GetNormalizedRealmName() or GetRealmName():gsub("[%s-]+", "")
end
SLASH_CustomNames1 = '/CN'
SLASH_CustomNames2 = '/cn'
SLASH_CustomNames3 = '/CustomNames'
SlashCmdList['CustomNames'] = function(msg) -- credit to Ironi
if strtrim(msg) == "" then --open the window
private.ToggleWindow()
elseif string.find(string.lower(msg), "add (.-) to (.-)") then --add
local _, _, type, from, to = string.find(msg, "(.-) (.*) to (.*)")
if from and not string.find(from, "#") and not string.find(from, "-") and not string.find(from, "self") then
if not UnitExists(from) and not private.IsForever() then
from = from .. "-" .. NormalizedRealmName()
end
end
lib.Set(from, to)
elseif string.find(string.lower(msg), "del (.-)") then --delete
local _, _, type, from = string.find(msg, "(.-) (.*)")
if UnitExists(from) then
local unitName, unitRealm = UnitName(from)
local nameToCheck = unitName
if UnitIsPlayer(from) and not private.IsForever() then
nameToCheck= unitName .. "-" .. (unitRealm or NormalizedRealmName())
elseif private.IsForever() then
nameToCheck = private.FixNameForever(unitName)
else
nameToCheck = unitName .. "-".. unitRealm
end
if nameToCheck and lib.isCharInDatabase(nameToCheck) then
local to = lib.Get(nameToCheck)
lib.Set(from)
end
elseif lib.isCharInDatabase(from) then
local to = lib.Get(from)
lib.Set(from)
elseif lib.IsInBnetDatabase(from) then
lib.Set(from)
else
print(private.getLocalisation("NoSuchName"))
end
elseif string.find(string.lower(msg), "edit (.-)") then --edit
local _, _, type, from, to = string.find(msg, "(.-) (.*) to (.*)")
if UnitExists(from) then
local unitName, unitRealm = UnitName(from)
local nameToCheck = unitName
if UnitIsPlayer(from) and not private.IsForever() then
nameToCheck= unitName .. "-" .. (unitRealm or NormalizedRealmName())
elseif private.IsForever() then
nameToCheck = private.FixNameForever(unitName)
else
nameToCheck = unitName .. "-".. unitRealm
end
if nameToCheck and lib.isCharInDatabase(nameToCheck) then
lib.Set(from, to)
end
elseif lib.isCharInDatabase(from) then
lib.Set(from, to)
elseif lib.IsInBnetDatabase(from) then
lib.Set(from, to)
else
print(private.getLocalisation("NoSuchName"));
end
elseif msg == "list" or msg == "l" then
for k,v in pairs(lib.GetList()) do
print(k.." -> "..v)
end
else
print(private.getLocalisation("CommandUsageHeader"))
print(private.getLocalisation("CommandUsageWindow"))
print(private.getLocalisation("CommandUsageAdd"))
print(private.getLocalisation("CommandUsageEdit"))
print(private.getLocalisation("CommandUsageDelete"))
print(private.getLocalisation("CommandUsageList"))
end
end