Advertisement
osmarks

FactoryLine Autocrafter

Feb 24th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local chest = "front"
  2. local chest_to_turtle = "east"
  3. local turtle_to_chest = "west"
  4.  
  5. local function sum_inv(i)
  6.   local out = {}
  7.   for _, v in pairs(i) do
  8.     out[v.name] = (out[v.name] or 0) + v.count
  9.   end
  10.   return out
  11. end
  12.  
  13. local function clear_inv()
  14.   for i = 1, 16 do
  15.     peripheral.call(chest, "pullItems", turtle_to_chest, i)
  16.   end
  17. end
  18.  
  19. local recipe = {
  20.   {"minecraft:sand",   "minecraft:sand",   "minecraft:sand"},
  21.   {"minecraft:sand",   "minecraft:gravel", "minecraft:gravel"},
  22.   {"minecraft:gravel", "minecraft:gravel", "minecraft:dye"},
  23. }
  24. local reqs = {}
  25. for y, row in pairs(recipe) do
  26.   for x, item in pairs(row) do
  27.     reqs[item] = (reqs[item] or 0) + 1
  28.   end
  29. end
  30.  
  31. local function satisfied(reqs, by)
  32.   for req, qty in pairs(reqs) do
  33.     if qty > (by[req] or 0) then return false end
  34.   end
  35.   return true
  36. end
  37.  
  38. local function move(what, to)
  39.   for slot, stack in pairs(peripheral.call(chest, "list")) do
  40.     if stack.name == what then peripheral.call(chest, "pushItems", chest_to_turtle, slot, 1, to) return end
  41.   end
  42. end
  43.  
  44. while true do
  45.   local contents = peripheral.call(chest, "list")
  46.   local items = sum_inv(contents)
  47.   if satisfied(reqs, items) then
  48.     print "Requirements satisfied; crafting."
  49.     for y, row in pairs(recipe) do
  50.       for x, item in pairs(row) do
  51.         local tslot = ((y - 1) * 4) + x
  52.         move(item, tslot)
  53.       end
  54.     end
  55.     turtle.select(1)
  56.     turtle.craft()
  57.     turtle.dropDown()
  58.   else
  59.     print "Not crafting; requirements not satisfied."
  60.   end
  61.   sleep(1)
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement