diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index a6cac3b..e17e5f5 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -28,7 +28,7 @@ print(MOD_NAME .. " v" .. MOD_VERSION .. " loaded!") ---@param table table ---@param depth number? -function DumpTable(table, depth) +local function DumpTable(table, depth) if depth == nil then depth = 0 end @@ -527,23 +527,53 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab return end - local recipe, err = getSelectedRecipeRequirements(fabricator.fabricator) + local recipe + recipe, err = getSelectedRecipeRequirements(fabricator.fabricator) if err then print(string.format("Error getting selected recipe requirements: %s", err)) end DumpTable(recipe) - local toGet = recipe.requiredItems + -- TODO: Maybe make it so every press cycles the input + -- For recipes that have multiple prefabs + -- But then again what if it has 3 items with 4 prefabs each.. + -- Is that 4 iterations or 3*4 iterations? + local toFind = recipe.requiredItems + ---@type Barotrauma.Item[] + local toGet = {} ---@type fun(item: Barotrauma.Item): boolean local filter = function(item) - if item.Prefab.Identifier == recipe.targetItem.identifier then - return true + 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 + toGet[#toGet + 1] = item + itemInfo.amount = itemInfo.amount - 1 + found = true + break + end + end + if itemInfo.amount <= 0 then + toFind[i] = nil + end + if found then break end end - return false + return found end - local items = enqueueInventory(bagItem.OwnInventory, {}, filter) + -- TODO: This might explode... Oh well? + local inputInventory = fabricator.item.OwnInventories[1] + 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) + previous = item.Prefab.Identifier + end DumpTable(items) end, Hook.HookMethodType.After)