Advertisement
Ignius12

Smelter

Aug 11th, 2022 (edited)
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. local chests = {peripheral.find("minecraft:chest")}
  2. local mon = peripheral.find("monitor")
  3. local furnaces = {peripheral.find("minecraft:furnace")}
  4. local fuel = {"minecraft:coal", "minecraft:coal_block", "minecraft:oak_log", "minecraft:jungle_log", "minecraft:charcoal"}
  5. local canSmelt = false
  6. local smelting = false
  7. local scroll = 0
  8. local windowHeight = 8
  9.  
  10. function draw_text(x, y, text, text_color, bg_color)
  11.   mon.setBackgroundColor(bg_color)
  12.   mon.setTextColor(text_color)
  13.   mon.setCursorPos(x,y)
  14.   mon.write(text)
  15. end
  16.  
  17.  
  18. function draw_line(x, y, length, color)
  19.     if length < 0 then
  20.       length = 0
  21.     end
  22.     mon.setBackgroundColor(color)
  23.     mon.setCursorPos(x,y)
  24.     mon.write(string.rep(" ", length))
  25. end
  26.  
  27. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  28.   draw_line(x, y, length, bg_color) --backgoround bar
  29.   local barSize = math.floor((minVal/maxVal) * length)
  30.   draw_line(x, y, barSize, bar_color) --progress so far
  31. end
  32.  
  33.  
  34. function clear()
  35.     --term.clear()
  36.     --term.setCursorPos(1,1)
  37.     mon.setBackgroundColor(colors.black)
  38.     mon.clear()
  39.     mon.setCursorPos(1,1)
  40. end
  41.  
  42.  
  43. function getInventories()
  44.     for idx, chest in pairs(chests) do
  45.         items = chest.list()
  46.         for slot = scroll, scroll + windowHeight do
  47.             if(items[slot] ~= nil) then
  48.                 draw_text(4, slot - scroll, ("%d x %s"):format(items[slot].count, items[slot].name), colors.cyan, colors.black)
  49.             end
  50.         end
  51.     end
  52. end
  53.  
  54. function button()
  55.     while (true) do
  56.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  57.         if(yPos == 19 and xPos >=2 and xPos <= 6 and canSmelt and not smelting) then
  58.                smelting = true
  59.         end
  60.        
  61.         if(xPos >= 1 and xPos <= 2) then
  62.             if(yPos == 1) then
  63.                 scroll = scroll - 1
  64.                 if(scroll < 0) then
  65.                     scroll = 0
  66.                 end
  67.             else if(yPos == 7) then
  68.                     scroll = scroll + 1
  69.                  end
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. function moveFuels(init, tar, qty)
  76.     for slot, item in pairs(init.list()) do
  77.             for i = 1, table.getn(fuel) do
  78.                 if(item.name == fuel[i]) then
  79.                     init.pushItems(peripheral.getName(tar), slot, qty)                
  80.                 end
  81.             end
  82.         end
  83. end
  84.  
  85. function moveFuels(init, tar, qty, idx)
  86.     for slot, item in pairs(init.list()) do
  87.             for i = 1, table.getn(fuel) do
  88.                 if(item.name == fuel[i]) then
  89.                     init.pushItems(peripheral.getName(tar), slot, qty, idx)                
  90.                 end
  91.             end
  92.         end
  93. end
  94.  
  95. function moveItems(init, tar, qty, tarItem, idx)
  96.     for slot, item in pairs(init.list()) do
  97.         if(item.name == tarItem) then
  98.             init.pushItems(peripheral.getName(tar), slot, qty, idx)
  99.             return true
  100.         end
  101.     end
  102. end
  103.  
  104. function emptySlot(init, tar, idx)
  105.     for slot, item in pairs(init.list()) do
  106.         if(slot == idx) then
  107.             init.pushItems(peripheral.getName(tar), slot)
  108.         end
  109.     end
  110. end
  111.  
  112.  
  113. function refuel()
  114.     fuelChest = chests[1]
  115.     while(true) do
  116.         if(smelting) then
  117.             for idx, chest in pairs(chests) do
  118.                 moveFuels(chest, fuelChest, 64)
  119.             end
  120.             for idx, furnace in pairs(furnaces) do
  121.                 moveFuels(fuelChest, furnace, 1, 2)
  122.             end
  123.         end
  124.         sleep(0.1)
  125.     end
  126. end
  127.  
  128. function loadTar(tar)
  129.     foundItem = true
  130.     while(foundItem) do
  131.         foundItem = false
  132.          for _, chest in pairs(chests) do
  133.             for _, furnace in pairs(furnaces) do
  134.                 foundItem = moveItems(chest, furnace, 1, tar, 1)
  135.                 sleep(0.1)
  136.             end
  137.         end
  138.     end
  139.     canSmelt = true
  140. end
  141.  
  142. function smelt()
  143.     while(true) do
  144.         if(canSmelt == false) then
  145.             print("Please give the desired target to smelt: ")
  146.             tar = read()
  147.  
  148.             loadTar(tar)
  149.         end
  150.         sleep(0.1)
  151.     end
  152. end
  153.  
  154. function isDone()
  155.     fuelChest = chests[1]
  156.     allEmpty = true
  157.     while(true) do
  158.         if(canSmelt) then
  159.             for idx, furnace in pairs(furnaces) do
  160.                 for slot, item in pairs(furnace.list()) do
  161.                     if(slot == 1 and item.count ~= 0) then
  162.                         allEmpty = false
  163.                     end
  164.                 end
  165.             end
  166.             if(allEmpty) then
  167.                 canSmelt = false
  168.                 smelting = false
  169.                 for _, furnace in pairs(furnaces) do
  170.                     emptySlot(furnace, fuelChest, 3)
  171.                     emptySlot(furnace, fuelChest, 2)
  172.                 end
  173.             end
  174.         end
  175.         allEmpty = true
  176.        sleep(0.1)
  177.    end
  178. end
  179.  
  180. function drawGUI()
  181.     while(true) do
  182.         draw_text(1, 1, "/\\", colors.white, colors.gray)
  183.         draw_text(1, 7, "\\/", colors.white, colors.gray)
  184.         local btnColor = colors.gray
  185.             if(canSmelt) then
  186.                 btnColor = colors.green
  187.             end
  188.             if(smelting) then
  189.                 btnColor = colors.blue
  190.             end
  191.        
  192.         draw_text(2, 19, "SMELT", colors.white, btnColor)
  193.         getInventories()
  194.         drawFurnaceStatus()
  195.         sleep(1)
  196.         clear()
  197.         --term.clear()
  198.         --term.setCursorPos(1,1)
  199.     end
  200. end
  201.  
  202. function drawFurnaceStatus()
  203.         local row = 1
  204.         local column = 1
  205.         local maxHeight = 5
  206.         for idx, furnace in pairs(furnaces) do
  207.             local textColor = colors.gray
  208.             local itemCount = 0
  209.             furnaceInv = furnace.list()
  210.                if(furnaceInv[1] ~= nil) then
  211.                     if(furnaceInv[2] ~= nil) then
  212.                         textColor = colors.green
  213.                     else
  214.                         textColor = colors.blue
  215.                     end
  216.                     itemCount = furnaceInv[1].count
  217.                end
  218.             draw_text(column * 2, 7 + row * 2, ("%d"):format(itemCount), colors.white, textColor)
  219.             row = row + 1
  220.             if(row == maxHeight) then
  221.                 row = 1
  222.                 column = column + 1
  223.             end
  224.         end
  225.  end
  226. parallel.waitForAny(drawGUI, button, smelt, refuel, isDone)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement