-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEvents.lua
More file actions
executable file
·227 lines (176 loc) · 5.75 KB
/
Events.lua
File metadata and controls
executable file
·227 lines (176 loc) · 5.75 KB
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
local _, RS = ...;
RS.loaded = false
RS.itemWaitTable = {}
RS.bankIsOpen = false
RS.merchantIsOpen = false
local lastTimeRestocked = GetTime()
local function count(T)
local i = 0
for _, _ in pairs(T) do
i = i+1
end
return i
end
local E = CreateFrame("Frame");
E:RegisterEvent("ADDON_LOADED");
E:RegisterEvent("MERCHANT_SHOW");
E:RegisterEvent("MERCHANT_CLOSED");
E:RegisterEvent("BANKFRAME_OPENED");
E:RegisterEvent("BANKFRAME_CLOSED");
E:RegisterEvent("GET_ITEM_INFO_RECEIVED");
E:RegisterEvent("PLAYER_LOGOUT");
E:RegisterEvent("PLAYER_ENTERING_WORLD");
E:RegisterEvent("UI_ERROR_MESSAGE");
E:SetScript("OnEvent", function(self, event, ...)
return self[event] and self[event](self, ...)
end)
function E:ADDON_LOADED(name)
if name ~= "Restocker" then return end
-- NEW RESTOCKER
RS:loadSettings()
for profile, _ in pairs(Restocker.profiles) do
for _, item in ipairs(Restocker.profiles[profile]) do
item.itemID = tonumber(item.itemID)
end
end
local f=InterfaceOptionsFrame;
f:SetMovable(true);
f:EnableMouse(true);
f:SetUserPlaced(true);
f:SetScript("OnMouseDown", f.StartMoving);
f:SetScript("OnMouseUp", f.StopMovingOrSizing);
SLASH_RESTOCKER1= "/restocker";
SLASH_RESTOCKER2= "/rs";
SlashCmdList.RESTOCKER = function(msg)
RS:SlashCommand(msg)
end
RS:CreateOptionsMenu()
RS:Show()
RS:Hide()
RS.loaded = true
end
function E:PLAYER_ENTERING_WORLD(login, reloadui)
if not RS.loaded then return end
if (login or reloadui) and Restocker.loginMessage then
print(RS.addonName .. "loaded")
end
end
function E:MERCHANT_SHOW()
RS.buying = true
if not Restocker.autoBuy then return end -- If not autobuying then return
if IsShiftKeyDown() then return end -- If shiftkey is down return
RS.merchantIsOpen = true
if count(Restocker.profiles[Restocker.currentProfile]) == 0 then return end -- If profile is emtpy then return
if GetTime() - lastTimeRestocked < 1 then return end -- If vendor repoened within 1 second then return (only activate addon once per second)
lastTimeRestocked = GetTime()
local boughtSomething = false
if Restocker.autoOpenAtMerchant then RS:Show() end
local _, class = UnitClass("PLAYER")
local poisonReagentsNeeded = class == "ROGUE" and RS:getPoisonReagents() or {}
local buyTable = {}
local restockList = Restocker.profiles[Restocker.currentProfile]
-- BUILD THE TABLE USED FOR BUYING ITEMS
for _, item in ipairs(restockList) do
local numInBags = GetItemCount(item.itemName, false)
local numNeeded = item.amount - numInBags
if numNeeded > 0 then
if not buyTable[item.itemName] then
buyTable[item.itemName] = {}
buyTable[item.itemName]["numNeeded"] = numNeeded
buyTable[item.itemName]["itemName"] = item.itemName
buyTable[item.itemName]["itemID"] = item.itemID
buyTable[item.itemName]["itemLink"] = item.itemLink
else
buyTable[item.itemName]["numNeeded"] = buyTable[item.itemName]["numNeeded"] + numNeeded
end
end
end
-- INSERT POISON REAGENTS INTO BUYTABLE
for reagent, amount in pairs(poisonReagentsNeeded) do
if not buyTable[reagent] then
buyTable[reagent] = {}
buyTable[reagent]["numNeeded"] = amount
buyTable[reagent]["itemName"] = reagent
else
buyTable[reagent]["numNeeded"] = buyTable[reagent]["numNeeded"] + amount
end
end
-- LOOP THROUGH VENDOR ITEMS
for i = 0, GetMerchantNumItems() do
if not RS.buying then return end
local itemName, _, _, _, numAvailable = GetMerchantItemInfo(i)
local itemLink = GetMerchantItemLink(i)
if buyTable[itemName] then
local item = buyTable[itemName]
local _, _, _, _, _, _, _, itemStackCount = GetItemInfo(itemLink)
if item.numNeeded > numAvailable and numAvailable > 0 then
BuyMerchantItem(i, numAvailable)
boughtSomething = true
else
for n = item.numNeeded, 1, -itemStackCount do
if n > itemStackCount then
BuyMerchantItem(i, itemStackCount)
boughtSomething = true
else
BuyMerchantItem(i, n)
boughtSomething = true
end
end -- forloop
end
end -- if buyTable[itemName] ~= nil
end -- for loop GetMerchantNumItems()
if boughtSomething then RS:Print("finished restocking from vendor.") end
end
function E:MERCHANT_CLOSED()
RS.merchantIsOpen = false
RS:Hide()
end
function E:BANKFRAME_OPENED(isMinor)
if IsShiftKeyDown() then return end
if not Restocker.restockFromBank then return end
if Restocker.profiles[Restocker.currentProfile] == nil then return end
if Restocker.autoOpenAtBank then RS:Show() end
if isMinor then
RS.minorChange = true
else
RS.minorChange = false
end
RS.didBankStuff = false
RS.bankIsOpen = true
RS.currentlyRestocking = true
RS.onUpdateFrame:Show()
end
function RS:BANKFRAME_OPENED(bool)
E:BANKFRAME_OPENED(not not bool)
end
function RS:MERCHANT_SHOW()
E:MERCHANT_SHOW()
end
function E:BANKFRAME_CLOSED()
RS.bankIsOpen = false
RS.currentlyRestocking = false
RS:Hide()
end
function E:GET_ITEM_INFO_RECEIVED(itemID, success)
if success == nil then return end
if RS.itemWaitTable[itemID] then
RS.itemWaitTable[itemID] = nil
RS:addItem(itemID)
end
end
function E:PLAYER_LOGOUT()
if Restocker.framePos == nil then Restocker.framePos = {} end
RS:Show()
RS:Hide()
local point, relativeTo, relativePoint, xOfs, yOfs = RS.addon:GetPoint(RS.addon:GetNumPoints())
Restocker.framePos.point = point
Restocker.framePos.relativePoint = relativePoint
Restocker.framePos.xOfs = xOfs
Restocker.framePos.yOfs = yOfs
end
function E:UI_ERROR_MESSAGE(id, message)
if id == 2 or id == 3 then -- catch inventory / bank full error messages
RS.currentlyRestocking = false
RS.buying = false
end
end