-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoulShardMax.lua
103 lines (85 loc) · 2.92 KB
/
SoulShardMax.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
local addonname = ...
local f = CreateFrame("Frame", addonname, UIParent)
f:RegisterEvent("ADDON_LOADED")
f:Hide()
SoulShardMax_max = 0
SoulBagType = 4
f:SetScript("OnEvent", function(self, event, arg1, arg2, ...)
if event == "ADDON_LOADED" and arg1 == addonname then
print("SoulShardMax is enabled, current max:", SoulShardMax_max)
end
end)
SLASH_SOULSHARDMAX1 = "/soulshardmax"
SLASH_SOULSHARDMAX2 = "/ssm"
SlashCmdList["SOULSHARDMAX"] = function(msg)
local tokens = SSM_Tokenize(msg)
if table.getn(tokens) > 0 then
argument = tonumber(tokens[1])
if argument and argument == math.floor(argument) and argument >= 0 then
SSM_SetMaxShards(argument)
print("SoulShardMax: Max number of Soul Shards to keep set to ", SoulShardMax_max)
end
else
SSM_DeleteExcessShards()
end
end
local SoulShardItemID = 6265
function SSM_Tokenize(str)
local tbl = {};
for v in string.gmatch(str, "[^ ]+") do
tinsert(tbl, v);
end
return tbl;
end
function SSM_DeleteExcessShards()
if SoulShardMax_max == 0 then return 0 end
local insertLeftToRight = C_Container.GetInsertItemsLeftToRight()
local bags = {}
local numShards = 0
-- Where is the soul bag if there are some ?
tinsert(bags, {bagIndex=0, isSoulBag=false}) -- backpack
tinsert(bags, {bagIndex=1, isSoulBag=select(2, C_Container.GetContainerNumFreeSlots(1)) == SoulBagType})
tinsert(bags, {bagIndex=2, isSoulBag=select(2, C_Container.GetContainerNumFreeSlots(2)) == SoulBagType})
tinsert(bags, {bagIndex=3, isSoulBag=select(2, C_Container.GetContainerNumFreeSlots(3)) == SoulBagType})
tinsert(bags, {bagIndex=4, isSoulBag=select(2, C_Container.GetContainerNumFreeSlots(4)) == SoulBagType})
-- soul bags first, but always take into account insertLeftToRight
table.sort(bags, function(a,b)
-- if they are different, first is the soulbag one
if a["isSoulBag"] ~= b["isSoulBag"] then
return a["isSoulBag"]
end
-- if they are the same, depends on insertLeftToRight
if insertLeftToRight then
return a["bagIndex"] > b["bagIndex"]
else
return a["bagIndex"] < b["bagIndex"]
end
end)
for i=1,5,1 do
local numShardsInThisBag = SSM_DeleteExcessShardsOneBag(bags[i]["bagIndex"], numShards)
numShards = numShards + numShardsInThisBag
end
end
function SSM_DeleteExcessShardsOneBag(bagId, previousShards)
local numShardsInThisBag = 0
-- count from start of bag to the end, so it will start to delete latest items
for slot=1,C_Container.GetContainerNumSlots(bagId),1 do
if C_Container.GetContainerItemID(bagId, slot) == SoulShardItemID then
if numShardsInThisBag + previousShards >= SoulShardMax_max then
C_Container.PickupContainerItem(bagId, slot)
_, cursorItemID = GetCursorInfo()
if cursorItemID == SoulShardItemID then
DeleteCursorItem()
else
ClearCursor()
end
else
numShardsInThisBag = numShardsInThisBag+1
end
end
end
return numShardsInThisBag
end
function SSM_SetMaxShards(count)
SoulShardMax_max = count;
end