forked from phuein/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJunkee.lua
More file actions
326 lines (287 loc) · 10.4 KB
/
Copy pathJunkee.lua
File metadata and controls
326 lines (287 loc) · 10.4 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Junkee = Junkee or {}
Junkee.__index = Junkee
Junkee.name = "Junkee2018"
local LAM = LibStub("LibAddonMenu-2.0")
local em = EVENT_MANAGER
local INVENTORIES_TO_HOOK = {INVENTORY_BACKPACK, INVENTORY_BANK}
Junkee.slotControl = nil -- Reference for misc' properties.
Junkee.bagId = nil
Junkee.slotId = nil
Junkee.isJunk = false
-- Defaults.
Junkee.savedVars = {
visible = true, -- All items visible.
DestroyVisible = true,
LinkVisible = true,
LockVisible = true,
firstRun = true,
-- Each command name will have "/" prepended to it automatically.
slashCmds = {
GroupLeave = {
cmd = "/gl",
active = false,
f = function()
if IsUnitGrouped("player") then
GroupLeave()
end
end
},
GroupDisband = {
cmd = "/gd",
active = false,
f = function()
if IsUnitGrouped("player") and IsUnitGroupLeader("player") then
GroupDisband()
end
end
},
}
}
Junkee.OnMouseEnter = function(control)
Junkee.slotControl = control
Junkee.bagId = control.dataEntry.data.bagId
Junkee.slotId = control.dataEntry.data.slotIndex
Junkee.isJunk = control.dataEntry.data.isJunk
if Junkee.savedVars.visible then
Junkee.AddJunkAction()
end
end
Junkee.OnMouseExit = function(control)
Junkee.slotControl = nil
Junkee.bagId = nil
Junkee.slotId = nil
Junkee.isJunk = false
if Junkee.savedVars.visible then
Junkee.RemoveJunkAction()
end
end
local function registerHook(inventory)
local listView = inventory.listView
if listView and listView.dataTypes and listView.dataTypes[1] then
local originalCallback = listView.dataTypes[1].setupCallback
listView.dataTypes[1].setupCallback = function(rowControl, slot)
originalCallback(rowControl, slot)
ZO_PreHookHandler(rowControl, "OnMouseEnter", Junkee.OnMouseEnter)
ZO_PreHookHandler(rowControl, "OnMouseExit", Junkee.OnMouseExit)
end
end
end
local function registerHooks()
for _, index in pairs(INVENTORIES_TO_HOOK) do
registerHook(PLAYER_INVENTORY.inventories[index])
end
end
-- Add menu with options.
local panelData = {
type = "panel",
name = "Junkee 2018",
displayName = "Junkee Settings",
registerForRefresh = true,
registerForDefaults = true,
}
local optionsTable = {
[1] = {
type = "checkbox",
name = "Display Keybindings",
tooltip = "Display the addon's keybindings when opening the Inventory. " ..
"They appear on the bottom left.",
getFunc = function() return Junkee.savedVars.visible end,
setFunc = function(v) Junkee.savedVars.visible = v end,
width = "full", --or "half",
},
[2] = {
type = "checkbox",
name = "Display the Destroy Keybinding",
tooltip = "Display the addon's keybinding for Destroy when opening the Inventory.",
getFunc = function()
return Junkee.savedVars.visible and Junkee.savedVars.DestroyVisible
end,
setFunc = function(v) Junkee.savedVars.DestroyVisible = v end,
width = "full", --or "half",
},
[3] = {
type = "checkbox",
name = "Display the Link Keybinding",
tooltip = "Display the addon's keybinding for Link when opening the Inventory.",
getFunc = function()
return Junkee.savedVars.visible and Junkee.savedVars.LinkVisible
end,
setFunc = function(v) Junkee.savedVars.LinkVisible = v end,
width = "full", --or "half",
},
[4] = {
type = "checkbox",
name = "Display the Lock Keybinding",
tooltip = "Display the addon's keybinding for Lock when opening the Inventory.",
getFunc = function()
return Junkee.savedVars.visible and Junkee.savedVars.LockVisible
end,
setFunc = function(v) Junkee.savedVars.LockVisible = v end,
width = "full", --or "half",
},
[5] = {
type = "checkbox",
name = Junkee.savedVars.slashCmds["GroupLeave"].cmd,
tooltip = "Chat command to leave your group.",
getFunc = function()
return Junkee.savedVars.slashCmds["GroupLeave"].active
end,
setFunc = function(v)
local cmd = Junkee.savedVars.slashCmds["GroupLeave"].cmd
Junkee.savedVars.slashCmds["GroupLeave"].active = v
if v then
SLASH_COMMANDS[cmd] = Junkee.savedVars.slashCmds["GroupLeave"].f
else
SLASH_COMMANDS[cmd] = nil
end
-- Reset autocomplete cache to update it.
SLASH_COMMAND_AUTO_COMPLETE:InvalidateSlashCommandCache()
end,
width = "full", --or "half",
},
[6] = {
type = "checkbox",
name = Junkee.savedVars.slashCmds["GroupDisband"].cmd,
tooltip = "Chat command to disband your group.",
getFunc = function()
return Junkee.savedVars.slashCmds["GroupDisband"].active
end,
setFunc = function(v)
local cmd = Junkee.savedVars.slashCmds["GroupDisband"].cmd
Junkee.savedVars.slashCmds["GroupDisband"].active = v
if v then
SLASH_COMMANDS[cmd] = Junkee.savedVars.slashCmds["GroupDisband"].f
else
SLASH_COMMANDS[cmd] = nil
end
-- Reset autocomplete cache to update it.
SLASH_COMMAND_AUTO_COMPLETE:InvalidateSlashCommandCache()
end,
width = "full", --or "half",
},
}
local function LoadMenu()
LAM:RegisterAddonPanel("Junkee 2018", panelData)
LAM:RegisterOptionControls("Junkee 2018", optionsTable)
end
Junkee.Loaded = function(eventCode, addonName)
if (Junkee.name == addonName) then
em:UnregisterForEvent(Junkee.name, EVENT_ADD_ON_LOADED)
registerHooks()
-- Load saved variables.
Junkee.savedVars = ZO_SavedVars:New("JunkeeAddonSavedVars", 2, nil, Junkee.savedVars)
-- Settings menu.
LoadMenu()
-- Chat /slash commands.
local newCmds = false
for name, item in pairs(Junkee.savedVars.slashCmds) do
if item.active then
newCmds = true
SLASH_COMMANDS[item.cmd] = item.f
end
end
-- Reset autocomplete cache to update it.
if newCmds then
SLASH_COMMAND_AUTO_COMPLETE:InvalidateSlashCommandCache()
end
end
end
em:RegisterForEvent(Junkee.name, EVENT_ADD_ON_LOADED, Junkee.Loaded)
Junkee.JunkIt = function()
if Junkee.bagId == nil then return end
local isJunk = IsItemJunk(Junkee.bagId, Junkee.slotId)
SetItemIsJunk(Junkee.bagId, Junkee.slotId, not isJunk)
if isJunk then
PlaySound(SOUNDS.INVENTORY_ITEM_UNJUNKED)
else
PlaySound(SOUNDS.INVENTORY_ITEM_JUNKED)
end
end
Junkee.DeleteIt = function()
if Junkee.bagId == nil then return end
DestroyItem(Junkee.bagId, Junkee.slotId)
end
local function createJunkStripDescriptor(name)
return JunkeeKeyStrip:New(name, "JUNKEE_JUNK_IT", Junkee.JunkIt)
end
local junkStripDescriptor = createJunkStripDescriptor(Junkee.tr("JunkLabel"))
local unjunkStripDescriptor = createJunkStripDescriptor(Junkee.tr("UnjunkLabel"))
local deleteStripDescriptor = JunkeeKeyStrip:New(Junkee.tr("DeleteLabel"), "JUNKEE_DELETE_IT", Junkee.DeleteIt)
local linkStripDescriptor = JunkeeKeyStrip:New(Junkee.tr("LinkLabel"), "JUNKEE_LINK_IT", Junkee.LinkIt)
local lockStripDescriptor = JunkeeKeyStrip:New(Junkee.tr("LockLabel"), "JUNKEE_LOCK_IT", Junkee.LockIt)
Junkee.AddJunkAction = function()
if (Junkee.isJunk) then
junkStripDescriptor:Remove()
unjunkStripDescriptor:Add(true)
else
unjunkStripDescriptor:Remove()
junkStripDescriptor:Add(true)
end
if Junkee.savedVars.DestroyVisible then deleteStripDescriptor:Add(true) end
if Junkee.savedVars.LinkVisible then linkStripDescriptor:Add(true) end
if Junkee.savedVars.LockVisible then lockStripDescriptor:Add(true) end
end
Junkee.RemoveJunkAction = function()
unjunkStripDescriptor:Remove()
junkStripDescriptor:Remove()
deleteStripDescriptor:Remove()
linkStripDescriptor:Remove()
lockStripDescriptor:Remove()
end
-- Link item in chat.
Junkee.LinkIt = function()
if Junkee.bagId == nil then return end
local link = GetItemLink(Junkee.bagId, Junkee.slotId, 1)
ZO_LinkHandler_InsertLink(link)
end
-- Un/Lock item.
Junkee.LockIt = function()
if Junkee.bagId == nil then return end
local bag, index = Junkee.bagId, Junkee.slotId
local locking = not IsItemPlayerLocked(bag, index) -- The locking state to apply.
if CanItemBePlayerLocked(bag, index) then
SetItemIsPlayerLocked(bag, index, locking)
PlaySound(not locking and SOUNDS.INVENTORY_ITEM_LOCKED or SOUNDS.INVENTORY_ITEM_UNLOCKED)
end
-- Below is taken from the game code for locking.
-- IsItemAlreadySlottedToCraft() errors,
-- so until that's solved I use the above code. Reference:
-- http://www.esoui.com/forums/showthread.php?p=34500
-- local inventorySlot = Junkee.slotControl
-- local bag, index = Junkee.bagId, Junkee.slotId -- ZO_Inventory_GetBagAndIndex(inventorySlot)
-- local locking = not IsItemPlayerLocked(bag, index) -- The locking state to apply.
-- local action
-- if locking then
-- action = SI_ITEM_ACTION_MARK_AS_LOCKED
-- -- Can't lock these.
-- if IsItemAlreadySlottedToCraft(inventorySlot) then return end
-- else
-- action = SI_ITEM_ACTION_UNMARK_AS_LOCKED
-- end
-- if CanItemBePlayerLocked(bag, index) and
-- not QUICKSLOT_WINDOW:AreQuickSlotsShowing() then
-- slotActions:AddSlotAction(action,
-- function() MarkAsPlayerLockedHelper(bag, index, locking) end,
-- "secondary")
-- end
end
-- Needed to bind CTRL/Shift+KEY.
function KEYBINDING_MANAGER:IsChordingAlwaysEnabled()
return true
end
ZO_CreateStringId("SI_BINDING_NAME_JUNKEE_JUNK_IT", Junkee.tr("JunkBindingName"))
ZO_CreateStringId("SI_BINDING_NAME_JUNKEE_DELETE_IT", Junkee.tr("DeleteBindingName"))
ZO_CreateStringId("SI_BINDING_NAME_JUNKEE_LINK_IT", Junkee.tr("LinkBindingName"))
ZO_CreateStringId("SI_BINDING_NAME_JUNKEE_LOCK_IT", Junkee.tr("LockBindingName"))
-- Act when everything is ready, for sure,
-- because player has acted.
local function OnActivated(eventCode, initial)
em:UnregisterForEvent(addonName, eventCode)
if Junkee.savedVars.firstRun then
Junkee.savedVars.firstRun = false
-- Do on first run of addon.
d("Junkee recommends these keybindings:\n" ..
"Junk/UnJunk = Z, Destroy = Shift+Z, Link = F2, Lock = Tab.")
end
end
em:RegisterForEvent(Junkee.name, EVENT_PLAYER_ACTIVATED, OnActivated)