-
Notifications
You must be signed in to change notification settings - Fork 8
/
Baggins-ItemOps.lua
385 lines (325 loc) · 14.3 KB
/
Baggins-ItemOps.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
--
-- This file contains operations on bags/items that are not really part of
-- Baggins core functionality, such as compressing your inventory, splitting
-- items, etc.
--
local Baggins = Baggins
local pairs, ipairs, next, select, format, wipe =
pairs, ipairs, next, select, format, wipe
local floor = floor
local GetItemInfo = C_Item and C_Item.GetItemInfo or GetItemInfo
local GetContainerItemLink = C_Container and C_Container.GetContainerItemLink or GetContainerItemLink
local GetContainerItemInfo = C_Container and C_Container.GetContainerItemInfo or GetContainerItemInfo
local GetContainerNumFreeSlots = C_Container and C_Container.GetContainerNumFreeSlots or GetContainerNumFreeSlots
local GetContainerNumSlots = C_Container and C_Container.GetContainerNumSlots or GetContainerNumSlots
local GetItemFamily = C_Item and C_Item.GetItemFamily or GetItemFamily
local PickupContainerItem, SplitContainerItem, IsShiftKeyDown =
C_Container and C_Container.PickupContainerItem or PickupContainerItem, SplitContainerItem, IsShiftKeyDown
local band =
bit.band
local BANK_CONTAINER = BANK_CONTAINER
local NUM_BAG_SLOTS = NUM_TOTAL_EQUIPPED_BAG_SLOTS or NUM_BAG_SLOTS
local NUM_BANKBAGSLOTS = NUM_BANKBAGSLOTS
local LibStub = LibStub
local L = LibStub("AceLocale-3.0"):GetLocale("Baggins")
local LBU = LibStub("LibBagUtils-1.0")
local bankBags = { BANK_CONTAINER }
for i=NUM_BAG_SLOTS+1, NUM_BAG_SLOTS+NUM_BANKBAGSLOTS do
tinsert(bankBags, i);
end
local charBags = {}
for i=0, NUM_BAG_SLOTS do
tinsert(charBags, i);
end
if Baggins:IsClassicWow() or Baggins:IsTBCWow() or Baggins:IsWrathWow() then
tinsert(charBags, KEYRING_CONTAINER)
end
------------------------------------------------------
-- Compress stacks and move stuff to specialty bags --
------------------------------------------------------
local incompleteSlots = {} -- pairs of itemlink = bag*1000+slot, e.g. "foo" = 2007 (bag 2 slot 7)
local compressLoopProtect = -1
function Baggins:CompressBags(bank) --luacheck: ignore 212
compressLoopProtect = 100
return Baggins:DoCompressBags(bank)
end
function Baggins:CanCompressBags(bank) --luacheck: ignore 212
return Baggins:DoCompressBags(bank,true)
end
function Baggins:DoCompressBags(bank,testonly)
local bags = bank and bankBags or charBags
wipe(incompleteSlots)
local lockedSlots
for _,bag in ipairs(bags) do
for slot=1,(GetContainerNumSlots(bag) or 0) do
local itemInfo = GetContainerItemInfo(bag, slot)
local itemCount = itemInfo and itemInfo.stackCount
local locked = itemInfo and itemInfo.isLocked
local link = GetContainerItemLink(bag, slot)
lockedSlots = lockedSlots or locked
if link and (testonly or not locked) then
local _, _, _, _, _, _, _, iMaxStack = GetItemInfo(link)
if iMaxStack and itemCount and itemCount < iMaxStack then
local itemid = link:match("item:(-?[%d]+):")
if(incompleteSlots[itemid]) then -- see if we have an incomplete stack of this sitting around
if testonly then return true end -- Yup, we've got something that needs compressing!
compressLoopProtect = compressLoopProtect - 1
if compressLoopProtect < 0 then return end
PickupContainerItem(floor(incompleteSlots[itemid]/1000), incompleteSlots[itemid]%1000)
PickupContainerItem(bag, slot)
self:ScheduleTimer("DoCompressBags", 0.1, bank)
return
else
incompleteSlots[itemid] = bag*1000 + slot -- remember that there's an incomplete stack of this item sitting around
end
end
end
end
end
if lockedSlots and not testonly then
compressLoopProtect = compressLoopProtect - 10
if compressLoopProtect > 0 then
self:ScheduleTimer("DoCompressBags", 1, bank) -- try again in 1s to see if locks released
return
end
end
return Baggins:MoveToSpecialtyBags(bank,testonly)
end
local specialtyTargetBags = {} -- [family] = bag*1000 + slot
function Baggins:MoveToSpecialtyBags(bank,testonly)
wipe(specialtyTargetBags)
for _,bag in ipairs(bank and bankBags or charBags) do
local free,bagFamily = LBU:GetContainerNumFreeSlots(bag)
if bag == 5 then
bagFamily = 2048
end
if free>0 and bagFamily~=0 then
for slot=1, (GetContainerNumSlots(bag) or 0) do
if not GetContainerItemLink(bag, slot) then
specialtyTargetBags[bagFamily] = bag*1000 + slot
break -- only interested in the first empty slot, we're only moving one item
end
end
end
end
local lockedSlots
-- Find stuff that can go in specialty bags
if next(specialtyTargetBags) then
for _,bag in ipairs(bank and bankBags or charBags) do
local bagFamily = LBU:GetContainerFamily(bag)
if bag == 5 then
bagFamily = 2048
end
if bagFamily==0 then -- only examine stuff in normal bags
for slot=1, (GetContainerNumSlots(bag) or 0) do
local _, _, locked, _, _ = GetContainerItemInfo(bag, slot)
local link = GetContainerItemLink(bag, slot)
lockedSlots = lockedSlots or locked
if link and (testonly or not locked) then
local itemFamily = GetItemFamily(link)
if itemFamily and itemFamily~=0 then -- itemFamily can apparently be null? (before item is cached?)
if select(9, GetItemInfo(link)) == "INVTYPE_BAG" then --luacheck: ignore 542
--Baggins:Debug('specialtyTargetBags Item Info', select(9, GetItemInfo(link))=="INVTYPE_BAG")
else
for bagFamilySpecial,dest in pairs(specialtyTargetBags) do
if Baggins:IsRetailWow() then
if band(itemFamily,bagFamilySpecial) ~= 0 then
if testonly then return true end
compressLoopProtect = compressLoopProtect - 1
if compressLoopProtect < 0 then return end
PickupContainerItem(bag,slot)
PickupContainerItem(floor(dest/1000),dest%1000)
self:ScheduleTimer("MoveToSpecialtyBags", 0.1, bank)
return
end
if bag ~= 5 and GetContainerNumFreeSlots(5) > 0 and select(17, GetItemInfo(link)) then
if testonly then return true end
compressLoopProtect = compressLoopProtect - 1
if compressLoopProtect < 0 then return end
PickupContainerItem(bag,slot)
PickupContainerItem(floor(dest/1000),dest%1000)
self:ScheduleTimer("MoveToSpecialtyBags", 0.1, bank)
return
end
end
if Baggins:IsClassicWow() or Baggins:IsTBCWow() or Baggins:IsWrathWow() or Baggins:IsCataWow() then
if itemFamily == bagFamilySpecial then
if testonly then return true end
compressLoopProtect = compressLoopProtect - 1
if compressLoopProtect < 0 then return end
PickupContainerItem(bag,slot)
PickupContainerItem(floor(dest/1000),dest%1000)
self:ScheduleTimer("MoveToSpecialtyBags", 0.1, bank)
return
end
end
end
end
end
end
end
end
end
end
if lockedSlots and not testonly then
compressLoopProtect = compressLoopProtect - 10
if compressLoopProtect > 0 then
self:ScheduleTimer("MoveToSpecialtyBags", 1, bank)
return
end
end
if not testonly then
compressLoopProtect = -1 -- signals compressor not running
end
end
Baggins:RegisterSignal("Baggins_AllBagsClosed",
function(self) --luacheck: ignore 212
compressLoopProtect = -1 -- stop compress worker very soon... not STRICTLY needed, but if it titsups, closing bags seems like a good user action to force it to stop
-- stupid stuff HAS happened in the past, like looping on trying to put a herb bag in a herb bag. derp.
end,
Baggins
);
local lastCanCompressBags = "maybe";
local lastCanCompressBank = "maybe";
local timerRecheckCompress
function Baggins:RecheckCompress()
if timerRecheckCompress then
self:CancelTimer(timerRecheckCompress)
end
if compressLoopProtect>0 then
timerRecheckCompress = self:ScheduleTimer("DoRecheckCompress", 1.5)
else
timerRecheckCompress = self:ScheduleTimer("DoRecheckCompress", 0.1)
end
end
function Baggins:DoRecheckCompress()
timerRecheckCompress = nil
local b
b = self:CanCompressBags(false) -- false=bags
if b ~= lastCanCompressBags then
lastCanCompressBags = b
self:FireSignal("Baggins_CanCompress", false, b) -- false=bags
end
b = self:CanCompressBags(true) -- true=bank
if b ~= lastCanCompressBank then
lastCanCompressBank = b
self:FireSignal("Baggins_CanCompress", true, b) -- true=bank
end
end
Baggins:RegisterSignal("Baggins_BagsUpdatedWhileOpen",
function(self)
self:RecheckCompress()
end,
Baggins
);
Baggins:RegisterSignal("Baggins_BagOpened",
function(self)
self:RecheckCompress()
end,
Baggins
);
Baggins.Baggins_RecheckCompress = Baggins.RecheckCompress
---------------------
-- Splitting items --
---------------------
local function BagginsItemButton_Split(bag,slot,amount)
-- Pick up the new stack
local link = GetContainerItemLink(bag,slot)
SplitContainerItem(bag, slot, amount or 1)
if(IsShiftKeyDown()) then
return; -- Just split off if shift was held down, keep new stack in cursor
end
-- First, try to put in specialty bags
local itemFamily = GetItemFamily(link)
if itemFamily~=0 then
for _,destbag in ipairs(charBags) do
local free,bagFamily = LBU:GetContainerNumFreeSlots(destbag)
if bag == 5 then
bagFamily = 2048
end
if free>0 and band(bagFamily,itemFamily)~=0 then
for destslot=1, GetContainerNumSlots(destbag) do
if not GetContainerItemLink(destbag, destslot) then
PickupContainerItem(destbag, destslot)
return
end
end
end
end
end
-- Mkay, shove it in any bag with free slots
for _,destbag in ipairs(charBags) do
local free,bagFamily = GetContainerNumFreeSlots(destbag)
if bag == 5 then
bagFamily = 2048
end
if free>0 and bagFamily==0 then
for destslot=1, GetContainerNumSlots(destbag) do
if not GetContainerItemLink(destbag, destslot) then
PickupContainerItem(destbag, destslot)
return
end
end
end
end
-- Ends up with the item in the cursor if there's no room. Indication enough?
end
---------------------
-- ItemButton menu --
---------------------
local lastSplitSliderValue = 1
local function splitSliderFunc(value)
lastSplitSliderValue = value;
end
Baggins:RegisterSignal("Baggins_ItemButtonMenu",
function(self, button, dewdrop, level, value) --luacheck: ignore 212
local bag = button:GetParent():GetID();
local slot = button:GetID();
local link = GetContainerItemLink(bag,slot)
if not link then return; end
local _, itemCount = GetContainerItemInfo(bag, slot);
local itemstring = link:match("(item:[-%d:]+)");
if level==1 then
local b;
-- "Use"
if not LBU:IsBank(bag) then
dewdrop:AddLine("text",L["Use"], "secure", { type="item", item=itemstring },
"closeWhenClicked",true, "tooltipText", L["Use/equip the item rather than bank/sell it"]);
b=true;
end
-- "Split"
if itemCount>1 then
dewdrop:AddLine("text",format(L["Split %d"],lastSplitSliderValue),
"func", BagginsItemButton_Split,
"arg1",bag, "arg2",slot, "arg3", lastSplitSliderValue,
"tooltipText", L["Split_tooltip"],
"mouseoverUnderline", true,
"hasArrow",true, "hasSlider", true,
"sliderMin", 1, "sliderMax", itemCount-1,
"sliderStep", 1,
"sliderValue", lastSplitSliderValue,
"sliderFunc", splitSliderFunc,
"textR", 1,
"textG", lastSplitSliderValue>=itemCount and 0.5 or 1,
"textB", lastSplitSliderValue>=itemCount and 0.5 or 1
);
b=true;
end
-- separator
if b then
dewdrop:AddLine("text","","disabled",1);
end
end
end,
Baggins
);
--[[
Baggins:RegisterSignal("Baggins_BagsUpdatedWhileOpen",
function(self)
if dewdrop:GetOpenedParent() then
dewdrop:Refresh(1);
end
end,
Baggins
);
--]]