Make quickstacking on a container stack all items from parent into that container

This commit is contained in:
2025-04-01 01:32:40 +02:00
parent 8e27229dd8
commit 1608c95091

View File

@@ -144,6 +144,9 @@ local function tryMoveItem(item, itemTree, force)
-- Then move to any of the empty slots -- Then move to any of the empty slots
if not moved then if not moved then
-- MyModGlobal.debugPrint("No existing stacks found, trying empty slots...") -- MyModGlobal.debugPrint("No existing stacks found, trying empty slots...")
if not itemTree['empty'] then
return "No empty slots found"
end
for _, itemLocation in ipairs(itemTree['empty']) do for _, itemLocation in ipairs(itemTree['empty']) do
local maxFits = itemLocation.maxFits local maxFits = itemLocation.maxFits
-- These empty slots are not guranteed to be empty, ironically -- These empty slots are not guranteed to be empty, ironically
@@ -227,6 +230,52 @@ local function tryBuildCharacterItemTree(character)
return itemTree, nil return itemTree, nil
end end
---@param item Barotrauma.Item
---@return string?
local function stackToContainer(item)
MyModGlobal.debugPrint(string.format("Attempting to stack items to container: %s", tostring(item)))
local itemInventory = item.OwnInventory
if not itemInventory then
return "Item has no own inventory"
end
local parentInventory = item.ParentInventory
if not parentInventory then
return "Item has no parent inventory"
end
local itemTree = buildItemTree(itemInventory)
itemTree = sortItemTree(itemTree)
local toMove = {}
local total = 0
utils.enqueueInventory(parentInventory, toMove, function(iterationItem)
total = total + 1
-- Get all items that aren't our mouseover item
if iterationItem.Equals(item) then
-- MyModGlobal.debugPrint(string.format("Skipping item: %s", iterationItem.Prefab.Identifier.Value))
return false
end
-- This won't work for nested containers
-- But making it work with nested containers is a pain in the ass
-- So we'll just not do that for now
if iterationItem.ParentInventory and iterationItem.ParentInventory.Equals(itemInventory) then
-- MyModGlobal.debugPrint(string.format("Skipping item: %s", iterationItem.Prefab.Identifier.Value))
return false
end
if total >= 10000 then
MyModGlobal.debugPrint("Too many items to stack, stopping")
return false, true
end
return true
end)
MyModGlobal.debugPrint(string.format("Enqueued %d items to stack", #toMove))
dump(toMove)
local errors = tryMoveItems(toMove, itemTree)
for _, error in ipairs(errors) do
MyModGlobal.debugPrint(string.format("Error stacking item: %s", error))
end
end
-- Function to quickly stack items from inventory to containers -- Function to quickly stack items from inventory to containers
-- 6 and 7 are hands -- 6 and 7 are hands
-- 9..18 are main slots -- 9..18 are main slots
@@ -234,6 +283,24 @@ local inventorySlotsToStack = { 6, 7, }
-- local inventorySlotsToStack = { 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 } -- local inventorySlotsToStack = { 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }
---@param character Barotrauma.Character ---@param character Barotrauma.Character
local function quickStackItems(character) local function quickStackItems(character)
MyModGlobal.debugPrint("Quick stack function called")
-- If we are mousing over an item that has an inventory (ie. is a container)
-- Then stack all items from the parent inventory into the mouseover container
local mouseover = utils.getFirstSlotUnderCursor()
if mouseover then
---@type Barotrauma.Item
local slotItem = mouseover.slot.items[1]
local itemInventory = slotItem.OwnInventory
if itemInventory then
MyModGlobal.debugPrint(string.format("Item inventory found: %s", tostring(itemInventory)))
local err = stackToContainer(slotItem)
if err then
MyModGlobal.debugPrint(string.format("Error stacking items to container: %s", err))
end
return
end
end
if not character then if not character then
MyModGlobal.debugPrint("No character found") MyModGlobal.debugPrint("No character found")
return return
@@ -245,16 +312,6 @@ local function quickStackItems(character)
return return
end end
-- for i, slot in ipairs(inventory.slots) do
-- if #slot.items > 0 then
-- local item = slot.items[1]
-- local identifier = item.Prefab.Identifier.Value
-- print(string.format("Item at slot %d is %s", i, identifier))
-- end
-- end
MyModGlobal.debugPrint("Quick stack function called")
local itemTree, err = tryBuildCharacterItemTree(character) local itemTree, err = tryBuildCharacterItemTree(character)
if err then if err then
MyModGlobal.debugPrint(string.format("Error building item tree: %s", err)) MyModGlobal.debugPrint(string.format("Error building item tree: %s", err))