This example works for me by using
GetItemCount(), with a few bugs
It returns more information than just
playerReagentCount and it's easier to check before/after the cast
- This can only track items used from the reagent bank, since GetInventoryItemID() does not seem to work for the normal bank while it's not opened. The reagent bank does not have this limitation
- This errors when the last piece of item in the bank has been consumed because it only looks for the item after the fact
Lua Code:
local f = CreateFrame("Frame")
local recipeIds = {}
local prevBankItemCount = {}
local currentReagents
local function GetBankItemCount(itemInfo)
local totalItemCount = GetItemCount(itemInfo, true)
local playerItemCount = GetItemCount(itemInfo)
local bankItemCount = totalItemCount-playerItemCount
return bankItemCount
end
-- get the list of recipe ids for the current tradeskill
function f:TRADE_SKILL_DATA_SOURCE_CHANGED()
recipeIds = tInvert(C_TradeSkillUI.GetAllRecipeIDs())
end
-- GetItemCount returns old values when this event fires
function f:UNIT_SPELLCAST_SUCCEEDED(unitTarget, castGUID, spellID)
if recipeIds[spellID] then
currentReagents = {}
for i = 1, C_TradeSkillUI.GetRecipeNumReagents(spellID) do
-- playerReagentCount returns the same value as GetItemCount(item, true)
--local name, icon, reagentCount, playerReagentCount = C_TradeSkillUI.GetRecipeReagentInfo(spellID, i)
local link = C_TradeSkillUI.GetRecipeReagentItemLink(spellID, i)
prevBankItemCount[link] = GetBankItemCount(link)
local itemID = GetItemInfoFromHyperlink(link)
currentReagents[itemID] = true
end
end
end
function f:PLAYERBANKSLOTS_CHANGED(slot)
local inventoryID = BankButtonIDToInvSlotID(slot)
self:BANKSLOTS_CHANGED(inventoryID)
end
function f:PLAYERREAGENTBANKSLOTS_CHANGED(slot)
local inventoryID = ReagentBankButtonIDToInvSlotID(slot)
self:BANKSLOTS_CHANGED(inventoryID)
end
-- GetItemCount returns new values when these events fire
function f:BANKSLOTS_CHANGED(inventoryID)
-- quirk: GetInventoryItemID only returns data for the bank while it's opened
-- but for the reagent bank invSlotIDs it does not seem to have this restriction
local itemID = GetInventoryItemID("player", inventoryID)
if currentReagents[itemID] then
local itemLink = select(2, GetItemInfo(itemID))
local prevCount = prevBankItemCount[itemLink]
local newCount = GetBankItemCount(itemLink)
if newCount < prevCount then
print(format("Used %dx %s from the (reagent) bank", prevCount-newCount, itemLink))
end
currentReagents[itemID] = nil
end
end
function f:OnEvent(event, ...)
self[event](self, ...)
end
f:RegisterEvent("TRADE_SKILL_DATA_SOURCE_CHANGED")
f:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
f:RegisterEvent("PLAYERBANKSLOTS_CHANGED")
f:RegisterEvent("PLAYERREAGENTBANKSLOTS_CHANGED")
f:SetScript("OnEvent", f.OnEvent)
Code:
You create: [Mithril Casing].
Used 3x [Mithril Bar] from the (reagent) bank
You create: [White Smoke Flare]x3.
Used 1x [Elemental Blasting Powder] from the (reagent) bank
Used 1x [Netherweave Cloth] from the (reagent) bank