Skip to content

Commit

Permalink
Fixed a bug where item counts may be wrong when swapping items around…
Browse files Browse the repository at this point in the history
… with partial stacks.
  • Loading branch information
Cidan committed May 4, 2024
1 parent 92f590b commit 7bfd326
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
13 changes: 13 additions & 0 deletions data/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ function items:ItemChanged(newData, oldData)
return false
end

---@param newData ItemData
---@param oldData ItemData
---@return boolean
function items:ItemGUIDChanged(newData, oldData)
if newData.isItemEmpty then return false end
if not oldData then return false end
return newData.itemInfo.itemGUID ~= oldData.itemInfo.itemGUID
end

---@param newData ItemData
---@param oldData ItemData
---@return boolean
Expand Down Expand Up @@ -451,6 +460,10 @@ function items:LoadItems(kind, wipe, dataCache)
debug:Log("ItemHashChanged", currentItem.itemInfo.itemLink)
slotInfo.removedItems[previousItem.slotkey] = previousItem
slotInfo.addedItems[currentItem.slotkey] = currentItem
elseif items:ItemGUIDChanged(currentItem, previousItem) then
debug:Log("ItemGUIDChanged", currentItem.itemInfo.itemLink)
slotInfo.removedItems[previousItem.slotkey] = previousItem
slotInfo.addedItems[currentItem.slotkey] = currentItem
elseif items:ItemChanged(currentItem, previousItem) then
debug:Log("ItemChanged", currentItem.itemInfo.itemLink)
slotInfo.updatedItems[currentItem.slotkey] = currentItem
Expand Down
10 changes: 9 additions & 1 deletion views/gridview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ end
---@param view View
---@param slotkey string
local function UpdateButton(view, slotkey)
debug:Log("UpdateButton", "Updating button for item", slotkey)
view:RemoveDeferredItem(slotkey)
local itemButton = view:GetOrCreateItemButton(slotkey)
itemButton:SetItem(slotkey)
Expand Down Expand Up @@ -164,7 +165,14 @@ local function GridView(view, bag, slotInfo)

for _, item in pairs(changed) do
if item.bagid ~= Enum.BagIndex.Keyring then
UpdateButton(view, view:ChangeButton(item))
local updateKey, removeKey = view:ChangeButton(item)
UpdateButton(view, updateKey)
if updateKey ~= item.slotkey then
UpdateButton(view, item.slotkey)
end
if removeKey then
ClearButton(view, items:GetItemDataFromSlotKey(removeKey))
end
end
end

Expand Down
11 changes: 10 additions & 1 deletion views/oneview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ end
---@param view View
---@param item ItemData
local function ClearButton(view, item)
debug:Log("ClearButton", "Clearing button for item", item.slotkey)
local cell = view.itemsByBagAndSlot[item.slotkey]
local bagid, slotid = view:ParseSlotKey(item.slotkey)
cell:SetFreeSlots(bagid, slotid, -1, "Recently Deleted")
Expand All @@ -71,6 +72,7 @@ end
---@param view View
---@param slotkey string
local function UpdateButton(view, slotkey)
debug:Log("UpdateButton", "Updating button for item", slotkey)
view:RemoveDeferredItem(slotkey)
local itemButton = view:GetOrCreateItemButton(slotkey)
itemButton:SetItem(slotkey)
Expand Down Expand Up @@ -130,7 +132,14 @@ local function OneBagView(view, bag, slotInfo)

for _, item in pairs(changed) do
if item.bagid ~= Enum.BagIndex.Keyring then
UpdateButton(view, view:ChangeButton(item))
local updateKey, removeKey = view:ChangeButton(item)
UpdateButton(view, updateKey)
if updateKey ~= item.slotkey then
UpdateButton(view, item.slotkey)
end
if removeKey then
ClearButton(view, items:GetItemDataFromSlotKey(removeKey))
end
end
end

Expand Down
13 changes: 10 additions & 3 deletions views/views.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ end
---@return string?
function views.viewProto:RemoveButton(item)
local stack = self.stacks[item.itemHash]
if not stack then
if not stack or not stack:IsInStack(item.slotkey) then
return nil
end
local updateKey = stack:RemoveItem(item.slotkey)
Expand Down Expand Up @@ -296,8 +296,10 @@ end
-- ChangeButton updates the item in the stack if it exists.
-- Returns the slotkey of the item base item if the item was updated in a stack, or the slotkey
-- of the item if it was not in a stack.
-- The second return is the slotkey of the item that should be removed from the view,
-- if the item was merged into a stack.
---@param item ItemData
---@return string
---@return string, string?
function views.viewProto:ChangeButton(item)
local opts = database:GetStackingOptions(self.kind)
-- If we're not merging stacks, return nil.
Expand All @@ -307,6 +309,11 @@ function views.viewProto:ChangeButton(item)

local stack = self.stacks[item.itemHash]
if stack then
if not stack:IsInStack(item.slotkey) then
stack:AddItem(item.slotkey)
stack:UpdateCount()
return stack.item, item.slotkey
end
stack:UpdateCount()
return stack.item
end
Expand Down Expand Up @@ -377,7 +384,7 @@ function stackProto:RemoveItem(slotkey)
return nil
end

assert(self.subItems[slotkey], "Slotkey not found in stack")
assert(self.subItems[slotkey], "Slotkey not found in stack" .. slotkey)

self.subItems[slotkey] = nil
self:UpdateCount()
Expand Down

0 comments on commit 7bfd326

Please sign in to comment.