Replace calls to utils for factored out functions

This commit is contained in:
2025-03-30 23:14:40 +02:00
parent 7bee770482
commit a210fe27c6
4 changed files with 16 additions and 167 deletions

View File

@@ -2,105 +2,10 @@
local quickstack = require("Cyka.quickstack")
local utils = require("Cyka.utils")
-- There is actually no need to recurse deep
-- Because we can only have an item in the inventory open
-- 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 InventorySlot[]
---@param depth number
---@return InventorySlot[], string?
local function getMouseoverSlots(inventory, slots, depth)
slots = slots or {}
depth = depth or 0
if depth > 1 then return slots, nil end
local visualSlots = inventory.visualSlots
if not visualSlots then return nil, "Inventory has no visual slots" end
for i, visualSlot in ipairs(visualSlots) do
local item
local itemInventory
-- local err
local slot = inventory.slots[i]
if not slot then
-- MyModGlobal.debugPrint("Slot is not a valid slot")
goto continue
end
if #slot.items == 0 then
goto mouseover
end
item = slot.items[1]
if not item then
goto mouseover
end
itemInventory = item.OwnInventory
if not itemInventory then
goto mouseover
end
-- print("Before: " .. #slots)--
getMouseoverSlots(itemInventory, slots, depth + 1)
-- if err then
-- MyModGlobal.debugPrint(string.format("Error getting mouseover slots: %s", err))
-- end
-- print("After: " .. #slots)
::mouseover::
if visualSlot:MouseOn() then
slots[#slots + 1] = {
inventory = inventory,
slotIndex = i,
slot = slot
}
end
::continue::
end
return slots, nil
end
---@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
local inventory = controlledCharacter.Inventory
if not inventory then return nil, "No inventory" end
local mouseoverSlots, err = getMouseoverSlots(inventory)
if err then return mouseoverSlots, err end
local openContainers = quickstack.getOpenContainers()
for _, container in ipairs(openContainers) do
local containerInventories = container.OwnInventories
for containerInventory in containerInventories do
for i, visualSlot in ipairs(containerInventory.visualSlots) do
if visualSlot:MouseOn() then
local slot = containerInventory.slots[i]
mouseoverSlots[#mouseoverSlots + 1] = {
inventory = containerInventory,
slotIndex = i,
slot = slot
}
end
end
end
end
return mouseoverSlots, nil
end
local targetInventory = nil
local slotThrottle = {}
local function tryStackCursorItem()
local slots, err = getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
-- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return
@@ -181,8 +86,7 @@ local function tryStackCursorItem()
end
local function setTargetInventory()
---@type InventorySlot[]
local slots, err = getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return
@@ -224,5 +128,4 @@ end
return {
tryStackCursorItem = tryStackCursorItem,
setTargetInventory = setTargetInventory,
getInventorySlotsUnderCursor = getInventorySlotsUnderCursor
}