local component = require("component") local controller = component.proxy(component.me_controller.address) local gpu = component.gpu local process = require("process") local shell = require("shell") local fs = require("filesystem") local text = require("text") local execution_path = shell.resolve(process.info().path) execution_path = execution_path:sub(1, -16) if fs.exists(execution_path .. "storekeeper.cfg") == false then print("No configuration found!") print("Please create a storekeeper.cfg file.") os.exit() end local cfg = io.open(execution_path .. "storekeeper.cfg", "r") local toStock = {} for line in cfg:lines() do local splitString = {} for match in (line.."|"):gmatch("(.-)".."|") do table.insert(splitString, match) end table.insert(toStock, splitString) end local toCraft = {} for _,stockable in ipairs(toStock) do craftables = controller.getCraftables({label = stockable[1]}) local length = 0 for _,item in pairs(craftables) do length = length + 1 end if length == 1 then error("Craftable not found: " .. stockable[1]) elseif length > 2 then error("More than 1 crafting recipe associated with label: " .. stockable[1]) end local itemstack = craftables[1].getItemStack() print("Craftable found: " .. itemstack.label .. " (" .. itemstack.name .. ")") table.insert(toCraft, {craftables[1], tonumber(stockable[2])}) end local activeCraftingOperations = {} function clearOldOperations() for index, operation in ipairs(activeCraftingOperations) do if operation[2].isCanceled() then print("Crafting of " .. operation[1].label .. " failed or cancelled") table.remove(activeCraftingOperations, index) elseif operation[2].isDone() then print("Crafting of " .. operation[1].label .. " completed since last check") table.remove(activeCraftingOperations, index) end end end function checkActiveOperations(givenstack) for _,operation in ipairs(activeCraftingOperations) do if operation[1].label == givenstack.label then return true end end return false end while true do for _,craftable in ipairs(toCraft) do local itemstack = craftable[1].getItemStack() if not checkActiveOperations(itemstack) then local networkContents = controller.getItemsInNetwork({label = itemstack.label}) if networkContents == nil or networkContents[1].size == nil then print("0 " .. itemstack.label .. " found in ME (target " .. tostring(craftable[2]) ") - crafting " .. tostring(craftable[2])) local operation = craftable[1].request(craftable[2]) table.insert(activeCraftingOperations, {itemstack, operation}) elseif networkContents[1].size < craftable[2] then print("Only " .. tostring(networkContents[1].size) .. " " .. itemstack.label .. " found in ME (target " .. tostring(craftable[2]) .. ") - crafting " .. tostring(craftable[2] - networkContents[1].size)) local operation = craftable[1].request(craftable[2] - networkContents[1].size) table.insert(activeCraftingOperations, {itemstack, operation}) end end end clearOldOperations() os.sleep(10) end