Thread Tools Display Modes
12-15-23, 09:21 PM   #1
pflueger92
A Murloc Raider
Join Date: Oct 2022
Posts: 6
On Right Click get ItemId (Classsic SoD)

First time poster... I am in the very beginning stages of writing my first AddOn and its not going well. For this reason I am trying to just print messages for debugging and I am stuck with registering when right click happens, while left shift key is down, and the bank is open. Individually all 3 of these are satisfied through testing but my print message is not received let alone the rest of the function...

Ultimately I would like to retrieve the items information that was just right clicked. I know this is probably simple but I have searched all over and most of the help must be outdated or not compatible with Classic as any of the events I come across have zero documentation.

Code:
-- Create the frame
local frame = CreateFrame("Frame")

-- Set up event handling
frame:RegisterEvent("BANKFRAME_OPENED")
frame:RegisterEvent("BANKFRAME_CLOSED")

-- Function to handle events
frame:SetScript("OnEvent", function(self, event, ...)
    if event == "BANKFRAME_OPENED" then
        isBankOpen = true
        print("Bank frame opened!")
		print(isBankOpen)
    elseif event == "BANKFRAME_CLOSED" then
        isBankOpen = false
        print("Bank frame closed!")
		print(isBankOpen)
    end
end)

-- Set up script to handle item usage
frame:SetScript("OnMouseDown", function(self, button)
    if isBankOpen and IsLeftShiftKeyDown() and button == "RightButton" then
		print("Right Click Detected!")
        local bag, slot = self:GetRightClickedItem()
        if bag and slot then
            local itemId = GetContainerItemID(bag, slot)
            if itemId then
                print("Shift + Right-Click detected on item with ID:", itemId)
            end
        end
    else
        -- Default behavior to deposit the item into the bank
        print("Un-Modified Right-Click detected on an item!")
    end
end)

-- Function to get bag and slot of the right-clicked item
function frame:GetRightClickedItem()
    for bag = 0, NUM_BAG_SLOTS do
        for slot = 1, GetContainerNumSlots(bag) do
            local _, _, _, _, _, _, _, _, _, _, itemLink = GetContainerItemInfo(bag, slot)
            if itemLink then
                if IsModifiedClick("OPENALLBAGS") and GetCursorInfo() == nil then
                    return bag, slot
                end
            end
        end
    end
    return nil, nil
end
  Reply With Quote
12-15-23, 10:23 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Somethng to start with.

Place a button that looks like the keyring button in the TOPLEFT of the screen:
Lua Code:
  1. -- Create the frame
  2. local frame = CreateFrame("Button", nil, UIParent)
  3. frame.Texture = frame:CreateTexture()
  4. frame.Texture:SetAllPoints()
  5. frame.Texture:SetTexture("Interface\\Buttons\\UI-Button-KeyRing")
  6. frame:SetSize(40, 40)
  7. frame:SetPoint("TOPLEFT", 5, -5)
  8. frame:RegisterForClicks("RightButtonDown")
  9. frame:SetScript("OnClick", function(self, button, down)
  10.     print(format("Shift Key is: %s!", IsLeftShiftKeyDown() and "DOWN" or "UP"))
  11.     if IsLeftShiftKeyDown() then
  12.         print(format("Bank is: %s!", self.isBankOpen and "OPEN" or "CLOSED"))
  13.     end
  14. end)
  15. frame:RegisterEvent("BANKFRAME_OPENED")
  16. frame:RegisterEvent("BANKFRAME_CLOSED")
  17. frame:SetScript("OnEvent", function(self, event)
  18.     if event == "BANKFRAME_OPENED" then
  19.         self.isBankOpen = true
  20.     else
  21.         self.isBankOpen = false
  22.     end
  23. end)

This won't do anything with information about bank items as they are their own separate buttons and it depends on what information you want and what you want to do with it as the buttons are already placing the items on the cursor or opening the stack count selector.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-15-23 at 10:34 PM.
  Reply With Quote
12-15-23, 10:45 PM   #3
pflueger92
A Murloc Raider
Join Date: Oct 2022
Posts: 6
Thank you for the reply! So are you saying that you cannot override or tag along a Blizzard function? Such that I intercept the stack count option and instead execute something else? It appears I can only register clicks on my own widgets?

Also it is worth mentioning that I don't want to do anything with the items in the bank. I actually want to interact with items in my inventory but only if the bank is open.

Last edited by pflueger92 : 12-15-23 at 10:54 PM.
  Reply With Quote
12-15-23, 11:11 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Maybe this as a starting point to print the item hyperlink if the bank is open and left shift key is down (doesn't discrimiate the container the clicked item is in but you should be able to determine that using self:GetParent():GetID() as that should be the parent bag/bank slot id.):
Lua Code:
  1. local isBankOpen = false
  2. local function ItemHook(self, button)
  3.     if isBankOpen and IsLeftShiftKeyDown() then
  4.         print(C_Container.GetContainerItemInfo(self:GetParent():GetID(), self:GetID()).hyperlink)
  5.     end
  6. end
  7. local frame = CreateFrame("Frame")
  8. frame:RegisterEvent("BANKFRAME_OPENED")
  9. frame:RegisterEvent("BANKFRAME_CLOSED")
  10. frame:SetScript("OnEvent", function(self, event)
  11.     if event == "BANKFRAME_OPENED" then
  12.         isBankOpen = true
  13.     else
  14.         isBankOpen = false
  15.     end
  16. end)
  17.  
  18. hooksecurefunc("ContainerFrameItemButton_OnClick", ItemHook)
  19. hooksecurefunc("ContainerFrameItemButton_OnModifiedClick", ItemHook)

This primarily hooks the functions called by the Blizzard item slots depending on modified state (so maybe you only need one?)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-15-23 at 11:17 PM.
  Reply With Quote
12-15-23, 11:28 PM   #5
pflueger92
A Murloc Raider
Join Date: Oct 2022
Posts: 6
Wow, that is much tighter than the path I was going down. My biggest struggle is knowing the library available and syntax. How would I have know ContainerFrameItemButton_OnClick or ContainerFrameItemButton_OnModifiedClick was a thing? If I even google just that I get quite literally about zero results. I have browsed all over wowpedia which can be helpful but has not been for anything within this post.

Could you also please break down (C_Container.GetContainerItemInfo(self:GetParent():GetID(), self:GetID()).hyperlink)? It looks as though that requires container index and slot index which I don't quite see how those values are passed into this.

Thank you again for the help!
  Reply With Quote
12-15-23, 11:46 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Essentially each item slot button has an id which is it's position in the container so that's self:GetID() The parent of the slot is the container (bag/bank slot) and it also has an id so that's self:GetParent():GetID()

C_Container.GetContainerItemInfo uses those to return a table of information about what's in a given slot in a given container.

The hooked functions was from looking at the code for the item slot button (which is a standard torturous trail you can start with /fstack find the button name on down through multiple inherited templates).

Which all starts with exporting the code for the game UI and a good search tool or possibly Gethe's on-line export.

warcraft.wiki.gg is the new home of API documentation. wowpedia will get more and more stale as time goes by
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-15-23 at 11:55 PM.
  Reply With Quote
12-16-23, 12:48 AM   #7
pflueger92
A Murloc Raider
Join Date: Oct 2022
Posts: 6
You've been a huge help! I have peaked into all of those resources and will be using them religiously. I was disappointed to see having the bank open appears to change things up, as C_Container.UseContainerItem now places items in the bank (otherwise is blocked), and there doesn't appear to be a way to use an item in any way shape or form via lua? That's probably obvious to veterans.

I noticed UseItemByName, UseContainerItem, RunMacro, RunMacroText are all protected which I assume cannot be called?
  Reply With Quote
12-16-23, 01:33 AM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
The hooks just run after the original called function is actioned so they don't change hoe clicking an item fundamentally works. That would take a much more complicated approach which would change the bags/bank workings entirely.

The protected function can only be used in response to a hardware event (keyboard/mouse click) usually on a widget that derives from a secure template

I think this thread might be useful, at least in terms of the idea of how it works using a SecureActionButtonTemplate in conjunction with macro text.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-16-23 at 01:36 AM.
  Reply With Quote
12-16-23, 09:21 AM   #9
pflueger92
A Murloc Raider
Join Date: Oct 2022
Posts: 6
Sorry my previous post was probably a bit confusing. What I meant was your script worked great and I understand it. Completely separate from it I was discouraged to see that the blizzard API seems to work differently when at a bank in general, as C_Container.UseContainerItem is blocked unless you are at a bank even though it does not say it's protected. This must be because right clicking items at a bank or "using them" just deposits them rather than casts the item spell.

I had tried every way I could find to provoke using an item while at the bank which included RunMacro although that was just an effort to see if anything would work. The entire idea behind this was I wanted to create an addon that when you hold down shift and right click items while the bank window is open it would use the item rather than deposit it. The most practical example personally is enchanting dust going from Lesser to Greater and needing to close the bank, consolidate, then re-deposit. I know this is completely silly but I thought it would be a very easy first addon project for myself to learn from.

Based on your last reply this would be much much more complex to do?
  Reply With Quote
12-16-23, 10:01 AM   #10
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Originally Posted by pflueger92 View Post
Based on your last reply this would be much much more complex to do?
Yes, because your code hooks are not secure. I've not tried (maybe someone else has and knows better) but I presume at least you would have to replace the slot buttons with your own secure buttons including everything they do.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » On Right Click get ItemId (Classsic SoD)


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