forked from gabrielecimolino/Swapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapper.lua
188 lines (171 loc) · 4.66 KB
/
Swapper.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
--What we'll need
--=======================================================
local Swapper = CreateFrame("Frame", "Swapper", UIParent)
local AceEvent = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0")
local DEBUG = false
--General functions
--=============================
local function debug(msg)
if DEBUG then
message(msg)
end
end
local function foldr(func, val, tbl)
for i,v in pairs(tbl) do
val = func(val, v)
end
return val
end
local function andIt(x, y)
return (x and y)
end
local function stringCat(x, y)
return x .. y
end
--Event handlers
--========================================================================================
local function handler()
if event == "ADDON_LOADED" then
debug("Swapper loaded")
elseif event == "UI_ERROR_MESSAGE" then
if arg1 == "You can only do that with empty bags." then
local frozenItemBag, frozenItemSlot, frozenItemID = findFrozen()
local frozenBag = findFrozenBag()
-- If there is a frozen item and a frozen bag then we've got the right error
if (frozenItemBag and frozenItemSlot and frozenItemID and frozenBag) then
local freeSlots = allFreeSlots(frozenBag)
local slotsNeeded = GetContainerNumSlots(frozenBag) - findFreeSlots(frozenBag)
if freeSlots >= slotsNeeded then
AceEvent:ScheduleEvent(unequipBag, 1, frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
end
end
end
end
end
function unequipBag(frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
debug("Unequip event")
-- Empty the bag and then store it
emptyBag(frozenBag)
PickupBagFromSlot(ContainerIDToInventoryID(frozenBag))
local freeBag, freeSlot = findNextFreeSlot(frozenBag)
if freeBag == 0 then
PutItemInBackpack()
else
PickupContainerItem(freeBag, freeSlot)
end
-- If it doesn't take then try it again
if CursorHasItem() then
unequipBag(frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
else
-- Now equip the new bag
AceEvent:ScheduleEvent(equipBag, 1, frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
end
end
function equipBag(frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
debug("Equip event")
-- Get the new bag and equip it where the old bag was
PickupContainerItem(frozenItemBag, frozenItemSlot)
EquipCursorItem(ContainerIDToInventoryID(frozenBag))
if CursorHasItem() then
equipBag(frozenItemBag, frozenItemSlot, frozenItemID, frozenBag)
end
end
--Events
--=======================================
Swapper:RegisterEvent("ADDON_LOADED")
Swapper:RegisterEvent("UI_ERROR_MESSAGE")
Swapper:SetScript("OnEvent", handler)
--Utilities
--===========================================================================================
function findFrozen()
for bag = 0, NUM_BAG_SLOTS do
for slot = 1, GetContainerNumSlots(bag) do
-- if the item at this container slot is locked then it's our new bag
local _, _, locked, _, _, _, link, _, _, itemID = GetContainerItemInfo(bag, slot)
if locked == 1 then
link = GetContainerItemLink(bag, slot)
return bag, slot, linkToItemID(link)
end
end
end
end
function linkToItemID(link)
local _, _, itemID = strfind(link, 'item:(%d+):(%d*):(%d*):(%d*)')
itemID = tonumber(itemID)
return itemID
end
local function findFreeSlot(bag)
for slot = 1, GetContainerNumSlots(bag) do
local filled = GetContainerItemInfo(bag, slot)
if not filled then
return slot
end
end
return 0
end
function findFreeSlots(bag)
local freeSlots = 0
for slot = 1, GetContainerNumSlots(bag) do
local filled = GetContainerItemInfo(bag, slot)
if not filled then
freeSlots = freeSlots + 1
end
end
return freeSlots
end
function allFreeSlots(locked)
local freeSlots = 0
for bag = 0, NUM_BAG_SLOTS do
if bag ~= locked then
freeSlots = freeSlots + findFreeSlots(bag)
end
end
return freeSlots
end
function findNextFreeSlot(locked)
for bag = 0, NUM_BAG_SLOTS do
if bag ~= locked then
for slot = 1, GetContainerNumSlots(bag) do
local filled = GetContainerItemInfo(bag, slot)
if not filled then
return bag, slot
end
end
end
end
end
function findBagForItem(frozenBag)
if findFreeSlots(0) > 0 then
PutItemInBackpack()
else
for i = 1, 4 do
local slot = findFreeSlot(i)
if slot > 0 and (i ~= frozenBag) then
PickupContainerItem(i, slot)
return
end
end
end
end
function emptyBag(bag)
for slot = 1, GetContainerNumSlots(bag) do
local filled = GetContainerItemInfo(bag, slot)
if filled then
PickupContainerItem(bag, slot)
findBagForItem(bag)
end
end
return nil
end
function findFrozenBag()
for bag = 0, NUM_BAG_SLOTS do
local isLocked = IsInventoryItemLocked(ContainerIDToInventoryID(bag))
if isLocked then
if bag ~= 0 then
return bag
else
return nil
end
end
end
end