-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilter.lua
More file actions
253 lines (220 loc) · 8.66 KB
/
Copy pathfilter.lua
File metadata and controls
253 lines (220 loc) · 8.66 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
---@class ns
local addon = select(2, ...)
-- BetterBags namespace
-----------------------------------------------------------
---@class BetterBags: AceAddon
local BetterBags = LibStub('AceAddon-3.0'):GetAddon("BetterBags")
---@class Categories: AceModule
---@field GetCategoryByName fun(self: Categories, name: string): CustomCategoryFilter|nil
---@field RemoveItemFromCategory fun(self: Categories, itemID: number): nil
---@field RegisterCategoryFunction fun(self: Categories, name: string, fn: fun(data: ItemData): string|nil): nil
---@field ephemeralCategories table<string, CustomCategoryFilter> -- private
---@field ephemeralCategoryByItemID table<number, CustomCategoryFilter>
local Categories = BetterBags:GetModule('Categories')
---@class Events: AceModule
local Events = BetterBags:GetModule('Events')
---@class Database: AceModule
---@field GetItemCategoryByItemID fun(self: Database, itemID: number): CustomCategoryFilter|nil
local Database = BetterBags:GetModule('Database')
-- Use the L:G() function to get the localized string.
---@class Localization: AceModule
local L = BetterBags:GetModule('Localization')
-- Lua API
-----------------------------------------------------------
local string_find = string.find
---@param inputString string
---@param patterns string[]
---@return boolean|nil
local function str_matchm(inputString, patterns)
for i = 1, #patterns do
if string_find(inputString, patterns[i]) then
return true
end
end
return nil
end
-- WoW API
-----------------------------------------------------------
local CreateFrame = CreateFrame
local C_TooltipInfo_GetBagItem = C_TooltipInfo and C_TooltipInfo.GetBagItem
local C_Item_IsEquippableItem = C_Item and C_Item.IsEquippableItem
-- Item quality constants
local QUALITY_POOR = 0
-- Bind type constants
local BIND_NONE = 0
local BIND_QUEST = 4
local BIND_UNUSED = 6
-----------------------------------------------------------
-- Filter Setup
-----------------------------------------------------------
local BOP_STRINGS = { ITEM_SOULBOUND, ITEM_BIND_ON_PICKUP }
local BOA_STRINGS = { ITEM_ACCOUNTBOUND, ITEM_BNETACCOUNTBOUND, ITEM_BIND_TO_ACCOUNT, ITEM_BIND_TO_BNETACCOUNT }
local WUE_STRINGS = { ITEM_ACCOUNTBOUND_UNTIL_EQUIP, ITEM_BIND_TO_ACCOUNT_UNTIL_EQUIP }
-- Tooltip used for scanning.
local _SCANNER = "AVY_ScannerTooltip"
--- Get the category of an item.
---@param bagIndex number
---@param slotIndex number
---@param itemInfo ExpandedItemInfo|nil
---@return string|nil
function addon:GetItemCategory(bagIndex, slotIndex, itemInfo)
local category = nil
--- Whether we have C_TooltipInfo APIs available
if (self.IsRetail) then
local tooltipInfo = C_TooltipInfo_GetBagItem(bagIndex, slotIndex)
if not tooltipInfo or not tooltipInfo.lines then return end
for i = 2, 6 do
local line = tooltipInfo.lines[i]
if (not line) then break end
local bind = self:GetBindString(line.leftText)
if (bind) then
category = bind
break
end
end
else
if itemInfo == nil then return end
if (itemInfo.bindType == 2 or itemInfo.bindType == 3) then
local Scanner = CreateFrame("GameTooltip", _SCANNER .. itemInfo.itemGUID, nil, "SharedTooltipTemplate")
Scanner:SetOwner(WorldFrame, "ANCHOR_NONE")
Scanner:ClearLines()
if bagIndex == BANK_CONTAINER then
Scanner:SetInventoryItem("player", BankButtonIDToInvSlotID(slotIndex, nil))
else
Scanner:SetBagItem(bagIndex, slotIndex)
end
local lines = self.GetTooltipLines(Scanner)
for _, line in ipairs(lines) do
if (line == '') then break end
local bind = self:GetBindString(line)
if (bind) then
category = bind
break
end
end
Scanner:Hide()
end
end
return category
end
---@param msg string
---@return string|nil
function addon:GetBindString(msg)
if (msg) then
if (string_find(msg, ITEM_BIND_ON_EQUIP)) then
return self.S_BOE
elseif (str_matchm(msg, WUE_STRINGS)) then
return self.S_WUE
elseif (str_matchm(msg, BOA_STRINGS)) then
return self.S_BOA
elseif (str_matchm(msg, BOP_STRINGS)) then
return self.S_BOP
end
end
end
---@param tooltip GameTooltip
function addon.GetTooltipLines(tooltip)
local textLines = {}
local regions = { tooltip:GetRegions() }
for _, r in ipairs(regions) do
if r:IsObjectType("FontString") then
table.insert(textLines, r:GetText())
end
end
return textLines
end
---@param category string|nil
---@return boolean
function addon:CategoryEnabled(category)
if (category == self.S_BOA) then
return self.db.enableBoa
elseif (category == self.S_BOE) then
return self.db.enableBoe
elseif (category == self.S_WUE) then
return self.db.enableWue
elseif (category == self.S_BOP) then
return self.db.enableBop
end
return false
end
---@param data ItemData
function addon:CategoryFilter(data)
local quality = data.itemInfo.itemQuality
local bindType = data.itemInfo.bindType
local equippable = C_Item_IsEquippableItem(data.itemInfo.itemID)
-- Early return for non-equippable if setting enabled
if (self.db.onlyEquippable and not equippable) then return nil end
-- Skip junk items (gray quality)
local isJunk = quality == QUALITY_POOR
if isJunk then return nil end
-- Skip items with no bind type
local hasNoBind = not bindType or bindType == BIND_NONE
if hasNoBind then return nil end
-- Skip quest items and unused binds (bind types 4-6)
local isQuestItem = bindType >= BIND_QUEST and bindType <= BIND_UNUSED
if isQuestItem then return nil end
-- Item qualifies for categorization
local category = self:GetItemCategory(data.bagid, data.slotid, data.itemInfo)
if (category ~= nil and self:CategoryEnabled(category)) then
return L:G(category)
end
return nil
end
local function GetCategory(itemID)
local category = Database:GetItemCategoryByItemID(itemID)
if (category and category.name) then return category.name end
-- this might break due to using internals of the Categories module
category = Categories.ephemeralCategoryByItemID[itemID]
if (category and category.name) then return category.name end
return nil
end
---@param slot number
function addon:RemoveBindConfirmFromCategory(slot)
if self.bindConfirm == nil then return end
if not self.IsRetail then return end
local id = self.bindConfirm.id
local itemID = C_Item.GetItemID({ equipmentSlotIndex = slot })
local category = self.bindConfirm.category
local categoryName = GetCategory(itemID)
-- ensure we're deleting an item from the correct category
if (itemID ~= id or category ~= categoryName) then return end
if (category == L:G(self.S_BOE) or category == L:G(self.S_WUE)) then
Categories:RemoveItemFromCategory(itemID)
self.bindConfirm = nil -- Clear the bind confirm
end
end
-- Check if the priority addon is available
local BetterBagsPriority = LibStub('AceAddon-3.0'):GetAddon("BetterBags_Priority", true)
local priorityEnabled = BetterBagsPriority ~= nil or false
if (priorityEnabled) then
---@class PriorityCategories: AceModule
---@field RegisterCategoryFunction fun(self: PriorityCategories, name: string, filterName: string, fn: fun(data: ItemData): string|nil): nil
local PriorityCategories = BetterBagsPriority:GetModule('Categories')
-- this is required because we have multiple categories and can't really register a single function for all of them
local cat = Categories:GetCategoryByName(L:G("Bound"))
if not cat then
Categories:CreateCategory(addon.context:New("Bound_Create_UmbrellaCat"), {
name = L:G("Bound"),
itemList = {},
})
end
-- If the priority addon is available, we register the custom category as an empty filter with BetterBags to keep the
-- "enable system" working. The actual filtering will be done by the priority addon
Categories:RegisterCategoryFunction("BoEBoAItemsCategoryFilter", function() return nil end)
-- categoriesWithPriority:RegisterCategoryFunction("YOUR_ADDON_TITLE", "YOUR_FILTER_NAME_HERE", fn)
PriorityCategories:RegisterCategoryFunction(L:G("Bound"), "BoEBoAItemsCategoryFilter", function(data)
return addon:CategoryFilter(data)
end)
else
-- Use this API to register a function that will be called for every item in the player's bags.
-- The function you provide will be given an ItemData table, which contains all properties of an item
-- loaded from the Blizzard API. From here, you can call any custom code you want to analyze the item.
-- Your function must return a string, which is the category name that the item should be placed in.
-- If your function returns nil, the item will not be placed in any category.
-- Results of this function, including nil, are cached, so you do not need to worry about performance
-- after the first scan.
-- Your current code goes here to maintain the current behaviour if the priority addon isn't enabled
Categories:RegisterCategoryFunction("BoEBoAItemsCategoryFilter", function(data)
return addon:CategoryFilter(data)
end)
end