From 8cbdf3d5491d99fd838246bbf1cd481641fe6aee Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 30 Mar 2025 03:12:40 +0200 Subject: [PATCH] Fix some minor bugs --- QuickStackToBag/Lua/Autorun/init.lua | 41 ++++++++++++++++++---------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index 024ec62..33ae36c 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -195,12 +195,17 @@ enqueueItem = function(item, queue, predicate) -- Only machines have multiple -- So inventrorY should be fine here -- debugPrint("Item has its own inventory, enqueuing inventory...") - queue, _ = enqueueInventory(item.OwnInventory, queue) + queue, _ = enqueueInventory(item.OwnInventory, queue, predicate) -- if err then -- debugPrint(string.format("Error enqueuing inventory: %s", err)) -- end end - if predicate(item) then queue[#queue + 1] = item end + local ok, stop = predicate(item) + if ok then + -- debugPrint(string.format("Enqueuing item: %s", item.Prefab.Identifier.Value)) + queue[#queue + 1] = item + end + if stop then return queue, nil end -- debugPrint(string.format("Item enqueued. Current queue size: %d", #queue)) return queue, nil end @@ -385,8 +390,8 @@ local function quickStackItems(character) for _, container in ipairs(openContainers) do local inventories = container.OwnInventories debugPrint(string.format("Found %d inventories in the open container", #inventories)) - for i, containerInventory in ipairs(inventories) do - debugPrint(string.format("Enqueuing inventory %d with %d slots", i, #containerInventory.slots)) + for containerInventory in inventories do + debugPrint(string.format("Enqueuing inventory with %d slots", #containerInventory.slots)) local before = #toMove toMove = enqueueInventory(containerInventory, toMove) local after = #toMove @@ -464,7 +469,7 @@ local function getSelectedRecipeRequirements(fabricator) if not selectedRecipe then return nil, "No selected recipe found" end local requiredItems = {} - for _, requiredItem in pairs(selectedRecipe.RequiredItems) do + for requiredItem in selectedRecipe.RequiredItems do local itemInfo = { amount = tonumber(requiredItem.Amount), minCondition = tonumber(requiredItem.MinCondition), @@ -473,7 +478,7 @@ local function getSelectedRecipeRequirements(fabricator) } for prefab in requiredItem.ItemPrefabs do - itemInfo.prefabs[#itemInfo.prefabs + 1] = prefab.Identifier + itemInfo.prefabs[#itemInfo.prefabs + 1] = prefab.Identifier.Value end requiredItems[#requiredItems + 1] = itemInfo @@ -547,34 +552,42 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab ---@type fun(item: Barotrauma.Item): boolean local filter = function(item) local found = false - if #toFind == 0 then return false end -- toFind are all items we need to find for i, itemInfo in ipairs(toFind) do -- prefabs are all items that satisfy the requirements for _, prefab in ipairs(itemInfo.prefabs) do - if item.Prefab.Identifier == prefab then + -- debugPrint(string.format("Checking %s against %s", item.Prefab.Identifier.Value, prefab)) + if item.Prefab.Identifier.Value == prefab then + -- debugPrint(string.format("That'll do %s %s", item.Prefab.Identifier.Value, prefab)) toGet[#toGet + 1] = item itemInfo.amount = itemInfo.amount - 1 - found = true + found = false break end end - if itemInfo.amount <= 0 then - toFind[i] = nil - end + if itemInfo.amount <= 0 then toFind[i] = nil end if found then break end end - return found + -- debugPrint(string.format("Found %s %s", item.Prefab.Identifier.Value, tostring(found))) + return found, #toFind == 0 end + -- DumpTable(toGet) local items = enqueueInventory(bagItem.OwnInventory, {}, filter) -- TODO: This might explode... Oh well? local inputInventory = fabricator.item.OwnInventories[1] + for iinventory in fabricator.item.OwnInventories do + if #iinventory.slots > 1 then + inputInventory = iinventory + break + end + end + local slot = -1 local previous = nil for _, item in ipairs(items) do if previous ~= item.Prefab.Identifier then slot = slot + 1 end - inputInventory.TryPutItem(item, slot, false, true, nil) + -- inputInventory.TryPutItem(item, slot, false, true, nil) previous = item.Prefab.Identifier end DumpTable(items)