View Single Post
02-19-21, 11:14 AM   #4
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
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:
  1. local f = CreateFrame("Frame")
  2. local recipeIds = {}
  3. local prevBankItemCount = {}
  4. local currentReagents
  5.  
  6. local function GetBankItemCount(itemInfo)
  7.     local totalItemCount = GetItemCount(itemInfo, true)
  8.     local playerItemCount = GetItemCount(itemInfo)
  9.     local bankItemCount = totalItemCount-playerItemCount
  10.     return bankItemCount
  11. end
  12.  
  13. -- get the list of recipe ids for the current tradeskill
  14. function f:TRADE_SKILL_DATA_SOURCE_CHANGED()
  15.     recipeIds = tInvert(C_TradeSkillUI.GetAllRecipeIDs())
  16. end
  17.  
  18. -- GetItemCount returns old values when this event fires
  19. function f:UNIT_SPELLCAST_SUCCEEDED(unitTarget, castGUID, spellID)
  20.     if recipeIds[spellID] then
  21.         currentReagents = {}
  22.         for i = 1, C_TradeSkillUI.GetRecipeNumReagents(spellID) do
  23.             -- playerReagentCount returns the same value as GetItemCount(item, true)
  24.             --local name, icon, reagentCount, playerReagentCount = C_TradeSkillUI.GetRecipeReagentInfo(spellID, i)
  25.             local link = C_TradeSkillUI.GetRecipeReagentItemLink(spellID, i)
  26.             prevBankItemCount[link] = GetBankItemCount(link)
  27.             local itemID = GetItemInfoFromHyperlink(link)
  28.             currentReagents[itemID] = true
  29.         end
  30.     end
  31. end
  32.  
  33. function f:PLAYERBANKSLOTS_CHANGED(slot)
  34.     local inventoryID = BankButtonIDToInvSlotID(slot)
  35.     self:BANKSLOTS_CHANGED(inventoryID)
  36. end
  37.  
  38. function f:PLAYERREAGENTBANKSLOTS_CHANGED(slot)
  39.     local inventoryID = ReagentBankButtonIDToInvSlotID(slot)
  40.     self:BANKSLOTS_CHANGED(inventoryID)
  41. end
  42.  
  43. -- GetItemCount returns new values when these events fire
  44. function f:BANKSLOTS_CHANGED(inventoryID)
  45.     -- quirk: GetInventoryItemID only returns data for the bank while it's opened
  46.     -- but for the reagent bank invSlotIDs it does not seem to have this restriction
  47.     local itemID = GetInventoryItemID("player", inventoryID)
  48.     if currentReagents[itemID] then
  49.         local itemLink = select(2, GetItemInfo(itemID))
  50.         local prevCount = prevBankItemCount[itemLink]
  51.         local newCount = GetBankItemCount(itemLink)
  52.         if newCount < prevCount then
  53.             print(format("Used %dx %s from the (reagent) bank", prevCount-newCount, itemLink))
  54.         end
  55.         currentReagents[itemID] = nil
  56.     end
  57. end
  58.  
  59. function f:OnEvent(event, ...)
  60.     self[event](self, ...)
  61. end
  62.  
  63. f:RegisterEvent("TRADE_SKILL_DATA_SOURCE_CHANGED")
  64. f:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
  65. f:RegisterEvent("PLAYERBANKSLOTS_CHANGED")
  66. f:RegisterEvent("PLAYERREAGENTBANKSLOTS_CHANGED")
  67. 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

Last edited by Ketho : 02-19-21 at 11:21 AM.
  Reply With Quote