Fix setting target inventory

This commit is contained in:
2025-03-30 18:40:04 +02:00
parent 788a0bfe4d
commit 8516600a66

View File

@@ -7,8 +7,9 @@ local utils = require("Cyka.utils")
-- And not an item in an item in the inventory -- And not an item in an item in the inventory
-- So in theory we only need to recurse 1 deep -- So in theory we only need to recurse 1 deep
---@param inventory Barotrauma.Inventory ---@param inventory Barotrauma.Inventory
---@param slots Barotrauma.Inventory.SlotReference[] ---@param slots InventorySlot[]
---@return Barotrauma.Inventory.SlotReference[], string? ---@param depth number
---@return InventorySlot[], string?
local function getMouseoverSlots(inventory, slots, depth) local function getMouseoverSlots(inventory, slots, depth)
slots = slots or {} slots = slots or {}
depth = depth or 0 depth = depth or 0
@@ -51,7 +52,11 @@ local function getMouseoverSlots(inventory, slots, depth)
::mouseover:: ::mouseover::
if visualSlot:MouseOn() then if visualSlot:MouseOn() then
slots[#slots + 1] = slot slots[#slots + 1] = {
inventory = inventory,
slotIndex = i,
slot = slot
}
end end
::continue:: ::continue::
@@ -60,8 +65,13 @@ local function getMouseoverSlots(inventory, slots, depth)
return slots, nil return slots, nil
end end
---@return Barotrauma.Inventory.ItemSlot[]?, string? ---@class InventorySlot
local function getInventorySlotUnderCursor() ---@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 -- Make sure we have a controlled character
local controlledCharacter = Character.Controlled local controlledCharacter = Character.Controlled
if not controlledCharacter then return nil, "No controlled character" end 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 for i, visualSlot in ipairs(containerInventory.visualSlots) do
if visualSlot:MouseOn() then if visualSlot:MouseOn() then
local slot = containerInventory.slots[i] local slot = containerInventory.slots[i]
mouseoverSlots[#mouseoverSlots + 1] = slot mouseoverSlots[#mouseoverSlots + 1] = {
inventory = containerInventory,
slotIndex = i,
slot = slot
}
end end
end end
end end
@@ -90,7 +104,7 @@ end
local targetInventory = nil local targetInventory = nil
local function tryStackCursorItem() local function tryStackCursorItem()
local slots, err = getInventorySlotUnderCursor() local slots, err = getInventorySlotsUnderCursor()
if err then if err then
-- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err)) -- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return return
@@ -162,32 +176,44 @@ local function tryStackCursorItem()
end end
local function setTargetInventory() local function setTargetInventory()
local visualSlot, itemInv, slot = getInventorySlotUnderCursor() ---@type InventorySlot[]
if not visualSlot or not itemInv or not slot then local slots, err = getInventorySlotsUnderCursor()
MyModGlobal.debugPrint(string.format("No inventory slot or item found")) if err then
MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return return
end end
if not slot.items or #slot.items == 0 then if not slots or #slots == 0 then
MyModGlobal.debugPrint("Slot is empty") MyModGlobal.debugPrint("No inventory slots found")
print(string.format("Setting target inventory to %s", tostring(itemInv)))
targetInventory = itemInv
return return
end end
local item = slot.items[1] -- Yes we do this in a loop
if not item then -- The idea is if we get one slot we're golden, great!
MyModGlobal.debugPrint("Slot is empty") -- If we get multiple we'll use the first valid one
print(string.format("Setting target inventory to %s", tostring(itemInv))) -- Although everything is valid to us...
targetInventory = itemInv for _, slot in ipairs(slots) do
return 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 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 end
return { return {