Advertisement
adriannic

Furnace.lua

Jun 19th, 2021
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. --[[
  2.     To use connect 4 chests and any number of furnaces to the computer.
  3.     To reset the peripherals list use the argument "new" when running the program.
  4. --]]
  5. local tArgs = {...}
  6. --Reset peripheral list
  7. if #tArgs == 1 then
  8.     if tArgs[1] == "new" then
  9.         fs.delete("./peripherals")
  10.     end
  11. end
  12.  
  13. local chests = {}
  14. local furnaces = {}
  15. --clears the screen
  16. local function clear()
  17.     term.clear()
  18.     term.setCursorPos(1,1)
  19. end
  20. --writes all the needed peripherals names to a file
  21. local function getPeripherals()
  22.     clear()
  23.     local file = fs.open("./peripherals","w")
  24.     local chests = {}
  25.     for k, v in pairs(peripheral.getNames()) do
  26.         if string.find(v,"chest") then
  27.             table.insert(chests,v)
  28.         end
  29.     end
  30.     if #chests < 4 then
  31.         error("Not enough chests connected.")
  32.     end
  33.     local lines = {
  34.         "input",
  35.         "fuel",
  36.         "output",
  37.         "buffer"
  38.     }
  39.     local chestList = {}
  40.     for i=1, 4 do
  41.         print("Choose the "..lines[i].." chest:")
  42.         for k, v in pairs(chests) do
  43.             print(k..": "..v)
  44.         end
  45.         local correct = false
  46.         while not correct do
  47.             local input = tonumber(read())
  48.             if chests[input] then
  49.                 file.writeLine(chests[input])
  50.                 table.insert(chestList,chests[input])
  51.                 table.remove(chests,input)
  52.                 correct = true
  53.             else
  54.                 print("Input the index of the chest you choose:")
  55.             end
  56.         end
  57.         clear()
  58.     end
  59.     local furnaces = {}
  60.     for k, v in pairs(peripheral.getNames()) do
  61.         if string.find(v,"furnace") then
  62.             table.insert(furnaces, v)
  63.             file.writeLine(v)
  64.         end
  65.     end
  66.     file.close()
  67.     return chestList, furnaces
  68. end
  69.  
  70. --Code
  71. --If the file does not exist in creates a new one.
  72. --If it does then it reads from it.
  73. if not fs.exists("./peripherals") then
  74.     chests, furnaces = getPeripherals()
  75. else
  76.     local file = fs.open("./peripherals", "r")
  77.     for i=1, 4 do
  78.         table.insert(chests,file.readLine())
  79.     end
  80.     local done = false
  81.     while not done do
  82.         local line = file.readLine()
  83.         if line then
  84.             table.insert(furnaces, line)
  85.         else
  86.             done = true
  87.         end
  88.     end
  89.     file.close()
  90. end
  91. --program loop
  92. local finish = false
  93. while not finish do
  94.     --input
  95.     local buffer = peripheral.wrap(chests[4])
  96.     local items = 0
  97.     for i = 1, 54 do
  98.         items = buffer.pullItems(chests[1],i)
  99.         if items ~= 0 then break end
  100.     end
  101.     if buffer.list()[1] then
  102.         items = buffer.list()[1].count
  103.     else items = 0 end
  104.     local n = math.floor(items/#furnaces)
  105.     if n <= 0 then n = 1 end
  106.     for k, v in pairs(furnaces) do
  107.         buffer.pushItems(v,1,n)
  108.         if items == 0 then break end
  109.     end
  110.     --fuel
  111.     local items = 0
  112.     for i = 1, 54 do
  113.         items = buffer.pullItems(chests[2],i,64,2)
  114.         if items ~= 0 then break end
  115.     end
  116.     if buffer.list()[2] then
  117.         items = buffer.list()[2].count
  118.     else items = 0 end
  119.     local n = math.floor(items/#furnaces)
  120.     if n <= 0 then n = 1 end
  121.     for k, v in pairs(furnaces) do
  122.         buffer.pushItems(v,2,n,2)
  123.         if items == 0 then break end
  124.     end
  125.     sleep(.05)
  126.     --output
  127.     local output = peripheral.wrap(chests[3])
  128.     for k, v in pairs(furnaces) do
  129.         output.pullItems(v,3)
  130.     end
  131.     --exit loop
  132.     if rs.getInput("top") then
  133.         finish = true
  134.     end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement