local quickstack = require("Cyka.quickstack") local utils = require("Cyka.utils") ---@return Barotrauma.VisualSlot|nil, Barotrauma.Inventory|nil, Barotrauma.Inventory.ItemSlot|nil local function getInventorySlotUnderCursor() -- Make sure we have a controlled character local controlledCharacter = Character.Controlled if not controlledCharacter then return nil, nil, nil end -- Check player inventory first local charInventory = controlledCharacter.Inventory if charInventory and charInventory.visualSlots then for i, visualSlot in ipairs(charInventory.visualSlots) do -- Check if mouse is over this slot if visualSlot:MouseOn() then local slot = charInventory.slots[i] return visualSlot, charInventory, slot end end end -- Check if selected item has inventory (containers, etc.) local selectedItem = controlledCharacter.SelectedItem if selectedItem and selectedItem.OwnInventory and selectedItem.OwnInventory.visualSlots then local itemInv = selectedItem.OwnInventory for i, visualSlot in ipairs(itemInv.visualSlots) do if visualSlot:MouseOn() then local slot = itemInv.slots[i] return visualSlot, itemInv, slot end end end -- Check open containers or other items with visible inventories for item in Item.ItemList do if item and item.OwnInventory and item.OwnInventory.visualSlots then local itemInv = item.OwnInventory for i, visualSlot in ipairs(itemInv.visualSlots) do if visualSlot:MouseOn() then local slot = itemInv.slots[i] return visualSlot, itemInv, slot end end end end return nil, nil, nil end local targetInventory = nil local function tryStackCursorItem() 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("Inventory slot: %s", tostring(slot))) if not slot or not slot.items or not slot.items[1] then MyModGlobal.debugPrint("No items in slot") return end local inventory = targetInventory MyModGlobal.debugPrint(string.format("Target inventory: %s", tostring(inventory))) if not inventory then local controlledCharacter = Character.Controlled if not controlledCharacter then MyModGlobal.debugPrint("No controlled character found") return end local cinventory = controlledCharacter.Inventory if not cinventory or not cinventory.slots then MyModGlobal.debugPrint("No inventory found") return end local bagSlot = cinventory.slots[MyModGlobal.BAG_SLOT] if not bagSlot or not bagSlot.items or not bagSlot.items[1] then MyModGlobal.debugPrint("No bag slot found") return end local bagItem = bagSlot.items[1] if not bagItem or not bagItem.OwnInventory then MyModGlobal.debugPrint("Bag item has no own inventory") return end local bagInventory = bagItem.OwnInventory if not bagInventory or not bagInventory.slots then MyModGlobal.debugPrint("Bag inventory has no slots") return end inventory = bagInventory end if not inventory then MyModGlobal.debugPrint("No inventory found") return end local itemTree, err = quickstack.buildItemTree(inventory) if err then MyModGlobal.debugPrint(string.format("Error building item tree: %s", err)) return end itemTree = quickstack.sortItemTree(itemTree) 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)) -- MyModGlobal.DumpTable(itemTree) local errors = quickstack.tryMoveItems(itemsToMove, itemTree, true) for _, error in ipairs(errors) do MyModGlobal.debugPrint(string.format("Error moving item: %s", error)) end 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")) return end if not slot.items or #slot.items == 0 then MyModGlobal.debugPrint("Slot is empty") return end local item = slot.items[1] if not item then MyModGlobal.debugPrint("Slot is empty") return end if not item.OwnInventory then MyModGlobal.debugPrint("Item has no own inventory") return end print(string.format("Setting target inventory to %s", tostring(item.OwnInventory))) targetInventory = item.OwnInventory end return { tryStackCursorItem = tryStackCursorItem, setTargetInventory = setTargetInventory }