Skip to content

Commit

Permalink
Merge pull request #1 from TErenberger/feature/use-set-name
Browse files Browse the repository at this point in the history
Utilize setName as category name instead of generic const Sets
  • Loading branch information
DJSchaffner authored Feb 21, 2024
2 parents 309e571 + 1020d81 commit c381f99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion BetterBags_ItemRack.toc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## Title: BetterBags - ItemRack
## Notes: Add a category to BetterBags for ItemRack sets
## Author: DJSchaffner
## Version: @1.2.1
## Version: @1.3.0
## Dependencies: BetterBags

main.lua
54 changes: 42 additions & 12 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ local L = addonBetterBags:GetModule('Localization')

local debug = false
local frame = CreateFrame("Frame", nil)
local categoryName = "Sets"
local customCategories = {}
-------------------------------------------------------
local function printChat(message)
if debug == true then
print("[BetterBags ItemRack] "..message)
print("[BetterBags ItemRack] ".. tostring(message))
end
end

Expand All @@ -25,11 +25,18 @@ local function split(s, sep)
return fields
end

local function updateCategory()
-- Wipe category since we can't retrieve deleted set from itemRack (Except maybe store duplicate of sets and check last version of it)
categories:WipeCategory(L:G(categoryName))
local function updateCategories()
-- Wipe custom categories since we can't retrieve deleted set from itemRack (Except maybe store duplicate of sets and check last version of it)
for category, _ in pairs(customCategories) do
-- @TODO completely remove label as custom category from BetterBags
categories:WipeCategory(L:G(category))
printChat("Wiped category '" .. L:G(category) .. "'")
end

-- Keep track of all used items and their associated sets
local usedItems = {}

-- Loop all sets
-- Loop all sets and collect items
for setName, _ in pairs(ItemRackUser.Sets) do
-- Only update user sets (internals start with '~')
if not string.match(setName, "^~") then
Expand All @@ -40,29 +47,52 @@ local function updateCategory()

-- Adding items that don't exist causes errors
if id ~= 0 then
categories:AddItemToCategory(id, L:G(categoryName))
--printChat("Added item '" .. id .. "' to '" .. categoryName .. "' category")
local itemSets = usedItems[id]

if itemSets == nil then
usedItems[id] = { setName }
-- Extend existing labels
else
table.insert(usedItems[id], setName)
end
end
end
else
printChat("Skipping internal set: " .. setName)
end
end

-- Loop collected items and add them to their respective categories
for item, sets in pairs(usedItems) do
local label = nil

if #sets == 1 then
label = "Set: " .. sets[1]
else
label = "Sets: ".. table.concat(sets, ", ")
end

customCategories[L:G(label)] = true
categories:AddItemToCategory(item, L:G(label))
-- printChat("Added item '" .. id .. "' to '" .. label .. "' category")
end
end

local function itemRackUpdated(event, _)

local function itemRackUpdated(event, eventData)
printChat(event)
updateCategory()
updateCategories()
end
-------------------------------------------------------
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addon, ...)
if event == "ADDON_LOADED" and addon == "ItemRack" then
ItemRack:RegisterExternalEventListener("ITEMRACK_SET_SAVED", itemRackUpdated)
ItemRack:RegisterExternalEventListener("ITEMRACK_SET_DELETED", itemRackUpdated)


printChat("ItemRack Loaded..")
printChat("Initializing Category..")
updateCategory()
printChat("Initializing Categories..")
updateCategories()
end
end)

0 comments on commit c381f99

Please sign in to comment.