Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
493 lines (415 loc) · 17.3 KB
/
Copy pathCore.lua
File metadata and controls
493 lines (415 loc) · 17.3 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
-- Core.lua – MyAutoVendor (AzerothCore + Ace3)
local ADDON_NAME = ...
local MyAutoVendor = LibStub("AceAddon-3.0"):NewAddon(
ADDON_NAME,
"AceConsole-3.0",
"AceEvent-3.0"
)
_G[ADDON_NAME] = MyAutoVendor
-- Keep the addon usable if an older client does not load the optional locale file.
if not MyAutoVendor.T then
function MyAutoVendor:T(key)
return key
end
end
local AceDB = LibStub("AceDB-3.0")
local defaults = {
profile = {
autoSell = true,
language = "en",
minimap = {},
globalSellList = {}, -- ★ GLOBAL SELL LIST
globalKeepList = {}, -- ★ GLOBAL KEEP LIST
},
char = {
autoKeepRules = {
keepConsumables = true,
keepQuest = true,
keepGear = true,
keepProfessionMats = true,
minIlvl = 0,
maxIlvl = 300,
},
}
}
local function CharKey()
return (UnitName("player") or "Unknown") .. "-" .. (GetRealmName() or "Realm")
end
local function toItemId(input)
if not input then return nil end
local n = tonumber(input)
if n then return n end
return tonumber(tostring(input):match("item:(%d+)"))
end
local function FormatCopper(c)
c = tonumber(c) or 0
local g = math.floor(c / 10000)
local s = math.floor((c % 10000) / 100)
local cc = c % 100
return string.format("%dg %ds %dc", g, s, cc)
end
---------------------------------------------------------------------
-- AUTO KEEP FIX (ITEM_INFO_RECEIVED)
---------------------------------------------------------------------
local infoWait = {}
local merchantPending = false
local infoFrame = CreateFrame("Frame")
infoFrame:RegisterEvent("ITEM_INFO_RECEIVED")
infoFrame:SetScript("OnEvent", function(_, _, itemID)
if infoWait[itemID] then
infoWait[itemID] = nil
end
if merchantPending then
MyAutoVendor:OnMerchantShow()
end
end)
local hardcodedKeepNames = {
["Legplates of the Lost Protector"] = true,
["Crown of the Wayward Protector"] = true,
["Mantle of the Lost Protector"] = true,
}
local function IsHardcodedKeepName(name)
if not name then return false end
return hardcodedKeepNames[name] == true
end
function MyAutoVendor:EvaluateAutoKeep(itemID)
local name, _, _, itemLevel, _, itemType, itemSubType, _, equipLoc = GetItemInfo(itemID)
if not name then return end
local db = self.char[self._charKey]
if IsHardcodedKeepName(name) then
self.profile.globalSellList[itemID] = nil
db.sellList[itemID] = nil
db.keepList[itemID] = { ts = time(), link = "item:"..itemID }
return
end
if itemType == "Projectile" or itemType == "Ammo" or itemSubType == "Arrow" or itemSubType == "Bullet" then
self.profile.globalSellList[itemID] = nil
db.sellList[itemID] = nil
db.keepList[itemID] = { ts = time(), link = "item:"..itemID }
return
end
local rules = db.autoKeepRules or defaults.char.autoKeepRules
local global = self.profile.globalKeepList
-- An explicit sell rule must not be converted into an auto-keep rule.
if db.sellList[itemID] or self.profile.globalSellList[itemID] then
return
end
-- Already kept globally
if global[itemID] then return end
-- Already kept locally
if db.keepList[itemID] then return end
-- Consumables
if rules.keepConsumables and itemType == "Consumable" then
db.keepList[itemID] = { ts = time(), link = "item:"..itemID }
return
end
-- Quest items
if rules.keepQuest and itemType == "Quest" then
db.keepList[itemID] = { ts = time(), link = "item:"..itemID }
return
end
-- Gear
local minIlvl = tonumber(rules.minIlvl) or 0
local maxIlvl = tonumber(rules.maxIlvl) or math.huge
local hasItemLevel = type(itemLevel) == "number" and itemLevel > 0
local gearInRange = hasItemLevel and (itemLevel >= minIlvl and itemLevel <= maxIlvl)
if rules.keepGear and hasItemLevel and equipLoc and equipLoc ~= "" and gearInRange then
db.keepList[itemID] = { ts = time(), link = "item:"..itemID, auto = true }
return
end
-- Profession mats
if rules.keepProfessionMats and itemType == "Trade Goods" then
db.keepList[itemID] = { ts = time(), link = "item:"..itemID }
return
end
end
---------------------------------------------------------------------
-- PRUNE KEEP LIST (dynamic re-evaluation against current rules)
---------------------------------------------------------------------
-- Removes auto-added keep entries (never manually pinned) that no longer
-- satisfy the current auto-keep rules, e.g. gear that fell below/above the
-- ilvl range, or consumables/gear/profession mats whose rule got disabled.
-- Also removes any keep entry whose item is on the local or global sell list.
function MyAutoVendor:PruneKeepList()
local db = self.char[self._charKey]
local rules = db.autoKeepRules or defaults.char.autoKeepRules
local minIlvl = tonumber(rules.minIlvl) or 0
local maxIlvl = tonumber(rules.maxIlvl) or math.huge
local globalSell = self.profile.globalSellList
local changed = false
for id, entry in pairs(db.keepList) do
-- Sell overrides local Keep, so an item on a sell list is dropped from Keep.
if db.sellList[id] or globalSell[id] then
db.keepList[id] = nil
changed = true
elseif not entry.manual then
local name, _, _, itemLevel, _, itemType, itemSubType, _, equipLoc = GetItemInfo(id)
if name then
local hardcodedKeep = IsHardcodedKeepName(name)
local ammoKeep = itemType == "Projectile" or itemType == "Ammo"
or itemSubType == "Arrow" or itemSubType == "Bullet"
if not hardcodedKeep and not ammoKeep then
local isGear = equipLoc and equipLoc ~= ""
local hasItemLevel = type(itemLevel) == "number" and itemLevel > 0
local keepThis
if isGear then
keepThis = rules.keepGear and hasItemLevel
and itemLevel >= minIlvl and itemLevel <= maxIlvl
elseif itemType == "Consumable" then
keepThis = rules.keepConsumables
elseif itemType == "Quest" then
keepThis = rules.keepQuest
elseif itemType == "Trade Goods" then
keepThis = rules.keepProfessionMats
else
keepThis = true
end
if not keepThis then
db.keepList[id] = nil
changed = true
end
end
end
end
end
if changed and self.RefreshUI then
self:RefreshUI()
end
end
---------------------------------------------------------------------
-- SHARED PROFILE
---------------------------------------------------------------------
-- Older saved variables gave every character its own AceDB profile, which
-- made the "global" lists per character. Fold those lists into the shared
-- Default profile once, then keep every character on Default.
function MyAutoVendor:UseSharedProfile()
local db = self.db
local current = db:GetCurrentProfile()
if not db.global.profilesMerged and db.profiles then
db.profiles["Default"] = db.profiles["Default"] or {}
local target = db.profiles["Default"]
for name, profile in pairs(db.profiles) do
if name ~= "Default" then
for _, key in ipairs({ "globalSellList", "globalKeepList" }) do
if type(profile[key]) == "table" then
target[key] = target[key] or {}
for id, meta in pairs(profile[key]) do
if target[key][id] == nil then
target[key][id] = meta
end
end
end
end
end
end
local currentProfile = db.profiles[current]
if target.language == nil and currentProfile and currentProfile.language then
target.language = currentProfile.language
end
db.global.profilesMerged = true
end
if current ~= "Default" then
db:SetProfile("Default")
end
end
---------------------------------------------------------------------
-- INIT
---------------------------------------------------------------------
function MyAutoVendor:OnInitialize()
self.db = AceDB:New("MyAutoVendorDB", defaults, true)
self:UseSharedProfile()
self.profile = self.db.profile
self.char = self.db.char
self._charKey = CharKey()
self.char[self._charKey] = self.char[self._charKey] or {}
self.char[self._charKey].keepList = self.char[self._charKey].keepList or {}
self.char[self._charKey].sellList = self.char[self._charKey].sellList or {}
self.char[self._charKey].autoKeepRules = self.char[self._charKey].autoKeepRules or {}
for k, v in pairs(defaults.char.autoKeepRules) do
if self.char[self._charKey].autoKeepRules[k] == nil then
self.char[self._charKey].autoKeepRules[k] = v
end
end
self._undoStack = {}
self.activeTab = "keep"
self:RegisterChatCommand("mav", "HandleSlash")
self:SetupMinimap()
end
function MyAutoVendor:OnEnable()
self:RegisterEvent("MERCHANT_SHOW", "OnMerchantShow")
self:PruneKeepList()
end
---------------------------------------------------------------------
-- MINIMAP ICON
---------------------------------------------------------------------
local LDB = LibStub("LibDataBroker-1.1", true)
local LDBIcon = LibStub("LibDBIcon-1.0", true)
function MyAutoVendor:SetupMinimap()
if not LDB or not LDBIcon then return end
local obj = LDB:NewDataObject("MyAutoVendor", {
type = "launcher",
icon = "Interface\\Icons\\INV_Misc_Bag_08",
OnClick = function() MyAutoVendor:ToggleUI() end,
OnTooltipShow = function(tt)
tt:AddLine("MyAutoVendor")
tt:AddLine(MyAutoVendor:T("clickOpen"), 1,1,1)
end,
})
LDBIcon:Register("MyAutoVendor", obj, self.db.profile.minimap)
end
---------------------------------------------------------------------
-- AUTO SELL (local + global) — UDEN GOTO
---------------------------------------------------------------------
function MyAutoVendor:OnMerchantShow()
if not self.profile.autoSell then return end
self:PruneKeepList()
local db = self.char[self._charKey]
local globalSell = self.profile.globalSellList
local globalKeep = self.profile.globalKeepList
local soldItems = {}
local total = 0
merchantPending = false
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local link = GetContainerItemLink(bag, slot)
if link then
local id = tonumber(link:match("item:(%d+)"))
if id then
-- Ensure item info is loaded
local name, _, _, itemLevel, _, itemType, itemSubType, _, equipLoc = GetItemInfo(id)
if not name then
infoWait[id] = true
merchantPending = true
else
self:EvaluateAutoKeep(id)
end
if name then
local hardcodedKeep = IsHardcodedKeepName(name)
local ammoKeep = itemType == "Projectile" or itemType == "Ammo" or itemSubType == "Arrow" or itemSubType == "Bullet"
if hardcodedKeep or ammoKeep then
db.keepList[id] = db.keepList[id] or { ts = time(), link = "item:"..id }
db.sellList[id] = nil
globalSell[id] = nil
end
local rules = db.autoKeepRules or defaults.char.autoKeepRules
local minIlvl = tonumber(rules.minIlvl) or 0
local autoKeepEntry = db.keepList[id]
-- Global Keep overrides Sell; Sell overrides local/automatic Keep.
local explicitSell = db.sellList[id] or (globalSell[id] and not (hardcodedKeep or ammoKeep))
local keep = globalKeep[id] or autoKeepEntry or hardcodedKeep or ammoKeep
local protectedItem = itemType == "Container"
or itemType == "Projectile"
or itemType == "Ammo"
or itemSubType == "Tools"
or itemSubType == "Miscellaneous"
or itemSubType == "Arrow"
or itemSubType == "Bullet"
local vendorPrice = select(11, GetItemInfo(id)) or 0
local autoSellLowGear = not protectedItem
and equipLoc and equipLoc ~= ""
and itemLevel and itemLevel < minIlvl
and vendorPrice > 0
local shouldSell = explicitSell or (autoSellLowGear and not keep)
if shouldSell and not globalKeep[id] then
-- SELL (local, global, or below the automatic gear threshold)
local itemName = name
local price = vendorPrice
local _, count = GetContainerItemInfo(bag, slot)
count = count or 1
local value = price * count
total = total + value
table.insert(soldItems, {
id=id, name=itemName, count=count, price=price, value=value
})
UseContainerItem(bag, slot)
end
end
end
end
end
end
if merchantPending then
return
end
if #soldItems > 0 then
print("|cff00ff00" .. self:T("sold"):format(#soldItems) .. "|r")
for _, info in ipairs(soldItems) do
print(string.format(" - %s x%d", info.name, info.count))
end
print("|cffffff00" .. self:T("totalSold") .. FormatCopper(total) .. "|r")
end
end
---------------------------------------------------------------------
-- ADD / REMOVE / UNDO (local/global)
---------------------------------------------------------------------
function MyAutoVendor:AddItem(input)
local id = toItemId(input)
if not id then return print(self:T("invalidItem")) end
local name = GetItemInfo(id) or ("item:"..id)
local listName =
self.activeTab == "sell" and "sellList" or
self.activeTab == "globalSell" and "globalSellList" or
self.activeTab == "globalKeep" and "globalKeepList" or
"keepList"
local list =
listName == "globalSellList" and self.profile.globalSellList or
listName == "globalKeepList" and self.profile.globalKeepList or
self.char[self._charKey][listName]
list[id] = { ts = time(), link = "item:"..id, manual = listName == "keepList" }
if listName == "sellList" or listName == "globalSellList" then
self:PruneKeepList()
end
print(self:T("added") .. name)
if self.RefreshUI then self:RefreshUI() end
end
function MyAutoVendor:RemoveItem(input)
local id = toItemId(input)
if not id then return end
local listName =
self.activeTab == "sell" and "sellList" or
self.activeTab == "globalSell" and "globalSellList" or
self.activeTab == "globalKeep" and "globalKeepList" or
"keepList"
local list =
listName == "globalSellList" and self.profile.globalSellList or
listName == "globalKeepList" and self.profile.globalKeepList or
self.char[self._charKey][listName]
if list[id] then
table.insert(self._undoStack, { id=id, meta=list[id], tab=self.activeTab })
list[id] = nil
print(self:T("removed"):format(id))
if self.RefreshUI then self:RefreshUI() end
end
end
function MyAutoVendor:Undo()
local u = table.remove(self._undoStack)
if not u then return print(self:T("nothingUndo")) end
local listName =
u.tab == "sell" and "sellList" or
u.tab == "globalSell" and "globalSellList" or
u.tab == "globalKeep" and "globalKeepList" or
"keepList"
local list =
listName == "globalSellList" and self.profile.globalSellList or
listName == "globalKeepList" and self.profile.globalKeepList or
self.char[self._charKey][listName]
list[u.id] = u.meta
print(self:T("undone"):format(u.id))
if self.RefreshUI then self:RefreshUI() end
end
---------------------------------------------------------------------
-- SLASH
---------------------------------------------------------------------
function MyAutoVendor:HandleSlash(msg)
if msg == "ui" then return self:ToggleUI() end
if msg == "undo" then return self:Undo() end
local language = msg:match("^lang%s+(%S+)$")
if language and self.Languages[language] then
self.profile.language = language
print(self:T("languageSet"):format(self:T("languageName")))
return ReloadUI()
end
print(self:T("slashUi"))
print(self:T("slashUndo"))
print(self:T("slashLang"))
end