Files
barotrauma-localmods/QuickStackToBag/Lua/Cyka/cursormacroer.lua
2025-03-30 15:20:36 +02:00

96 lines
2.9 KiB
Lua

local quickstack = require("Cyka.quickstack")
---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|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]
local item = nil
if #slot.items > 0 then
item = slot.items[1]
end
return visualSlot, charInventory, item
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]
local item = nil
if #slot.items > 0 then
item = slot.items[1]
end
return visualSlot, itemInv, item
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]
local slotItem = nil
if #slot.items > 0 then
slotItem = slot.items[1]
end
return visualSlot, itemInv, slotItem
end
end
end
end
return nil, nil, nil
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
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)))
local controlledCharacter = Character.Controlled
if not controlledCharacter then
MyModGlobal.debugPrint("No controlled character found")
return
end
local itemTree, err = quickstack.tryBuildCharacterItemTree(controlledCharacter)
if err then
MyModGlobal.debugPrint(string.format("Error building item tree: %s", err))
return
end
itemTree = quickstack.sortItemTree(itemTree)
err = quickstack.tryMoveItem(item, itemTree, true)
if err then
MyModGlobal.debugPrint(string.format("Error moving item: %s", err))
return
end
end
return {
tryStackCursorItem = tryStackCursorItem
}