· 6 years ago · Oct 16, 2019, 07:38 AM
1-- Note: Run this only once. If you load it twice, you may have
2-- unexpected results. If that does happen, just reload your UI
3-- and reload this script, but ONCE this time.
4--
5---------------------------------------------------------------
6-- Instructions for use (After loading the script, of course!):
7---------------------------------------------------------------
8--
9-- Step 1: Open The auction window and any professions window and
10-- navigate to a spell.
11--
12-- Step 2: ALT and LEFT-CLICK the reagent or item you want to
13-- search for in the trade skills window.
14--
15-- Step 3: Profit!
16--
17-- Note: It doesn't matter which order Steps 1 & 2 are carried
18-- out in, so long as both windows are open.
19
20
21-- A quick note on `self`. When calling a function on a table like this:
22--
23-- tbl:func() (note the colon)
24--
25-- it's the equivalent of:
26--
27-- tbl.func(tbl)
28--
29-- It's syntactical sugar and table passes itself in as the first argument
30-- to the function. However, self is set implicitly. It does not appear in
31-- the function signature unless no colon is used.
32
33-- A quick note on global variables (and functions):
34--
35-- _G["someString"] is how we access and set global variables. You can just
36-- call the global without _G, for example, MyAddon instead of _G["MyAddon"],
37-- but _G makes it possible to use dynamically generated strings to call a
38-- global. For example, if you know you have 5 globals, Var1...Var5, in a
39-- loop, you can call _G["Var"..i], where `i` is a number.
40
41
42------------------------------------------------------------------------
43
44-- Create a frame to register events on. This will also serve as a table
45-- on which to bolt all of our functions.
46local frame = CreateFrame("Frame")
47
48-------------------------------------------------
49-- Search the auction house for the given string.
50-------------------------------------------------
51local function SearchAuctionHouseForItem(str)
52 -- No string, no service. Sorry!
53 if not str then return end
54
55 -- Does the auction frame exist and is the browse frame visible?
56 -- Sometimes we need to test for the existance of a frame, because
57 -- a lot of Blizzard's addons are load-on-demand, and therefore won't
58 -- always exist.
59 if AuctionFrame and AuctionFrameBrowse and AuctionFrameBrowse:IsVisible() then
60
61 -- WoW Retail function. Call if this BrowseResetButton frame exists.
62 if BrowseResetButton then
63 AuctionFrameBrowse_Reset(BrowseResetButton)
64 end
65
66 AuctionFrameBrowse.page = 0 -- Set the auction page to 0
67 BrowseName:SetText(str) -- Set the item to search for.
68 AuctionFrameBrowse_Search() -- Go go go!
69 end
70end
71
72-----------------------------------------
73-- Handle Alt + Left-click on a reagent.
74-----------------------------------------
75local function ClickReagantLinkHook(self, button)
76 if button == "LeftButton" and IsAltKeyDown() then
77 local reagentName = getglobal(self:GetName().."Name"):GetText()
78 SearchAuctionHouseForItem(reagentName)
79 end
80end
81
82----------------------------------------------------
83-- Handle ALT + Left-click on the Trade Skill icon.
84----------------------------------------------------
85local function ClickCraftIconHook(self, button)
86 if button == "LeftButton" and IsAltKeyDown() then
87 SearchAuctionHouseForItem(CraftName:GetText())
88 end
89end
90
91-------------------------------------------------------------------------
92-- Handle Alt + Left-click on the Craft Skill icon.
93-------------------------------------------------------------------------
94-- This needs a separate function from the above because of the naming
95-- inconsistency. The icon for Crafting is called "CraftIcon" but the one
96-- for Trade Skills is called "TradeSkillSkillIcon". I mean, SkillSkill?
97-- Heard you the first time, Blizzard. Ever heard of DRY? ;-)
98-------------------------------------------------------------------------
99function ClickTradeSkillIconHook(self, button)
100 if button == "LeftButton" and IsAltKeyDown() then
101 SearchAuctionHouseForItem(TradeSkillSkillName:GetText())
102 end
103end
104
105-------------------------------------
106-- Handle the TRADE_SKILL_SHOW event.
107-------------------------------------
108function frame:TRADE_SKILL_SHOW()
109 -- We only need to call this once for initialization when the frame
110 -- is shown for the first time, so unregister the event.
111 frame:UnregisterEvent("TRADE_SKILL_SHOW")
112
113 -- Hook onclick event onto the Trade Skill icon.
114 -- Since we can't use a colon here, we have to pass the frame in as the first argument
115 TradeSkillSkillIcon:HookScript("OnClick", ClickTradeSkillIconHook)
116
117
118 -- Hook an OnCick handler onto each reagent button.
119 for i=1, MAX_TRADE_SKILL_REAGENTS do
120 local reagentButton = _G["TradeSkillReagent"..i]
121 reagentButton:HookScript("OnClick", ClickReagantLinkHook)
122 end
123end
124
125-------------------------------
126-- Handle the CRAFT_SHOW event.
127-------------------------------
128function frame:CRAFT_SHOW()
129 -- We only need to call this once for initialization when the frame
130 -- is shown for the first time, so unregister the event.
131 frame:UnregisterEvent("CRAFT_SHOW")
132
133 -- Hook onclick event onto the Trade Skill icon.
134 CraftIcon:HookScript("OnClick", ClickCraftIconHook)
135
136 -- Hook an OnCick handler onto each reagent button.
137 for i=1, MAX_CRAFT_REAGENTS do
138 local reagentButton = _G["CraftReagent"..i]
139 reagentButton:HookScript("OnClick", ClickReagantLinkHook)
140 end
141end
142
143-- Register the event handler. When an event is fired, the method will be
144-- called on the frame object for the event. For example, when the
145-- CRAFT_SHOW event fires, frame:CRAFT_SHOW() will be called.
146frame:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)
147
148-- Register some events for when the Trade and craft windows are open.
149-- If the've already been opened, certain constants will be set, so if
150-- they are set, just run the intialisation functions instead.
151if not MAX_TRADE_SKILL_REAGENTS then
152 frame:RegisterEvent("TRADE_SKILL_SHOW")
153else
154 frame:TRADE_SKILL_SHOW()
155end
156
157if not MAX_CRAFT_REAGENTS then
158 frame:RegisterEvent("CRAFT_SHOW")
159else
160 frame:CRAFT_SHOW()
161end