-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendorPriceScan.lua
More file actions
324 lines (285 loc) · 12.1 KB
/
Copy pathVendorPriceScan.lua
File metadata and controls
324 lines (285 loc) · 12.1 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
-- Local references to constants
local addonName = VendorPriceScan.ADDON_NAME
local addonPrefix = VendorPriceScan.ADDON_PREFIX
local addonVersion = VendorPriceScan.ADDON_VERSION
local slashCommands = VendorPriceScan.SLASH_COMMANDS
local colors = VendorPriceScan.COLORS
local strings = VendorPriceScan.STRINGS
-- Initialize saved variables (account-wide)
VendorPriceScan_Data = VendorPriceScan_Data or {}
-- Initialize character-specific saved variables
VendorPriceScan_CharData = VendorPriceScan_CharData or {}
-- Create main frame
local frame = CreateFrame("Frame", "VendorPriceScanFrame")
-- Variable to store the last price captured
local capturedPrice = nil
-- Store original SetTooltipMoney function
local OriginalSetTooltipMoney = SetTooltipMoney
-- Hook SetTooltipMoney to capture vendor prices
if OriginalSetTooltipMoney == SetTooltipMoney then
SetTooltipMoney = function(tooltip, money, type, prefixText, suffixText)
-- Store the money value before calling original function
capturedPrice = money
-- Call original function and return ALL its values
return OriginalSetTooltipMoney(tooltip, money, type, prefixText, suffixText)
end
end
-- Helper function to count items
local function CountItems()
local count = 0
for _ in pairs(VendorPriceScan_Data) do
count = count + 1
end
return count
end
-- Event handler
frame:SetScript("OnEvent", function()
if event == "MERCHANT_SHOW" then
VendorPriceScan_ScanBags()
elseif event == "ADDON_LOADED" and arg1 == "VendorPriceScan" then
-- Initialize saved variables if nil
if not VendorPriceScan_Data then
VendorPriceScan_Data = {}
end
-- Initialize character-specific settings
if not VendorPriceScan_CharData then
VendorPriceScan_CharData = {}
end
if VendorPriceScan_CharData.showTooltips == nil then
VendorPriceScan_CharData.showTooltips = true
end
if VendorPriceScan_CharData.showTooltipTitle == nil then
VendorPriceScan_CharData.showTooltipTitle = true
end
-- Count items
local count = CountItems()
-- Single line load message
DEFAULT_CHAT_FRAME:AddMessage("|c" .. colors.MAGE_BLUE .. addonName .. "|r: |c" .. colors.GOLD .. "v" .. addonVersion .. "|r " .. strings.LOADED_WITH .. " |c" .. colors.COPPER .. count .. "|r " .. strings.ITEMS_LOWERCASE .. " " .. strings.TYPE_FOR_INFO .. " |c" .. colors.GOLD .. slashCommands.PRIMARY .. "|r " .. strings.FOR_INFO)
end
end)
-- Register events
frame:RegisterEvent("MERCHANT_SHOW")
frame:RegisterEvent("ADDON_LOADED")
-- Create scan tooltip once
local ScanTooltip = CreateFrame("GameTooltip", "VPS_ScanTooltip", UIParent, "GameTooltipTemplate")
ScanTooltip:SetOwner(UIParent, "ANCHOR_NONE")
-- Function to scan all bags
function VendorPriceScan_ScanBags()
-- Scan all 5 bags (0-4, where 0 is backpack)
for bag = 0, 4 do
local numSlots = GetContainerNumSlots(bag)
if numSlots and numSlots > 0 then
for slot = 1, numSlots do
local itemLink = GetContainerItemLink(bag, slot)
if itemLink then
-- Extract itemID from link using shared library
local itemID = VendorPriceScan_UI.ExtractItemID(itemLink)
if itemID then
-- Reset captured price
capturedPrice = nil
-- Trigger tooltip to capture price
ScanTooltip:ClearLines()
ScanTooltip:SetBagItem(bag, slot)
-- Check if we captured a price
if capturedPrice and capturedPrice > 0 then
local _, qty = GetContainerItemInfo(bag, slot)
local pricePerItem = capturedPrice
-- If item is stacked, divide by quantity
if qty and qty > 1 then
pricePerItem = capturedPrice / qty
end
-- Get item name for saving
local itemName = GetItemInfo(itemID)
-- Only save if new or price/name has changed
local oldData = VendorPriceScan_Data[itemID]
local oldPrice = oldData and oldData.vendorPrice or nil
local oldName = oldData and oldData.itemName or nil
local currentName = itemName or (strings.ITEM_PREFIX .. itemID)
if not oldPrice or oldPrice ~= pricePerItem or oldName ~= currentName then
-- Save as table with vendorPrice and itemName
VendorPriceScan_Data[itemID] = {
vendorPrice = pricePerItem,
itemName = currentName
}
end
else
-- No price captured (nil or 0) = item cannot be sold to vendor
-- Remove from database if it exists
if VendorPriceScan_Data[itemID] then
VendorPriceScan_Data[itemID] = nil
end
end
end
end
end
end
end
end
-- Helper function to create tooltip hooks
local function CreateTooltipHook(originalFunc, getLinkAndQty)
return function(...)
-- Call original function and store all return values
local results = {originalFunc(unpack(arg))}
-- Get itemLink and quantity using the provided function
local itemLink, qty = getLinkAndQty(unpack(arg))
-- Add vendor price to tooltip if we have the data and tooltips are enabled
if itemLink and VendorPriceScan_CharData.showTooltips then
VendorPriceScan_UI.AddVendorPriceToTooltip(arg[1], itemLink, qty, VendorPriceScan_Data)
end
-- Return all original values
return unpack(results)
end
end
-- Hook into GameTooltip.SetBagItem (bags)
GameTooltip.SetBagItem = CreateTooltipHook(
GameTooltip.SetBagItem,
function(tooltip, bag, slot)
local itemLink = GetContainerItemLink(bag, slot)
local _, qty = GetContainerItemInfo(bag, slot)
return itemLink, qty
end
)
-- Hook into GameTooltip.SetAuctionItem (auction house browsing)
GameTooltip.SetAuctionItem = CreateTooltipHook(
GameTooltip.SetAuctionItem,
function(tooltip, type, index)
local itemLink = GetAuctionItemLink(type, index)
local _, _, qty = GetAuctionItemInfo(type, index)
return itemLink, qty
end
)
-- Hook into GameTooltip.SetAuctionSellItem (auction house selling)
GameTooltip.SetAuctionSellItem = CreateTooltipHook(
GameTooltip.SetAuctionSellItem,
function(tooltip)
local itemName, _, qty = GetAuctionSellItemInfo()
if itemName then
local itemLink = VendorPriceScan_UI.FindItemLinkByName(itemName)
return itemLink, qty
end
return nil, nil
end
)
-- Hook into GameTooltip.SetInventoryItem (equipped items and character sheet)
GameTooltip.SetInventoryItem = CreateTooltipHook(
GameTooltip.SetInventoryItem,
function(tooltip, unit, slot)
local itemLink = GetInventoryItemLink(unit, slot)
return itemLink, 1
end
)
-- Hook into GameTooltip.SetLootItem (loot window)
GameTooltip.SetLootItem = CreateTooltipHook(
GameTooltip.SetLootItem,
function(tooltip, slot)
local itemLink = GetLootSlotLink(slot)
local _, _, qty = GetLootSlotInfo(slot)
return itemLink, qty
end
)
-- Hook into GameTooltip.SetLootRollItem (loot roll frame - need/greed/pass)
GameTooltip.SetLootRollItem = CreateTooltipHook(
GameTooltip.SetLootRollItem,
function(tooltip, rollID)
local itemLink = GetLootRollItemLink(rollID)
return itemLink, 1
end
)
-- Hook into GameTooltip.SetMerchantItem (vendor window)
GameTooltip.SetMerchantItem = CreateTooltipHook(
GameTooltip.SetMerchantItem,
function(tooltip, slot)
local itemLink = GetMerchantItemLink(slot)
local _, _, _, qty = GetMerchantItemInfo(slot)
return itemLink, qty
end
)
-- Hook into GameTooltip.SetQuestItem (quest rewards)
GameTooltip.SetQuestItem = CreateTooltipHook(
GameTooltip.SetQuestItem,
function(tooltip, type, index)
local itemLink = GetQuestItemLink(type, index)
local _, _, qty = GetQuestItemInfo(type, index)
return itemLink, qty
end
)
-- Hook into GameTooltip.SetQuestLogItem (quest log rewards)
GameTooltip.SetQuestLogItem = CreateTooltipHook(
GameTooltip.SetQuestLogItem,
function(tooltip, type, index)
local itemLink = GetQuestLogItemLink(type, index)
return itemLink, 1
end
)
-- Hook into GameTooltip.SetTradeSkillItem (tradeskill window - crafted item)
GameTooltip.SetTradeSkillItem = CreateTooltipHook(
GameTooltip.SetTradeSkillItem,
function(tooltip, skillIndex, reagentIndex)
if reagentIndex then
-- Reagent (material)
local itemLink = GetTradeSkillReagentItemLink(skillIndex, reagentIndex)
local _, _, qty = GetTradeSkillReagentInfo(skillIndex, reagentIndex)
return itemLink, qty
else
-- Crafted item result
local itemLink = GetTradeSkillItemLink(skillIndex)
local minMade, maxMade = GetTradeSkillNumMade(skillIndex)
return itemLink, minMade or 1
end
end
)
-- Hook into GameTooltip.SetCraftItem (crafting window - simpler professions like enchanting)
GameTooltip.SetCraftItem = CreateTooltipHook(
GameTooltip.SetCraftItem,
function(tooltip, skillIndex, reagentIndex)
if reagentIndex then
-- Reagent (material)
local itemLink = GetCraftReagentItemLink(skillIndex, reagentIndex)
local _, _, qty = GetCraftReagentInfo(skillIndex, reagentIndex)
return itemLink, qty
else
-- Crafted item result
local itemLink = GetCraftItemLink(skillIndex)
return itemLink, 1
end
end
)
-- Slash command
SLASH_VENDORPRICESCAN1 = slashCommands.PRIMARY
SLASH_VENDORPRICESCAN2 = slashCommands.SECONDARY
SlashCmdList["VENDORPRICESCAN"] = function(msg)
if msg == slashCommands.SUBCOMMANDS.SHOW then
VendorPriceScan_ShowWindow()
return
end
local count = CountItems()
DEFAULT_CHAT_FRAME:AddMessage("|c" .. colors.MAGE_BLUE .. "[" .. addonPrefix .. "]|r " .. strings.LOADED_ITEMS .. " |c" .. colors.COPPER .. count .. "|r " .. strings.ITEMS_FROM_DATA)
DEFAULT_CHAT_FRAME:AddMessage("|c" .. colors.MAGE_BLUE .. "[" .. addonPrefix .. "]|r " .. strings.VISIT_VENDOR)
DEFAULT_CHAT_FRAME:AddMessage("|c" .. colors.MAGE_BLUE .. "[" .. addonPrefix .. "]|r " .. strings.USE_COMMAND .. " |c" .. colors.GOLD .. slashCommands.PRIMARY .. " " .. slashCommands.SUBCOMMANDS.SHOW .. "|r " .. strings.TO_OPEN_WINDOW)
end
-- Show window using shared library
function VendorPriceScan_ShowWindow()
VendorPriceScan_UI.ShowWindow({
windowName = "VPS_DatabaseWindow",
title = addonName .. strings.DATABASE_TITLE,
data = VendorPriceScan_Data,
getItemData = function(itemID, rawData)
return {
price = rawData.vendorPrice or 0,
itemName = rawData.itemName
}
end,
formatPrice = function(itemData)
return VendorPriceScan_UI.FormatMoney(itemData.price)
end,
onTooltip = function(tooltip, itemData)
if itemData.price then
tooltip:AddLine(strings.VENDOR_PRICE, 1, 1, 1)
tooltip:AddLine(VendorPriceScan_UI.FormatMoney(itemData.price), 1, 1, 1)
end
end,
sortByPrice = function(a, b)
return a.price < b.price
end
})
end