-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGearSwitcher.lua
451 lines (362 loc) · 11.7 KB
/
GearSwitcher.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
local parent, ns = ...
-- The frame!
local gearSwitcher = CreateFrame("Frame")
-- Used to hold our set data, auto fills with saved data later
local sets = {}
-- Defaults to Arms
local playerSpec = {"Arms", ""}
-- Queue up changes if we enter into combat or are changing sets while in combat
local queue = {}
--[[
0 = ammo
1 = head
2 = neck
3 = shoulder
4 = shirt
5 = chest
6 = belt
7 = legs
8 = feet
9 = wrist
10 = gloves
11 = finger 1
12 = finger 2
13 = trinket 1
14 = trinket 2
15 = back
16 = main hand
17 = off hand
18 = ranged
19 = tabard
20 = first bag (the rightmost one)
21 = second bag
22 = third bag
23 = fourth bag (the leftmost one)
--]]
local invSlots = {
[1] = "HeadSlot",
[2] = "NeckSlot",
[3] = "ShoulderSlot",
[15] = "BackSlot",
[5] = "ChestSlot",
[9] = "WristSlot",
[10] = "HandsSlot",
[6] = "WaistSlot",
[7] = "LegsSlot",
[8] = "FeetSlot",
[11] = "Finger0Slot",
[12] = "Finger1Slot",
[13] = "Trinket0Slot",
[14] = "Trinket1Slot",
[18] = "RangedSlot",
[16] = "MainHandSlot",
[17] = "SecondaryHandSlot"
}
-- PVP Zones
local PvPZoneNames = {
-- Battlegrounds
"Warsong Gulch",
"Arathi Basin",
"Twin Peaks",
"The Battle for Gilneas",
"Eye of the Storm",
"Atlerac Valley",
"Strand of the Ancients",
"Isle of Conquest",
-- Arenas
"Blade's Edge Arena",
"Dalaran Arena",
"Nagrand Arena",
"Ruins of Lordaeron",
"The Ring of Valor",
-- World PvP Zones
"Tol Barad", -- Cata
"Wintergrasp", -- WoTLK
}
-- Debugging
local function debug(message) DEFAULT_CHAT_FRAME:AddMessage(message) end
local function CheckPvPStatus (currentZone)
for _, zone in pairs(PvPZoneNames) do
if zone == currentZone and UnitIsPVP("player") then
return true
end
end
if UnitIsPVP("player") then
return true
end
return false
end
-- Returns a table of currently equipped gear as {[slot] = itemLink}
local function GetEquipped ()
local gear = {}
for slotID, slot in pairs(invSlots) do
if CursorHasItem() then ClearCursor() end
-- Pickup the slot
PickupInventoryItem(slotID)
if CursorHasItem() then
ClearCursor()
gear[slot] = GetInventoryItemLink("player", slotID)
else
ClearCursor()
end
end
return gear
end
-- Checks to see if we've got an updated gear item.
-- We return if we have queue'd gear chagnes that need to be updated in our equipment sets.
local function CheckForUpdates(stance, change_weapons)
local equipped = GetEquipped()
local gearUpdate = false
local weaponUpdate = false
for slotID, slot in pairs(invSlots) do
if slot == "MainHandSlot" or slot == "SecondaryHandSlot" then
if equipped[slot] and equipped[slot] ~= "" then
if sets[stance]["Gear"][slot] == "" and equipped[slot] ~= "" then
sets[stance]["Gear"][slot] = equipped[slot]
weaponUpdate = true
else
local statsEquipped = GetItemStats(equipped[slot])
local statsSet = GetItemStats(sets[stance]["Gear"][slot])
if change_weapons or (statsSet["ITEM_MOD_STRENGTH_SHORT"] < statsEquipped["ITEM_MOD_STRENGTH_SHORT"]) then
sets[stance]["Gear"][slot] = equipped[slot]
weaponUpdate = true
end
end
end
else
if sets[0][slot] == "" then
sets[0][slot] = equipped[slot]
gearUpdate = true
elseif sets[0][slot] ~= equipped[slot] then
local statsEquipped = GetItemStats(equipped[slot])
local statsSet = GetItemStats(sets[0][slot])
if statsSet["ITEM_MOD_RESILIENCE_RATING_SHORT"] < statsEquipped["ITEM_MOD_RESILIENCE_RATING_SHORT"] then
sets[0][slot] = equipped[slot]
gearUpdate = true
end
end
end
end
return weaponUpdate, gearUpdate
end
-- Check if our Equipment Name exists
local function CheckEquipmentName(name)
for index = 1, GetNumEquipmentSets() do
if name == select(1, GetEquipmentSetInfo(index)) then
return true
end
end
return false
end
-- Update our EquipmentManager set
local function UpdateEquipmentManager(setID)
-- Break out if we are in combat as we can't use EquipItemByName in combat.
if InCombatLockdown() then
table.concat(queue, setID)
gearSwitcher:RegisterEvent("PLAYER_REGEN_ENABLED")
return
end
if setID > 0 and setID <= 3 then
local setName = sets[setID]["SetName"]
-- Used to make sure we are overwriting the right set.
if CheckEquipmentName(setName) then
UseEquipmentSet(setName)
end
-- Equip our weapons.
if sets[setID]["Gear"]["MainHandSlot"] ~= "" then
EquipItemByName(sets[setID]["Gear"]["MainHandSlot"])
end
if sets[setID]["Gear"]["SecondaryHandSlot"] ~= "" then
EquipItemByName(sets[setID]["Gear"]["SecondaryHandSlot"])
end
SaveEquipmentSet(setName, 1)
elseif setID == 0 then
local setName
for stance = 1, 3 do
setName = sets[stance]["SetName"]
-- Check if our set already exists and equip it so we don't overwrite previous weapon sets.
if CheckEquipmentName(setName) then
UseEquipmentSet(setName)
end
-- Equip our armor, no matter if it's already equiped or not
for _, slot in pairs(invSlots) do
if slot ~= "MainHandSlot" and slot ~= "SecondarySlotHand" then
EquipItemByName(sets[setID][slot])
end
end
-- Save the set over the top of the previous set.
SaveEquipmentSet(setName, 1)
end
end
end
-- Sets our players primary spec as well as sets the subspec for fury's Titan Grip or Single-Minded Fury
local function SetSpec()
for tab = 1, GetNumTalentTabs() do
if select(5, GetTalentTabInfo(tab)) >= 31 then
local spec = select(2, GetTalentTabInfo(tab))
if spec == "Fury" then
playerSpec = {spec, select(5, GetTalentInfo(tab, 20)) == 1 and GetTalentInfo(tab, 20) or (select(5, GetTalentInfo(tab, 21)) == 1 and GetTalentInfo(tab, 21) or "")}
debug("Subspec = " .. select(5, GetTalentInfo(tab, 20)) == 1 and GetTalentInfo(tab, 20) or (select(5, GetTalentInfo(tab, 21)) == 1 and GetTalentInfo(tab, 21) or ""))
else
playerSpec = {spec, ""}
end
end
end
end
local function WhatSpecAmI()
for tab = 1, GetNumTalentTabs() do
if select(5, GetTalentTabInfo(tab)) >= 31 then
return select(2, GetTalentTabInfo(tab))
end
end
return ""
end
-- Default set table
local function DefaultSet()
return {
[0] = {
["HeadSlot"] = "",
["NeckSlot"] = "",
["ShoulderSlot"] = "",
["BackSlot"] = "",
["ChestSlot"] = "",
["WristSlot"] = "",
["HandsSlot"] = "",
["WaistSlot"] = "",
["LegsSlot"] = "",
["FeetSlot"] = "",
["Finger0Slot"] = "",
["Finger1Slot"] = "",
["Trinket0Slot"] = "",
["Trinket1Slot"] = "",
["RangedSlot"] = "",
},
[1] = {
["SetName"] = "PvP Arms",
["Gear"] = {
["MainHandSlot"] = "",
}
},
[2] = {
["SetName"] = "PvP Protection",
["Gear"] = {
["MainHandSlot"] = "",
["SecondaryHandSlot"] = "",
}
},
[3] = {
["SetName"] = "PvP Fury",
["Gear"] = {
["MainHandSlot"] = "",
["SecondaryHandSlot"] = "",
}
},
}
end
------------------
-- Event Handler
------------------
-- Admin stuff related to loading and saving the database.
gearSwitcher:RegisterEvent("ADDON_LOADED")
gearSwitcher:RegisterEvent("PLAYER_LOGOUT")
-- The actual meat of the addong that switches the gear
gearSwitcher:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
-- Used to update spec details
gearSwitcher:RegisterEvent("PLAYER_ENTERING_WORLD")
gearSwitcher:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
gearSwitcher:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" then
local name = ...
if name == "GearSwitcher" then
if not gearDB or gearDB == {} or gearDB == nil then
sets = DefaultSet()
else
sets = gearDB
end
end
elseif event == "PLAYER_LOGOUT" then
gearDB = sets
elseif event == "UPDATE_SHAPESHIFT_FORM" then
if WhatSpecAmI() ~= playerSpec[1] then
SetSpec()
end
-- Make sure that the weapon switching is only going to happen for pvp zones.
if not CheckPvPStatus(GetRealZoneText()) then return end
local stance = GetShapeshiftForm()
local set
if sets and sets[stance] then
local spec, subspec = unpack(playerSpec)
local mainhand, secondaryhand
if spec == "Fury" then
if (stance == 1 or stance == 3) then
set = sets[3]["SetName"]
else
set = sets[2]["SetName"]
end
elseif spec == "Arms" then
if (stance == 1 or stance == 3) then
set = sets[1]["SetName"]
else
set = sets[2]["SetName"]
end
else
set = sets[2]["SetName"]
end
if CheckEquipmentName(set) then
UseEquipmentSet(set)
end
end
elseif event == "PLAYER_ENTERING_WORLD" or event == "ACTIVE_TALENT_GROUP_CHANGED" then
SetSpec()
elseif event == "PLAYER_REGEN_ENABLED" then
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
end
end)
------------------
-- SLASH COMMAND
------------------
local function handleSlash (msg)
if msg == "reset" then
sets = DefaultSet()
for index = 1, GetNumEquipmentSets() do
for stance = 1, 3 do
if GetEquipmentSetInfo(index) == sets[stance]["SetName"] then
DeleteEquipmentSet(sets[stance]["SetName"])
end
end
end
return
end
local activeSpec = GetActiveTalentGroup()
-- Used to hold which spec is primary.
local stance = GetShapeshiftForm()
local updateWeapons = msg:sub(3, 6) == "true" and true or false
SetSpec()
local name, subspec = unpack(playerSpec)
if msg:sub(1, 1) == "1" then
stance = 1
name = "Arms"
elseif msg:sub(1, 1) == "2" then
stance = 2
name = "Protection"
elseif msg:sub(1, 1) == "3" then
stance = 3
name = "Fury"
else
if not sets or sets == nil or sets == {} then
sets = DefaultSet()
end
-- XXX: Attempt to make an educated guess on what spec/stance they want
return
end
-- Check our current equipped gear against our saved set gear to see if we need to update
local weaponUpdated, gearUpdated = CheckForUpdates(stance, updateWeapons)
if weaponUpdated then
UpdateEquipmentManager(stance)
end
if gearUpdated then
UpdateEquipmentManager(0)
end
end
SLASH_GEARSWITCHER1 = '/gearset'
SlashCmdList.GEARSWITCHER = handleSlash