From 24848cfe5f946b5bd41cbf7966fced07bf04cc27 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 30 Mar 2025 15:55:38 +0200 Subject: [PATCH] Fix up some more minor issues with stacker --- QuickStackToBag/Lua/Cyka/cursormacroer.lua | 44 +++++++++----------- QuickStackToBag/Lua/Cyka/quickstack.lua | 48 +++++++++++++++------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/QuickStackToBag/Lua/Cyka/cursormacroer.lua b/QuickStackToBag/Lua/Cyka/cursormacroer.lua index 04f7204..c1d5819 100644 --- a/QuickStackToBag/Lua/Cyka/cursormacroer.lua +++ b/QuickStackToBag/Lua/Cyka/cursormacroer.lua @@ -1,6 +1,7 @@ local quickstack = require("Cyka.quickstack") +local utils = require("Cyka.utils") ----@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|nil +---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.InventorySlot|nil local function getInventorySlotUnderCursor() -- Make sure we have a controlled character local controlledCharacter = Character.Controlled @@ -13,11 +14,7 @@ local function getInventorySlotUnderCursor() -- Check if mouse is over this slot if visualSlot:MouseOn() then local slot = charInventory.slots[i] - local item = nil - if #slot.items > 0 then - item = slot.items[1] - end - return visualSlot, charInventory, item + return visualSlot, charInventory, slot end end end @@ -29,11 +26,7 @@ local function getInventorySlotUnderCursor() for i, visualSlot in ipairs(itemInv.visualSlots) do if visualSlot:MouseOn() then local slot = itemInv.slots[i] - local item = nil - if #slot.items > 0 then - item = slot.items[1] - end - return visualSlot, itemInv, item + return visualSlot, itemInv, slot end end end @@ -45,11 +38,7 @@ local function getInventorySlotUnderCursor() for i, visualSlot in ipairs(itemInv.visualSlots) do if visualSlot:MouseOn() then local slot = itemInv.slots[i] - local slotItem = nil - if #slot.items > 0 then - slotItem = slot.items[1] - end - return visualSlot, itemInv, slotItem + return visualSlot, itemInv, slot end end end @@ -61,14 +50,14 @@ end local function tryStackCursorItem() MyModGlobal.debugPrint("Shift + Left Click detected") - local visualSlot, itemInv, item = getInventorySlotUnderCursor() - if not visualSlot or not itemInv or not item then + local visualSlot, itemInv, slot = getInventorySlotUnderCursor() + if not visualSlot or not itemInv or not slot then MyModGlobal.debugPrint(string.format("No inventory slot or item found")) return end - MyModGlobal.debugPrint(string.format("Visual slot: %s", tostring(visualSlot))) - MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv))) - MyModGlobal.debugPrint(string.format("Item: %s", tostring(item))) + -- MyModGlobal.debugPrint(string.format("Visual slot: %s", tostring(visualSlot))) + -- MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv))) + -- MyModGlobal.debugPrint(string.format("Inventory slot: %s", tostring(slot))) local controlledCharacter = Character.Controlled if not controlledCharacter then @@ -83,10 +72,15 @@ local function tryStackCursorItem() end itemTree = quickstack.sortItemTree(itemTree) - err = quickstack.tryMoveItem(item, itemTree, true) - if err then - MyModGlobal.debugPrint(string.format("Error moving item: %s", err)) - return + local itemsToMove = utils.enqueueSlot(slot) + for _, item in ipairs(itemsToMove) do + MyModGlobal.debugPrint(string.format("Enqueued item: %s", tostring(item))) + end + -- MyModGlobal.debugPrint(string.format("Enqueued %d items from the inventory slot", #itemsToMove)) + + local errors = quickstack.tryMoveItems(itemsToMove, itemTree) + for _, error in ipairs(errors) do + MyModGlobal.debugPrint(string.format("Error moving item: %s", error)) end end diff --git a/QuickStackToBag/Lua/Cyka/quickstack.lua b/QuickStackToBag/Lua/Cyka/quickstack.lua index c8ad713..020d986 100644 --- a/QuickStackToBag/Lua/Cyka/quickstack.lua +++ b/QuickStackToBag/Lua/Cyka/quickstack.lua @@ -112,19 +112,19 @@ local function tryMoveItem(item, itemTree, force) -- MyModGlobal.debugPrint(string.format("Attempting to move item: %s", item.Prefab.Identifier.Value)) -- MyModGlobal.DumpTable(location) - if item.Condition <= item.MaxCondition then - -- MyModGlobal.debugPrint(string.format("Item %s has condition %.2f(<=%.2f), not stacking", item.Prefab.Identifier.Value, item.Condition, item.MaxCondition)) - -- Don't even TRY to stack them - location = nil - end - local moved = false if location then -- First try to move to existing stacks for _, itemLocation in ipairs(location) do - if itemLocation.maxFits > 0 then + -- We cannot stack items with decreased condition + local canBePut = itemLocation.inventory.CanBePutInSlot(item.Prefab, itemLocation.slotIndex, item.Condition) + -- MyModGlobal.debugPrint(string.format("Can be put in slot %d: %s", itemLocation.slotIndex, tostring(canBePut))) + + if itemLocation.maxFits > 0 and canBePut then moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, false, true, nil) - itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) + if moved then + itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) + end -- if moved then -- MyModGlobal.debugPrint(string.format("Moved item to existing stack at slot index: %d", itemLocation .slotIndex)) -- else @@ -139,13 +139,26 @@ local function tryMoveItem(item, itemTree, force) if not moved then -- MyModGlobal.debugPrint("No existing stacks found, trying empty slots...") for _, itemLocation in ipairs(itemTree['empty']) do - moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, true, true, nil) + local maxFits = itemLocation.maxFits + -- These empty slots are not guranteed to be empty, ironically + -- After we insert an item into one it's no longer empty + -- But it still is in the empty table + -- So we want to make sure we can insert our item + -- Into the maybe empty slots itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) - -- if moved then - -- MyModGlobal.debugPrint(string.format("Moved item to empty slot at index: %d", itemLocation.slotIndex)) - -- else - -- MyModGlobal.debugPrint(string.format("Failed to move item to empty slot at index: %d", itemLocation.slotIndex)) - -- end + + if maxFits > 0 then + -- MyModGlobal.debugPrint(string.format("Trying to move item to empty slot at index: %d", itemLocation.slotIndex)) + moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, true, false, nil) + if moved then + itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) + end + -- if moved then + -- MyModGlobal.debugPrint(string.format("Moved item to empty slot at index: %d", itemLocation.slotIndex)) + -- else + -- MyModGlobal.debugPrint(string.format("Failed to move item to empty slot at index: %d", itemLocation.slotIndex)) + -- end + end end end @@ -250,7 +263,12 @@ local function quickStackItems(character) return end - -- local inventory = character.Inventory + local inventory = character.Inventory + if not inventory or not inventory.slots then + MyModGlobal.debugPrint("Character has no inventory") + return + end + -- for i, slot in ipairs(inventory.slots) do -- if #slot.items > 0 then -- local item = slot.items[1]