Advertisement
fatboychummy

SurvivalGeneratorController.lua

Mar 31st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1.  
  2. -- config
  3. local rsIn = "top"
  4. local rsOut = "front"
  5. local chestPrefix = "minecraft:chest"
  6. local genPrefix = "xu2:tilemachineprovider"
  7.  
  8. -- end of config
  9. local chest = false
  10.  
  11. local function get(tb)
  12.   local tab = {}
  13.   for k, v in pairs(peripheral.getNames()) do
  14.     if string.find(v, tb) then
  15.       tab[#tab + 1] = v
  16.     end
  17.   end
  18.   return tab
  19. end
  20.  
  21. local function moveToGenerator(genName)
  22.   local gen = peripheral.wrap(genName)
  23.   if gen then
  24.     local i = gen.list()
  25.     local flg = true
  26.     for k, v in pairs(i) do
  27.       flg = false
  28.       break
  29.     end
  30.     if flg then
  31.       print("Stocking peripheral " .. tostring(genName) .. ".")
  32.       local dat = chest.list()
  33.       for i = 1, chest.size() do
  34.         if dat[i] then
  35.           if chest.pushItems(genName,i,1) then
  36.             print("Peripheral stocked.")
  37.             return
  38.           end
  39.         end
  40.       end
  41.     else
  42.       print("Peripheral " .. tostring(genName) .. " is already stocked.")
  43.     end
  44.   else
  45.     printError("Warning: Peripheral " .. tostring(genName) .. " doesn't actually exist!")
  46.   end
  47. end
  48.  
  49. local function moveToGenerators()
  50.   local genNames = get(genPrefix)
  51.   chest = peripheral.find(chestPrefix)
  52.   if chest then
  53.     local itm = chest.list()
  54.     local flg = false
  55.     for i = 1,#itm do
  56.       if itm[i] then
  57.         flg = true break -- check if there are items in the chest.
  58.       end
  59.     end
  60.     if not flg then
  61.       print("The chest is empty.")
  62.       return
  63.     end -- if no items in the chest, exit.
  64.     -- if not, move items to generators.
  65.     for i = 1,#genNames do
  66.       moveToGenerator(genNames[i])
  67.     end
  68.   else
  69.     printError("Warning: Failed to find chest.")
  70.   end
  71. end
  72.  
  73. local function rsWatch()
  74.   while true do
  75.     if rs.getInput(rsIn) then
  76.       rs.setOutput(rsOut, true)
  77.       print("GENERATORS ON\n-------- rsWatch")
  78.     else
  79.       rs.setOutput(rsOut, false)
  80.       print("GENERATORS OFF\n-------- rsWatch")
  81.     end
  82.     os.sleep(1)
  83.   end
  84. end
  85.  
  86.  
  87. local function main()
  88.   local i = 1
  89.   while true do
  90.     moveToGenerators()
  91.     print("--------", i)
  92.     i = i + 1
  93.     os.sleep(10)
  94.   end
  95. end
  96.  
  97. local ok, err = pcall(parallel.waitForAny, main, rsWatch)
  98. if not ok then
  99.   printError(err)
  100.   print()
  101.   print("Restarting in 15 seconds")
  102.   os.sleep(15)
  103.   os.reboot()
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement