Quantcast
How to track if a Reagent was used from Reagent Bank while crafting? - WoWInterface
 
Thread Tools Display Modes
01-17-21, 10:01 AM   #1
Xruptor
A Flamescale Wyrmkin
 
Xruptor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 126
How to track if a Reagent was used from Reagent Bank while crafting?

Hello all,

I've been pondering on what would be the best way to determine if an item was used from the Reagent Bank during crafting? My addon currently tracks the items in the Reagent Bank. However, when a user crafts something use the Tradeskill UI. It will consume items from the Reagent Bank. That item count won't be updated until they revisit the bank. This creates inaccurate item counts.

So I've been wracking my brain for a few days to come up with ideas on how to solve this. Sadly, I like none of the ideas i've come up with. Some of them are too elaborate and a few are just too simple.

One idea I had was to use GetTradeSkillReagentInfo and grab the playerReagentCount to determine if the reagent count is greater than what the player has in their inventory. Subtracting and then creating a modifier that utilizes the count in the reagent bank. This is terribly ugly way of doing it. As I would have to grab the current recipe being used (TradeSkillFrame.RecipeList:GetSelectedRecipeID()). Track when it's actually crafted using UNIT_SPELLCAST_SUCCEEDED and then record a modifier for the count.

I'm hoping a few of you can give some suggestions or ideas on how to tackle this. I've looked over the API and it's not very helpful for the situation I described above. If anyone know of any other methods/functions/events etc.. that would help please let me know as well.

Thanks in advance!
__________________
Click HERE for the ultimate idiot test.

if (sizeof(sadness) > sizeof(happiness)) { initDepression(); }

Last edited by Xruptor : 01-19-21 at 10:59 AM.
  Reply With Quote
02-19-21, 08:17 AM   #2
Xruptor
A Flamescale Wyrmkin
 
Xruptor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 126
Hmm guess no one has an idea. I really wouldn't to do a prediction algorithm on use as it may provide inaccurate item counts. Wish there was a way to track this stuff properly when consuming items from the reagent bank while crafting.
__________________
Click HERE for the ultimate idiot test.

if (sizeof(sadness) > sizeof(happiness)) { initDepression(); }
  Reply With Quote
02-19-21, 09:13 AM   #3
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 992
Originally Posted by Xruptor View Post
determine if an item was used from the Reagent Bank during crafting

PLAYERREAGENTBANKSLOTS_CHANGED fires when an item is crafted from the reagent bank, or an item was deposited/withdrawn/moved in the reagent bank

Lua Code:
  1. -- assume the player just crafted something
  2. local function OnEvent(self, event, reagentSlot)
  3.     local inventoryID = ReagentBankButtonIDToInvSlotID(reagentSlot)
  4.     local itemID = GetInventoryItemID("player", inventoryID)
  5.     local itemLink = select(2, GetItemInfo(itemID))
  6.  
  7.     print(format("item %d %s was used from your reagent bank", itemID, itemLink))
  8.     print("reagentSlot:", reagentSlot)
  9.     print("inventoryID:", inventoryID)
  10. end
  11.  
  12. local f = CreateFrame("Frame")
  13. f:RegisterEvent("PLAYERREAGENTBANKSLOTS_CHANGED")
  14. f:SetScript("OnEvent", OnEvent)
Code:
item 2840 [Copper Bar] was used from your reagent bank
reagentSlot: 1
inventoryID: 99
You create: [Handful of Copper Bolts].

Originally Posted by Xruptor View Post
use GetTradeSkillReagentInfo and grab the playerReagentCount to determine if the reagent count is greater than what the player has in their inventory.
Probably something like that, yes

Last edited by Ketho : 02-19-21 at 09:39 AM.
  Reply With Quote
02-19-21, 11:14 AM   #4
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 992
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
02-19-21, 05:24 PM   #5
Xruptor
A Flamescale Wyrmkin
 
Xruptor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 126
Thanks for the response Ketho. I will admit I wasn't aware of the PLAYERREAGENTBANKSLOTS_CHANGED event. Odd I did event tracking and it never fired for me. I figured I'd had to do a predictive calculation based on what was consumed in Reagent Bank and what was in the players inventory. I'm going to have to do a few tests to see if it can do it accurately. One method is to subtract the totals from the Reagent Bank per event fire and then rescan all the player bags again to get an accurate count based on the items used.
__________________
Click HERE for the ultimate idiot test.

if (sizeof(sadness) > sizeof(happiness)) { initDepression(); }
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to track if a Reagent was used from Reagent Bank while crafting?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off