-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestroyJunk.lua
executable file
·208 lines (177 loc) · 6.71 KB
/
DestroyJunk.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- Destroy junk items on request, and sell junk items to vendor automatically
-- written by X'Genesis Qhulut
-- November 2022
-- This destroys "gray" (poor quality) items in your bags
-- plus items listed below in otherJunk which tend to drop in instances but are
-- effectively junk when I am trying to fill up my bags with blue/green items.
-- It destroys a maximum of ITEM_LIMIT items, so if you are close to a vendor you don't throw away
-- a lot of stuff that might make a few gold.
local YELLOW_TEXT = '|cFFFFFF00' -- (format is |caarrggbb where aa is alpha and should be FF)
-- to detect poor quality items
local GREY_COLOUR = "ff9d9d9d"
local ITEM_LIMIT = 5 -- max number to destroy each time, make zero for no limit
local SOUND_TO_PLAY = "igPVPUpdate" -- sound to play if could not destroy anything
-- Normal (quality 1) items to also destroy.
-- These are Lua patterns however we add ^ to the start and $ to the end for you.
local otherJunk = {
"Alterac Swiss",
"Black Diamond",
"Boar Intestines",
"Coal", -- warning: needed for Thorium Brotherhood
"Cured Ham Steak",
"Delicious Cave Mold",
"Dried King Bolete",
"Fine Aged Cheddar",
"Fish Oil",
"Flask of Oil",
"Heavy Stone",
"Homemade Cherry Pie",
"Moonberry Juice",
"Moon Harvest Pumpkin",
"Morning Glory Dew", -- not at level 45
"Murloc Eye",
"Murloc Fin",
"Mystery Meat",
-- "Nightcrawlers",
"Raw Black Truffle",
"Red Wolf Meat",
-- "Roasted Quail",
-- "Scroll of [SIP]%a+ [IXV]+", -- Stamina, Strength, Intellect, Protection, Spirit -- exclude "Mizrael"
"Shiny Fish Scales",
"Slimy Murloc Scale",
"Soft Banana Bread",
"Tender Wolf Meat",
"Unadorned Seal of Ascension",
"Wicked Claw",
-- add more here
} -- end of otherJunk
-- trim leading and trailing spaces from a string
local function trim (s)
return (string.gsub (s, "^%s*(.-)%s*$", "%1"))
end -- trim
-- make a print function to simplify sending messages
local function print(...)
-- ensure all arguments are strings
-- we do it without using ipairs because that stops on the first nil
-- but the nil is still in the table which makes table.concat fail
-- (in case nil is an argument to the print function)
for i = 1, table.getn (arg) do
arg [i] = tostring (arg [i]) -- ensure a string
end -- for
-- contcatenate them with a space inbetween
local s = table.concat (arg, " ")
-- show them in purple
DEFAULT_CHAT_FRAME:AddMessage(s, 1.0, 0.2, 0.55)
end -- print
-- returns 's' if count is not one, otherwise an empty string
local function plural (count)
if count == 1 then
return ''
else
return 's'
end -- if
end -- plural
-- determines if a particular inventory bag and slot contains a junk item
-- if so, returns true and the item link, so it can be displayed
-- if not, returns false
local function isItJunk (bag, slot)
local texture, itemCount, locked, quality = GetContainerItemInfo(bag, slot)
if not quality then
return false
end -- if no item in that slot
local itemLink = GetContainerItemLink(bag, slot)
local _, _, colour, linkType, itemID = string.find (itemLink, "^|c(%x+)|?H?([^:]+):(%d+)")
-- some stuff like "Dripping Spider Mandible" had a quality of -1 for some reason
local _, _, name = string.find (itemLink, "|h%[(.-)%]|")
-- check other items if quality white or less
if quality <= 1 then
for k, v in ipairs (otherJunk) do
if string.find (name, "^" .. v .. "$") then
quality = 0
break -- done with checking names
end -- if match found
end -- for
end -- if
if (quality == 0 or -- poor quality
(quality == -1 and colour == GREY_COLOUR and linkType == "item")) then
return true, itemLink
else
return false
end -- if
end -- isItJunk
-- here when "/dj destroy" is typed
function destroyJunk()
local count = 0
for bag = 0, 4 do -- 0 is backpack, 1 to 4 is each of the 4 bags
local slots = GetContainerNumSlots(bag) -- find how many slots in container
if slots then
for slot = 1, slots do
local junk, itemLink = isItJunk (bag, slot)
if junk and (count < ITEM_LIMIT or ITEM_LIMIT == 0) then
print ("Destroying " .. itemLink)
PickupContainerItem(bag, slot)
DeleteCursorItem()
count = count + 1
if count >= ITEM_LIMIT and ITEM_LIMIT ~= 0 then
print ("Destroyed " .. ITEM_LIMIT .. " items, stopping now.")
end -- if done ITEM_LIMIT items
end -- if poor quality
end -- for each slot
end -- if container exists
end -- for each container
print (count .. " junk item" .. plural (count) .. " destroyed.")
if count <= 0 and SOUND_TO_PLAY then -- if nothing destroyed, warn them
PlaySound (SOUND_TO_PLAY)
end -- if
end -- function destroyJunk
-- here when visiting a vendor
function sellJunk(event)
local count = 0
for bag = 0, 4 do -- 0 is backpack, 1 to 4 is each of the 4 bags
local slots = GetContainerNumSlots(bag) -- find how many slots in container
if slots then
for slot = 1, slots do
local junk, itemLink = isItJunk (bag, slot)
if junk then
print ("Selling " .. itemLink)
UseContainerItem (bag, slot)
count = count + 1
end -- if poor quality
end -- for each slot
end -- if container exists
end -- for each container
print (count .. " junk item" .. plural (count) .. " sold.")
end -- function sellJunk
local function slashHandler (msg)
if msg and msg ~= "" then
msg = trim (string.lower (msg))
if msg == "destroy" then
local result, err = pcall (destroyJunk)
if not result then
print (YELLOW_TEXT .. "Error in destroyJunk: " .. err)
end -- if failure
else
print ("Did not recognise slash command: " .. msg)
end
end -- if have a message
end -- slashHandler
-- ---------------------------------------------------------------
-- START HERE
-- ---------------------------------------------------------------
function DestroyJunkInit()
-- set up a slash command handler
SLASH_DESTROYJUNK1 = "/dj"
SlashCmdList["DESTROYJUNK"] = slashHandler
this:RegisterEvent("MERCHANT_SHOW");
print ("To destroy your junk: /dj destroy")
print ("Junk will automatically be sold at a vendor.")
end -- end of DestroyJunkInit
function DestroyJunkEventHandler(event)
sellJunk (event)
end -- function DestroyJunkEventHandler
function DestroyJunkEvent(...)
local result, err = pcall (DestroyJunkEventHandler, unpack (arg))
if not result then
print (YELLOW_TEXT .. "Error in DestroyJunkEventHandler: " .. err)
end -- if failure
end -- DestroyJunkEvent