Thread Tools Display Modes
01-17-23, 05:13 PM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Retrieve item name to disenchant

My intent is to retrirve the name of the item I'm about to disenchant, so as to warn if I'm disenchanting a piece I want to keep. How can I accomplish all this?

Last edited by Benalish : 01-18-23 at 12:53 PM.
  Reply With Quote
01-18-23, 12:34 PM   #2
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Ok, this script works, and is based on the GameTooltip. But what about if GameTooltip is hidden for some reason?

Lua Code:
  1. local mt = {
  2.     __index = {
  3.         isvalue = function(t, value)
  4.             local is = false
  5.             for k, entry in ipairs(t) do
  6.                 if (entry == value) then
  7.                     is = true
  8.                     break
  9.                 end
  10.             end
  11.             return is
  12.         end
  13.     }
  14. }
  15.  
  16.  
  17. local protected = { "item1", "item2", "item3", "etch." } -- items I want to protect
  18. setmetatable(protected, mt)
  19.  
  20. local antidisenchant = CreateFrame("Frame")
  21.  
  22. antidisenchant:RegisterEvent("UNIT_SPELLCAST_SENT")
  23.  
  24. antidisenchant:SetScript("OnEvent", function(self, event, ...)
  25.     if (event == "UNIT_SPELLCAST_SENT") then
  26.         local item, link = GameTooltip:GetItem()
  27.         if (arg2 == "Disenchant") and (protected:isvalue(item)) then
  28.             DEFAULT_CHAT_FRAME:AddMessage("WARNING! You're disenchanting "..link,1,0,0)
  29.         end
  30.     end
  31. end)
  Reply With Quote
01-19-23, 09:16 AM   #3
fatrog
A Fallenroot Satyr
Join Date: Nov 2022
Posts: 21
I'm not sure if I can help you, but have you tried to get the target of the spell itself?
spell id of disenchant is 13262
  Reply With Quote
01-19-23, 04:48 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
This hooks calls to target an item and store the ItemID in an upvalue. I added support for Classic Era, which still has UseContainerItem() in the global table instead of C_Container. Feel free to remove this support if you don't plan on dealing with that client. There are other situations that'll overwrite the value, but when read immediately when the spellcast starts, it should be fine.

Lua Code:
  1. local C_Container=C_Container or _G;--  C_Container doesn't exist in Classic Era (added to Wrath Classic in 3.4.1)
  2. local TargetItemID;
  3.  
  4. --  Runs from ContainerFrameItemButton_OnClick() (also SecureActionButton "target-bag"/"target-slot" attributes for container items)
  5. hooksecurefunc(C_Container,"UseContainerItem",function(bag,slot)
  6.     TargetItemID=C_Container.GetContainerItemID(bag,slot);
  7. end);
  8.  
  9. --  Runs from SecureActionButton "target-slot" attribute for inventory items
  10. hooksecurefunc("UseInventoryItem",function(slot)
  11.     TargetItemID=GetInventoryItemID("player",slot);
  12. end);
  13.  
  14. --  Runs from SecureActionButton "target-item" attribute
  15. hooksecurefunc("SpellTargetItem",function(item)
  16.     TargetItemID=GetItemInfoInstant(item);--    Accepts ItemID, ItemLink, or ItemName; Returns ItemID as first value
  17. end);



PS: I have concerns over reading from the arg2 global. I had thought Blizzard stopped using arg# as globals a long time ago.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-19-23 at 04:57 PM.
  Reply With Quote
01-19-23, 06:14 PM   #5
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Since I'm unfamiliar with hooksecurefuncs, how could I trace the item using these functions?
  Reply With Quote
01-19-23, 11:05 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
hooksecurefunc() lets you post-hook a function without tainting it. As used in the code I posted, it takes the args passed into each function that could be triggered by clicking an item to disenchant it and it records the ItemID into an upvalue named TargetItemID. All you need to do outside of this is check TargetItemID when you detect that the Disenchant spell casts and use that as a reference.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-21-23, 01:22 PM   #7
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Thank you very much!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Retrieve item name to disenchant


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