-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddon.lua
337 lines (274 loc) · 9.45 KB
/
Addon.lua
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
--[[--------------------------------------------------------------------
Broker: Reincarnation
Reincarnation cooldown monitor for shamans.
Formerly known as AnkhUp and Ankh Cooldown Timer.
Copyright (c) 2006-2016 Phanx <addons@phanx.net>. All rights reserved.
http://www.wowinterface.com/downloads/info6330-BrokerReincarnation.html
https://mods.curse.com/addons/wow/broker-reincarnation
https://github.com/Phanx/Broker_Reincarnation
----------------------------------------------------------------------]]
local ADDON_NAME, L = ...
if select(2, UnitClass("player")) ~= "SHAMAN" then
return DisableAddOn(ADDON_NAME)
end
------------------------------------------------------------------------
local ceil, format, print, strfind, strjoin = ceil, format, print, strfind, strjoin
local db
local cooldown, cooldownStartTime, resurrectionTime = 0, 0, 0
local COOLDOWN_MAX_TIME = 1800
local Addon = CreateFrame("Frame")
Addon:SetScript("OnEvent", function(self, event, ...) return self[event] and self[event](self, ...) end)
Addon:RegisterEvent("ADDON_LOADED")
L.Reincarnation = GetSpellInfo(20608)
------------------------------------------------------------------------
function Addon:Debug(lvl, str, ...)
if lvl <= 0 then
if ... then
if strfind(str, "%%[dfqsx%.%d]") then
str = format(str, ...)
else
str = strjoin(", ", str, ...)
end
end
print("|cffff7f7f[DEBUG] Broker Reincarnation:|r", str)
end
end
function Addon:Print(str, ...)
if (...) then
if strfind(str, "%%[dfqsx%.%d]") then
str = format(str, ...)
else
str = strjoin(", ", ...)
end
end
print("|cffffcc00Broker Reincarnation:|r %s", str)
end
------------------------------------------------------------------------
local ABBR_DAY = strlower(gsub(DAY_ONELETTER_ABBR, " ", ""))
local ABBR_HOUR = strlower(gsub(HOUR_ONELETTER_ABBR, " ", ""))
local ABBR_MINUTE = strlower(gsub(MINUTE_ONELETTER_ABBR, " ", ""))
local ABBR_SECOND = strlower(gsub(SECOND_ONELETTER_ABBR, " ", ""))
local function GetAbbreviatedTime(seconds)
if seconds >= 86400 then
return format(ABBR_DAY, ceil(seconds / 86400))
elseif seconds >= 3600 then
return format(ABBR_HOUR, ceil(seconds / 3600))
elseif seconds >= 60 then
return format(ABBR_MINUTE, ceil(seconds / 60))
end
return format(ABBR_SECOND, seconds)
end
------------------------------------------------------------------------
local function GetGradientColor(percent)
local r1, g1, b1, r2, g2, b2
if percent <= 0.5 then
percent = percent * 2
r1, g1, b1 = 0.2, 1, 0.2
r2, g2, b2 = 1, 1, 0.2
else
percent = percent * 2 - 1
r1, g1, b1 = 1, 1, 0.2
r2, g2, b2 = 1, 0.2, 0.2
end
return r1 + (r2 - r1) * percent, g1 + (g2 - g1) * percent, b1 + (b2 - b1) * percent
end
------------------------------------------------------------------------
function Addon:UpdateText()
if cooldown > 0 then
local r, g, b = GetGradientColor(cooldown / COOLDOWN_MAX_TIME)
self.dataObject.text = format("|cff%02x%02x%02x%s|r", r * 255, g * 255, b * 255, GetAbbreviatedTime(cooldown))
else
self.dataObject.text = format("|cff33ff33%s|r", L.Ready)
end
end
------------------------------------------------------------------------
local timer = Addon:CreateAnimationGroup()
timer.animation = timer:CreateAnimation()
timer.animation:SetDuration(0.25)
timer:SetScript("OnFinished", function(self, requested)
cooldown = cooldownStartTime + COOLDOWN_MAX_TIME - GetTime()
Addon:UpdateText()
if cooldown <= 0 then
if db.notify then
local color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)["SHAMAN"]
UIErrorsFrame:AddMessage(L.ReadyMessage, color.r, color.g, color.b)
end
cooldown = 0
else
self:Play()
end
end)
------------------------------------------------------------------------
function Addon:Reincarnate(start)
cooldownStartTime = start
db.last = time() - (GetTime() - start)
db.first = db.first or db.last
db.total = 1 + (db.total or 0)
timer:Play()
end
------------------------------------------------------------------------
function Addon:ADDON_LOADED(addon)
if addon ~= ADDON_NAME then return end
self:Debug(1, "ADDON_LOADED", addon)
local defaults = {
notify = true,
}
if not AnkhUpDB then
AnkhUpDB = defaults
db = AnkhUpDB
else
db = AnkhUpDB
for k, v in pairs(defaults) do
if type(db[k]) ~= type(v) then
db[k] = v
end
end
end
self:UnregisterEvent("ADDON_LOADED")
self.ADDON_LOADED = nil
if IsLoggedIn() then
self:PLAYER_LOGIN()
else
self:RegisterEvent("PLAYER_LOGIN")
end
end
------------------------------------------------------------------------
function Addon:PLAYER_LOGIN()
self:Debug(1, "PLAYER_LOGIN")
self:UnregisterEvent("PLAYER_LOGIN")
self.PLAYER_LOGIN = nil
self:SPELLS_CHANGED()
if not self.dataObject then
self:RegisterEvent("SPELLS_CHANGED")
end
end
------------------------------------------------------------------------
function Addon:PLAYER_ALIVE()
self:Debug(1, "PLAYER_ALIVE")
if UnitIsGhost("player") then
return self:Debug(1, "UnitIsGhost player")
end
resurrectionTime = GetTime()
end
------------------------------------------------------------------------
function Addon:SPELL_UPDATE_COOLDOWN()
self:Debug(1, "SPELL_UPDATE_COOLDOWN")
local now = GetTime()
if now - resurrectionTime > 1 then return end
self:Debug(1, "Player just resurrected.")
local start, duration = GetSpellCooldown(L.Reincarnation)
if start and duration and start > 0 and duration > 0 then
if now - start < 1 then
self:Debug(1, "Player just used Reincarnation.")
self:Reincarnate(start)
timer:Play()
end
end
end
------------------------------------------------------------------------
function Addon:SPELLS_CHANGED()
self:Debug(1, "SPELLS_CHANGED")
if not IsSpellKnown(20608) then
self:Debug(1, "Reincarnation not learned yet.")
return
end
self:UnregisterEvent("SPELLS_CHANGED")
if self.dataObject then
self:Debug(1, "Data object already created.")
return
end
local menu = CreateFrame("Frame", "BrokerReincarnationMenu", UIParent, "UIDropDownMenuTemplate")
menu.displayMode = "MENU"
menu.GetNotify = function() return db.nofity end
menu.SetNotify = function() db.notify = not db.notify end
menu.Reset = function() db.total, db.first, db.last = nil, nil, nil end
menu.Close = function() CloseDropDownMenus() end
menu.initialize = function(self, level)
if not level or level > 1 then return end
local info = UIDropDownMenu_CreateInfo()
info.text = ADDON_NAME
info.isTitle = 1
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
info = UIDropDownMenu_CreateInfo()
info.text = L.NotifyReady
info.checked = self.GetNotify
info.func = self.SetNotify
info.isNotRadio = true
info.keepShownOnClick = 1
UIDropDownMenu_AddButton(info, level)
info = UIDropDownMenu_CreateInfo()
info.text = L.Reset
info.func = self.Reset
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
info = UIDropDownMenu_CreateInfo()
info.text = CLOSE
info.func = self.Close
info.notCheckable = 1
UIDropDownMenu_AddButton(info, level)
end
local function CleanDate(...)
local text = date(...)
text = gsub(text, "^0", "")
text = gsub(text, "([^:%d])0", "%1")
return text
end
self.dataObject = LibStub("LibDataBroker-1.1"):NewDataObject("Reincarnation", {
type = "data source",
icon = [[Interface\ICONS\Spell_Shaman_ImprovedReincarnation]],
label = L.Reincarnation,
text = L.Ready,
OnClick = function(self, button)
if button == "RightButton" then
ToggleDropDownMenu(1, nil, menu, self, 0, 0)
end
end,
OnTooltipShow = function(tooltip)
--tooltip:AddLine(L.Reincarnation, 1, 0.82, 0)
if cooldown > 0 then
local r, g, b = GetGradientColor(cooldown / COOLDOWN_MAX_TIME)
tooltip:AddDoubleLine(L.Reincarnation, GetAbbreviatedTime(cooldown), nil, nil, nil, r, g, b)
--tooltip:AddDoubleLine(COOLDOWN_REMAINING, GetAbbreviatedTime(cooldown), 1, 1, 1, r, g, b)
else
tooltip:AddDoubleLine(L.Reincarnation, L.Ready, nil, nil, nil, 0.2, 1, 0.2)
--tooltip:AddLine(L.Ready, 0.2, 1, 0.2)
end
local last = db.last
if db.last then
local h, m = GetGameTime()
local today = time() - (h * 3600) - (m * 60)
local yesterday = today - 86400
local text
if last > today then
text = CleanDate(L.Today, last)
elseif last > yesterday then
text = CleanDate(L.Yesterday, last)
else
text = CleanDate(L.Date, last)
end
tooltip:AddLine(" ")
tooltip:AddLine(L.Last, 1, 0.8, 0)
tooltip:AddDoubleLine(" ", text, nil, nil, nil, 1, 1, 1)
if db.first and db.total and db.total > 1 then
tooltip:AddLine(" ")
tooltip:AddLine(L.Total, 1, 0.8, 0)
tooltip:AddDoubleLine(" ", format(CleanDate(L.TotalSinceDate, db.first), db.total), nil, nil, nil, 1, 1, 1)
end
end
tooltip:AddLine(" ")
tooltip:AddLine(L.RightClickOptions, 1, 0.8, 0)
end,
})
self:RegisterEvent("PLAYER_ALIVE")
self:RegisterEvent("SPELL_UPDATE_COOLDOWN")
local start, duration = GetSpellCooldown(L.Reincarnation)
if start and duration and start > 0 and duration > 0 then
self:Debug(1, "Reincarnation is on cooldown.")
cooldownStartTime = start
timer:Play()
end
self:Debug(1, "cooldown = %d", cooldown)
self:UpdateText()
end
------------------------------------------------------------------------