From 8516600a6626b65f5d49002f1406742660e72e90 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 30 Mar 2025 18:40:04 +0200 Subject: [PATCH] Fix setting target inventory --- QuickStackToBag/Lua/Cyka/cursormacroer.lua | 82 ++++++++++++++-------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/QuickStackToBag/Lua/Cyka/cursormacroer.lua b/QuickStackToBag/Lua/Cyka/cursormacroer.lua index 27be6ad..1456516 100644 --- a/QuickStackToBag/Lua/Cyka/cursormacroer.lua +++ b/QuickStackToBag/Lua/Cyka/cursormacroer.lua @@ -7,8 +7,9 @@ local utils = require("Cyka.utils") -- And not an item in an item in the inventory -- So in theory we only need to recurse 1 deep ---@param inventory Barotrauma.Inventory ----@param slots Barotrauma.Inventory.SlotReference[] ----@return Barotrauma.Inventory.SlotReference[], string? +---@param slots InventorySlot[] +---@param depth number +---@return InventorySlot[], string? local function getMouseoverSlots(inventory, slots, depth) slots = slots or {} depth = depth or 0 @@ -51,7 +52,11 @@ local function getMouseoverSlots(inventory, slots, depth) ::mouseover:: if visualSlot:MouseOn() then - slots[#slots + 1] = slot + slots[#slots + 1] = { + inventory = inventory, + slotIndex = i, + slot = slot + } end ::continue:: @@ -60,8 +65,13 @@ local function getMouseoverSlots(inventory, slots, depth) return slots, nil end ----@return Barotrauma.Inventory.ItemSlot[]?, string? -local function getInventorySlotUnderCursor() +---@class InventorySlot +---@field inventory Barotrauma.Inventory +---@field slotIndex number +---@field slot Barotrauma.Inventory.ItemSlot + +---@return InventorySlot[], string? +local function getInventorySlotsUnderCursor() -- Make sure we have a controlled character local controlledCharacter = Character.Controlled if not controlledCharacter then return nil, "No controlled character" end @@ -79,7 +89,11 @@ local function getInventorySlotUnderCursor() for i, visualSlot in ipairs(containerInventory.visualSlots) do if visualSlot:MouseOn() then local slot = containerInventory.slots[i] - mouseoverSlots[#mouseoverSlots + 1] = slot + mouseoverSlots[#mouseoverSlots + 1] = { + inventory = containerInventory, + slotIndex = i, + slot = slot + } end end end @@ -90,7 +104,7 @@ end local targetInventory = nil local function tryStackCursorItem() - local slots, err = getInventorySlotUnderCursor() + local slots, err = getInventorySlotsUnderCursor() if err then -- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err)) return @@ -162,32 +176,44 @@ local function tryStackCursorItem() end local function setTargetInventory() - 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")) + ---@type InventorySlot[] + local slots, err = getInventorySlotsUnderCursor() + if err then + MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err)) return end - if not slot.items or #slot.items == 0 then - MyModGlobal.debugPrint("Slot is empty") - print(string.format("Setting target inventory to %s", tostring(itemInv))) - targetInventory = itemInv + if not slots or #slots == 0 then + MyModGlobal.debugPrint("No inventory slots found") return end - local item = slot.items[1] - if not item then - MyModGlobal.debugPrint("Slot is empty") - print(string.format("Setting target inventory to %s", tostring(itemInv))) - targetInventory = itemInv - return + -- Yes we do this in a loop + -- The idea is if we get one slot we're golden, great! + -- If we get multiple we'll use the first valid one + -- Although everything is valid to us... + for _, slot in ipairs(slots) do + local item + local items = slot.slot.items + if not items or #items == 0 then + print(string.format("Slot is empty, setting target inventory to %s", tostring(slot.inventory))) + targetInventory = slot.inventory + goto continue + end + item = items[1] + if not item then + print(string.format("Item in slot is nil, setting target inventory to %s", tostring(slot.inventory))) + targetInventory = slot.inventory + goto continue + end + if not item.OwnInventory then + print(string.format("Item has no own inventory, setting target inventory to %s", tostring(slot.inventory))) + targetInventory = slot.inventory + goto continue + end + print(string.format("Item has own inventory, setting target inventory to %s", tostring(item.OwnInventory))) + targetInventory = item.OwnInventory + break + ::continue:: end - if not item.OwnInventory then - MyModGlobal.debugPrint("Item has no own inventory") - print(string.format("Setting target inventory to %s", tostring(itemInv))) - targetInventory = itemInv - return - end - print(string.format("Setting target inventory to %s", tostring(item.OwnInventory))) - targetInventory = item.OwnInventory end return {