WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to track if a Reagent was used from Reagent Bank while crafting? (https://www.wowinterface.com/forums/showthread.php?t=58552)

Xruptor 01-17-21 10:01 AM

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!

Xruptor 02-19-21 08:17 AM

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.

Ketho 02-19-21 09:13 AM

Quote:

Originally Posted by Xruptor (Post 338298)
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].


Quote:

Originally Posted by Xruptor (Post 338298)
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

Ketho 02-19-21 11:14 AM

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


Xruptor 02-19-21 05:24 PM

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.


All times are GMT -6. The time now is 01:51 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI