Advertisement
SuperZorro

Refined Storage Autocraft

Sep 1st, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. -- Refined Storage Autocraft 1.1
  2. --
  3. -- Run the program with the pathname of the crafts' listing file
  4. -- Crafts file (a craft per line): [item_name] [count]
  5. -- File example: https://pastebin.com/zDrXzfSM
  6. --
  7. -- Created by SuperZorro
  8. -- Inspired and borrowed code from Nyhillius and thraaawn
  9.  
  10. -- 1.0.1: Updated to change component.block_refinedstorage_interface to component.refinedstorage_interface after updating opencomputers?
  11.  
  12. -- How long to wait between crafts.
  13. local waittime = 300
  14.  
  15. local event = require("event")
  16. local io = require("io")
  17. local sides = require("sides")
  18. local component = require("component")
  19. local rs = component.refinedstorage_interface
  20. local term = require("term")
  21.  
  22. local args = { ... }
  23. local paths = {}
  24.  
  25.  
  26. -- Check if the file exist
  27. local function file_exist(path)
  28.   local file = io.open(path)
  29.  
  30.   if (not file) then
  31.     print("[ERROR]: No such file: " .. path .. ".")
  32.     return false
  33.   end
  34.   io.close(file)
  35.   return true
  36. end
  37.  
  38. -- Load and parse the file, return a table with all the item to craft.
  39. local function load_file(path)
  40.   local crafts = {}
  41.  
  42.   for line in io.lines(path) do
  43.     local n, c, l, f = line:match "(%S+)%s+(%d+)"
  44.     l = n:match "(%u%S+)"
  45.     f = n:match("(%l+:%l+)")
  46.     if (l) then
  47.       table.insert(crafts, { name = f, label = l, count = c, fullName = n })
  48.     else
  49.       table.insert(crafts, { name = f, count = c, fullName = n })
  50.     end
  51.   end
  52.   return crafts
  53. end
  54.  
  55. -- Check the args
  56. if (#args > 0) then
  57.   paths[1] = os.getenv("PWD") .. "/" .. args[1]
  58.   if not file_exist(paths[1]) then
  59.     return
  60.   end
  61. else
  62.   print("[ERROR]: Filename is needed.")
  63.   return
  64. end
  65.  
  66.  
  67. local crafts = load_file(paths[1])
  68.  
  69.  
  70. while(true) do
  71.     for i,craft in ipairs(crafts) do
  72.         a = {}
  73.         k = "name"
  74.         a[k] = craft["fullName"]
  75.         if(rs.hasPattern(a)) then
  76.             local rsStack = rs.getItem(a)
  77. --            local rsStack = rs.getItem(craft["fullName"])
  78.  
  79.             local toCraft = craft["count"];
  80.             if(rsStack ~= nil) then
  81.                 toCraft = toCraft - rsStack.size
  82.             end
  83.  
  84.             if(toCraft ~= 0) then
  85.                 if(toCraft > 0) then
  86.                     term.clearLine()
  87.                     io.write("Crafting: " .. toCraft, craft["fullName"] .. "\n")
  88.                     rs.scheduleTask(a, toCraft)
  89.                 end
  90.             end
  91.         else
  92.             print("Missing pattern for: " .. craft.name)
  93.         end
  94.     end
  95.  
  96.  
  97. for waittime1 = waittime, 0, -1 do
  98.     term.clearLine()
  99.         io.write("Sleeping " .. waittime1 .. " secs.")
  100.         os.sleep(1)
  101.         local waittime1 = waittime1 - 1
  102. end
  103.  
  104.  
  105.  
  106. end
  107.  
  108.  
  109.  
  110. -- component.block_refinedstorage_interface.hasPattern()
  111. -- component.block_refinedstorage_interface.getItem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement