Advertisement
Guest User

Conveyor.lua

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local from, to = ...
  2.  
  3. os.setComputerLabel("Conveyor")
  4.  
  5. local inv = {}
  6.  
  7. local function hasMethod(name,method)
  8.     for _,m in pairs(peripheral.getMethods(name)) do
  9.         if m == method then
  10.             return true
  11.         end
  12.     end
  13.     return false
  14. end
  15.  
  16. for _,name in pairs(peripheral.getNames()) do
  17.     if hasMethod(name, "pushItems") then
  18.         table.insert(inv, peripheral.wrap(name))
  19.     end
  20. end
  21.  
  22. local function suck(container)
  23.     local slot = 1
  24.     while true do
  25.         print(string.format("Suck slot %s from %s", slot, from))
  26.         if not pcall(container.pullItems, from, slot) then
  27.             return
  28.         end
  29.         slot = slot+1
  30.     end
  31. end
  32.  
  33. local function dump(container)
  34.     for slot, item in pairs(container.list()) do
  35.         print(string.format("Dump slot %s to %s", slot, to))
  36.         if not container.pushItems(to, slot) then
  37.             print(string.format("Fail to dump slot %s to %s", slot, to))
  38.             return
  39.         end
  40.     end
  41. end
  42.  
  43. while true do
  44.     for _, container in pairs(inv) do
  45.         dump(container)
  46.         sleep()
  47.         suck(container)
  48.         sleep()
  49.         dump(container)
  50.         sleep()
  51.     end
  52.     print("pause")
  53.     sleep(5)
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement