Advertisement
Guest User

storekeeper.lua

a guest
Feb 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. local component = require("component")
  2. local controller = component.proxy(component.me_controller.address)
  3. local gpu = component.gpu
  4.  
  5. local process = require("process")
  6. local shell = require("shell")
  7. local fs = require("filesystem")
  8. local text = require("text")
  9.  
  10. local execution_path = shell.resolve(process.info().path)
  11. execution_path = execution_path:sub(1, -16)
  12.  
  13. if fs.exists(execution_path .. "storekeeper.cfg") == false then
  14.   print("No configuration found!")
  15.   print("Please create a storekeeper.cfg file.")
  16.   os.exit()
  17. end
  18.  
  19. local cfg = io.open(execution_path .. "storekeeper.cfg", "r")
  20.  
  21. local toStock = {}
  22.  
  23. for line in cfg:lines() do
  24.   local splitString = {}
  25.   for match in (line.."|"):gmatch("(.-)".."|") do
  26.     table.insert(splitString, match)
  27.   end
  28.   table.insert(toStock, splitString)
  29. end
  30.  
  31. local toCraft = {}
  32.  
  33. for _,stockable in ipairs(toStock) do
  34.   craftables = controller.getCraftables({label = stockable[1]})
  35.  
  36.   local length = 0
  37.  
  38.   for _,item in pairs(craftables) do
  39.     length = length + 1
  40.   end
  41.  
  42.   if length == 1 then
  43.     error("Craftable not found: " .. stockable[1])
  44.   elseif length > 2 then
  45.     error("More than 1 crafting recipe associated with label: " .. stockable[1])
  46.   end
  47.  
  48.   local itemstack = craftables[1].getItemStack()
  49.   print("Craftable found: " .. itemstack.label .. " (" .. itemstack.name .. ")")
  50.  
  51.   table.insert(toCraft, {craftables[1], tonumber(stockable[2])})
  52. end
  53.  
  54. local activeCraftingOperations = {}
  55.  
  56. function clearOldOperations()
  57.   for index, operation in ipairs(activeCraftingOperations) do
  58.     if operation[2].isCanceled() then
  59.       print("Crafting of " .. operation[1].label .. " failed or cancelled")
  60.       table.remove(activeCraftingOperations, index)
  61.     elseif operation[2].isDone() then
  62.       print("Crafting of " .. operation[1].label .. " completed since last check")
  63.       table.remove(activeCraftingOperations, index)
  64.     end
  65.   end
  66. end
  67.  
  68. function checkActiveOperations(givenstack)
  69.   for _,operation in ipairs(activeCraftingOperations) do
  70.     if operation[1].label == givenstack.label then
  71.       return true
  72.     end
  73.   end
  74.   return false
  75. end
  76.  
  77. while true do
  78.   for _,craftable in ipairs(toCraft) do
  79.     local itemstack = craftable[1].getItemStack()
  80.     if not checkActiveOperations(itemstack) then
  81.       local networkContents = controller.getItemsInNetwork({label = itemstack.label})
  82.       if networkContents == nil or networkContents[1].size == nil then
  83.         print("0 " .. itemstack.label .. " found in ME (target " .. tostring(craftable[2]) ") - crafting "  .. tostring(craftable[2]))
  84.         local operation = craftable[1].request(craftable[2])
  85.         table.insert(activeCraftingOperations, {itemstack, operation})
  86.       elseif networkContents[1].size < craftable[2] then
  87.         print("Only " .. tostring(networkContents[1].size) .. " " .. itemstack.label .. " found in ME (target " .. tostring(craftable[2]) .. ") - crafting " .. tostring(craftable[2] - networkContents[1].size))
  88.         local operation = craftable[1].request(craftable[2] - networkContents[1].size)
  89.         table.insert(activeCraftingOperations, {itemstack, operation})
  90.       end
  91.     end
  92.   end
  93.   clearOldOperations()
  94.   os.sleep(10)
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement