Skip to content

Commit

Permalink
Equip location sorting + inventory validation
Browse files Browse the repository at this point in the history
- Change how sorting for Eqiup Location is handled. Previously, the game names (ex: INVTYPE_LEGS) or localized (ex: Legs) were offered. Both have been replaced with a custom sort based on character sheet order, working from top to bottom on the left, same on the right, then left to right at the bottom.
- Any existing Sort Orders containing either of the old Equip Location fields will be updated to use the new one.
- Sort Orders now get cleaned at startup and any invalid fields are removed.
- Added Equip Location to all the default Sort Orders (#13).
- As part of this change, inventory cache validation has been added. This should reduce the chance of errors due to item cache fields being added or removed.
- ItemInfo has been lightly refactored so it no longer needs to load so late. This was needed to support the cache validation code.
- ItemInfo:InitializeItem() has been updated to be usable for cache validation.
- InitializeEmptySlotItem() was moved from Inventory to ItemInfo, where it probably should have been all along.
  • Loading branch information
veechs committed Jan 18, 2025
1 parent 7e83c9f commit 837ef6a
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 170 deletions.
81 changes: 74 additions & 7 deletions Bagshui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,13 @@ local bagshuiEnvironment = {
bagType = "",
charges = 0,
count = 0,
equipLocationLocalized = "",
equipLocation = "",
emptySlot = 0, -- Using 0/1 instead of true/false for easy sorting
equipLocationLocalized = "",
equipLocationSort = 0,
emptySlot = 0, -- Using 0/1 instead of true/false for easy sorting.
id = 0,
itemLink = "", -- Item link (|cffffffff|Hitem:12345:0:0:0|h[Name]|h|r)
itemString = "", -- Item string (item:12345:0:0:0)
itemLink = "", -- Item link (|cffffffff|Hitem:12345:0:0:0|h[Name]|h|r).
itemString = "", -- Item string (item:12345:0:0:0).
locked = 0,
maxStackCount = 0,
minLevel = "",
Expand All @@ -330,7 +331,7 @@ local bagshuiEnvironment = {
tooltip = "",
texture = "Interface\\Icons\\INV_Misc_QuestionMark",
type = "",
uncategorized = 0, -- Using 0/1 instead of true/false for easy sorting
uncategorized = 0, -- Using 0/1 instead of true/false for easy sorting.

bagshuiGroupId = "",
bagshuiCategoryId = "",
Expand All @@ -357,8 +358,7 @@ local bagshuiEnvironment = {
"bagType",
"charges",
"count",
"equipLocationLocalized",
"equipLocation",
"equipLocationSort",
"emptySlot",
"id",
"itemLink",
Expand All @@ -373,6 +373,22 @@ local bagshuiEnvironment = {
"uncategorized",
},

-- ItemInfo:InitializeItem() will never reset these properties.
---@type table<string, boolean>
BS_ITEM_PROTECTED_PROPERTIES = {
bagNum = true,
bagType = true,
slotNum = true,
},

-- ItemInfo:InitializeItem() will not set these properties to default if nil.
-- Typically needed for properties that are used for empty slot identification.
---@type table<string, boolean>
BS_ITEM_NIL_PROPERTIES = {
itemLink = true,
itemString = true,
},

-- Rule functions that correspond to item properties.
-- This controls what appears in the Item Information menu, tooltip, and window.
-- ```
Expand Down Expand Up @@ -486,6 +502,42 @@ local bagshuiEnvironment = {
---@type string[]
BS_ITEM_PROPERTIES_SORTED = nil,

-- Used to get numbers for slot names so they can be sorted.
---@type table<string, number>
BS_INVENTORY_EQUIP_LOCATION_SORT_ORDER = {
INVTYPE_HEAD = 1,
INVTYPE_NECK = 2,
INVTYPE_SHOULDER = 3,
INVTYPE_CLOAK = 4, -- Back
INVTYPE_CHEST = 5,
INVTYPE_ROBE = 5, -- Chest
INVTYPE_BODY = 6, -- Shirt
INVTYPE_TABARD = 7,
INVTYPE_WRIST = 8,
INVTYPE_HAND = 9,
INVTYPE_WAIST = 10,
INVTYPE_LEGS = 11,
INVTYPE_FEET = 12,
INVTYPE_FINGER = 13,
INVTYPE_FINGER_OTHER = 14,
INVTYPE_TRINKET = 15,
INVTYPE_TRINKET_OTHER = 16,
INVTYPE_2HWEAPON = 17, -- Main Hand
INVTYPE_WEAPON = 17, -- Main Hand
INVTYPE_WEAPONMAINHAND = 17, -- Main Hand
INVTYPE_HOLDABLE = 18, -- Offhand
INVTYPE_SHIELD = 18, -- Offhand
INVTYPE_WEAPON_OTHER = 18, -- Offhand
INVTYPE_WEAPONOFFHAND = 18, -- Offhand
INVTYPE_RANGED = 19,
INVTYPE_RELIC = 19, -- Ranged
INVTYPE_WAND = 19, -- Ranged
INVTYPE_GUN = 19, -- Ranged
INVTYPE_CROSSBOW = 19, -- Ranged
INVTYPE_THROWN = 19, -- Ranged
INVTYPE_PROJECTILE = 20, -- Ammo
},


-- In addition to the bagshuiStockState item property, stock state table values are used
-- to determine the following stock badge attributes, so they must be PascalCase:
Expand Down Expand Up @@ -955,6 +1007,21 @@ function Bagshui:AddonLoaded()
end
self.currentCharacterInfo = self.currentCharacterData[BS_CONFIG_KEY.CHARACTER_INFO]

-- Inventory validation.
-- This will ensure there are no errors when changes are made to the expected inventory cache structure.
for character, characterData in pairs(self.characters) do
for _, inventoryType in pairs(BS_INVENTORY_TYPE) do
inventoryType = string.lower(inventoryType)
if characterData[inventoryType] and characterData[inventoryType].inventory then
for containerNum, containerContents in pairs(characterData[inventoryType].inventory) do
for slotNum, item in ipairs(containerContents) do
BsItemInfo:InitializeItem(item, false, true)
end
end
end
end
end

-- Log storage.
if _G.BagshuiData[BS_CONFIG_KEY.LOG] == nil then
_G.BagshuiData[BS_CONFIG_KEY.LOG] = {}
Expand Down
4 changes: 1 addition & 3 deletions Bagshui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Don't move things around unless you're sure it won't break anything.
<Include file="Config\ItemFixes.lua" />
<Include file="Config\PeriodicTable.lua" />
<Include file="Components\Hooks.lua" />
<Include file="Components\ItemInfo.lua" />
<Include file="Components\Slash.lua" />
<Include file="Components\Menus.lua" />
<Include file="Components\ObjectList.lua" />
Expand Down Expand Up @@ -68,9 +69,6 @@ Don't move things around unless you're sure it won't break anything.
<Include file="Config\Skins.lua" />
<Include file="Components\Skins.lua" />

<!-- Item info must load after UI because it depends on ScrollableTextWindow. -->
<Include file="Components\ItemInfo.lua" />

<!-- Rules -->
<Include file="Config\RuleFunctions.lua" />
<Include file="Config\RuleFunctionTemplates.lua" />
Expand Down
30 changes: 2 additions & 28 deletions Components/Inventory.Cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function Inventory:UpdateCache()

else
-- Slot is empty.
self:InitializeEmptySlotItem(item)
BsItemInfo:InitializeEmptySlotItem(item)
if self.containers[bagNum].isProfessionBag then
item.name = item.bagType .. " " .. item.name
end
Expand Down Expand Up @@ -598,32 +598,6 @@ end



--- Set the proper cache values for an empty slot item.
---@param item table self.inventory cache entry.
---@param isEmptySlotStack boolean? true for empty slot stack proxy entries.
function Inventory:InitializeEmptySlotItem(item, isEmptySlotStack)
item.id = 0
-- Using 0/1 for ease of sorting.
item.emptySlot = 1
-- Add "[Profession Bag Type]" to the name when needed.
item.name =
(isEmptySlotStack and item.bagType ~= BsGameInfo.itemSubclasses.Container.Bag)
and string.format(L.Suffix_EmptySlot, item.bagType)
or L.ItemPropFriendly_emptySlot
item.subtype = item.bagType
item.quality = -1
item.charges = -1
item.texture = nil
item.readable = nil
if not item._bagsRepresented then
item._bagsRepresented = {}
else
BsUtil.TableClear(item._bagsRepresented)
end
end



--- Prepare an entry in the emptySlotStacks table for to track the empty slot count for a given bag.
---@param bagInfo table self.containers entry.
function Inventory:InitializeEmptySlotStackTracking(bagInfo)
Expand All @@ -638,7 +612,7 @@ function Inventory:InitializeEmptySlotStackTracking(bagInfo)
-- Always re-initialize when called to ensure bag changes don't result in incorrect empty slot textures.
BsItemInfo:InitializeItem(self.emptySlotStacks[bagInfo.genericType])
self:AddItemBagInfo(self.emptySlotStacks[bagInfo.genericType], bagInfo, true)
self:InitializeEmptySlotItem(self.emptySlotStacks[bagInfo.genericType], true)
BsItemInfo:InitializeEmptySlotItem(self.emptySlotStacks[bagInfo.genericType], true)
end


Expand Down
Loading

0 comments on commit 837ef6a

Please sign in to comment.