alexhorner

Simple Smelting Automation

Jan 1st, 2023 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. local furnaces = { peripheral.find("minecraft:furnace") }
  2.  
  3. local furnaceSlots = {
  4.     input = 1,
  5.     fuel = 2,
  6.     output = 3
  7. }
  8.  
  9. local input = peripheral.wrap("minecraft:barrel_21")
  10. local fuel = peripheral.wrap("minecraft:barrel_22")
  11. local output = peripheral.wrap("minecraft:barrel_23")
  12.  
  13. local function CountNonEmptySlots(inventory)
  14.     local count = 0
  15.  
  16.     for _, _ in pairs(inventory.list()) do
  17.         count = count + 1
  18.     end
  19.  
  20.     return count
  21. end
  22.  
  23. local function FindFreeFurnace(itemName)
  24.     local bestFurnace = nil --Find a furnace which can preferrably take more of the same item, otherwise is empty, otherwise no best furnace
  25.  
  26.     for index, furnace in pairs(furnaces) do
  27.         local furnaceInputDetails = furnace.getItemDetail(furnaceSlots.input)
  28.  
  29.         if furnaceInputDetails ~= nil and furnaceInputDetails.name == itemName and furnaceInputDetails.count < 64 then
  30.             --Furnace has space for more of this item
  31.             bestFurnace = furnace
  32.             break;
  33.         elseif furnaceInputDetails == nil or furnaceInputDetails.count == 0 then
  34.             --Empty furnace which can take any item
  35.             bestFurnace = furnace
  36.         end
  37.     end
  38.    
  39.     return bestFurnace
  40. end
  41.  
  42. local function FindFuellessFurnace(fuelItemName)
  43.     local fuellessFurnace = nil
  44.  
  45.     for index, furnace in pairs(furnaces) do
  46.         local furnaceFuelDetails = furnace.getItemDetail(furnaceSlots.fuel)
  47.  
  48.         if furnaceFuelDetails == nil or furnaceFuelDetails.count == 0 then
  49.             --Furnace is empty so should be next target
  50.             fuellessFurnace = furnace
  51.             break;
  52.         elseif furnaceFuelDetails ~= nil and furnaceFuelDetails.name == fuelItemName and furnaceFuelDetails.count < 64 then
  53.             fuellessFurnace = furnace
  54.         end
  55.     end
  56.    
  57.     return fuellessFurnace
  58. end
  59.  
  60. local function FindPendingOutputFurnace()
  61.     local pendingOutputFurnace = nil
  62.  
  63.     for index, furnace in pairs(furnaces) do
  64.         local furnaceOutputDetails = furnace.getItemDetail(furnaceSlots.output)
  65.  
  66.         if furnaceOutputDetails ~= nil and furnaceOutputDetails.count >= 1 then
  67.             pendingOutputFurnace = furnace
  68.             break;
  69.         end
  70.     end
  71.  
  72.     return pendingOutputFurnace
  73. end
  74.  
  75. local function CheckPendingJobs()
  76.     while true do
  77.         local pendingItemSlots = input.list()
  78.         local pendingItemSlotCount = CountNonEmptySlots(input)
  79.        
  80.         if pendingItemSlotCount > 0 then
  81.             --There is at least 1 pending slot
  82.  
  83.             print("Pending input slots: "..pendingItemSlotCount)
  84.  
  85.             for pendingSlotNumber, pendingSlotItem in pairs(pendingItemSlots) do
  86.                 local freeFurnace = FindFreeFurnace(pendingSlotItem.name)
  87.  
  88.                 if freeFurnace ~= nil then
  89.                     --We have a furnace we can ram these items into
  90.                     local freeFurnaceInputSlotDetails = freeFurnace.getItemDetail(furnaceSlots.input)
  91.  
  92.                     if freeFurnaceInputSlotDetails == nil then freeFurnaceInputSlotDetails = { count = 0 } end
  93.  
  94.                     local remainingCapacity = freeFurnace.getItemLimit(furnaceSlots.input) - freeFurnaceInputSlotDetails.count
  95.                     term.setTextColour(colours.lightblue)
  96.                     print("Placing "..pendingSlotItem.count.." item(s) into input of "..peripheral.getName(freeFurnace))
  97.                     print()
  98.                     term.setTextColour(colours.white)
  99.                     input.pushItems(peripheral.getName(freeFurnace), pendingSlotNumber, remainingCapacity, furnaceSlots.input)
  100.                 end
  101.             end
  102.         else
  103.             sleep(0.5)
  104.         end
  105.     end
  106. end
  107.  
  108. local function CheckFinishedJobs()
  109.     while true do
  110.         local pendingOutputFurnace = FindPendingOutputFurnace()
  111.        
  112.         if pendingOutputFurnace ~= nil then
  113.             term.setTextColour(colours.green)
  114.             print("Pulling items out of "..peripheral.getName(pendingOutputFurnace))
  115.             print()
  116.             output.pullItems(peripheral.getName(pendingOutputFurnace), furnaceSlots.output)
  117.         else
  118.             sleep(0.5)
  119.         end
  120.     end
  121. end
  122.  
  123. local function CheckRefueling()
  124.     while true do
  125.         local availableFuelSlots = fuel.list()
  126.         local availableFuelSlotCount = CountNonEmptySlots(fuel)
  127.        
  128.         if availableFuelSlotCount > 0 then
  129.             --There is at least 1 available slot
  130.  
  131.             --print("Available fuel slots: "..availableFuelSlotCount)
  132.  
  133.             for availableSlotNumber, availableSlotItem in pairs(availableFuelSlots) do
  134.                 local freeFurnace = FindFuellessFurnace(availableSlotItem.name)
  135.  
  136.                 if freeFurnace ~= nil then
  137.                     --We have a furnace we can ram these items into
  138.                     local freeFurnaceFuelSlotDetails = freeFurnace.getItemDetail(furnaceSlots.fuel)
  139.  
  140.                     if freeFurnaceFuelSlotDetails == nil then freeFurnaceFuelSlotDetails = { count = 0 } end
  141.  
  142.                     local remainingCapacity = freeFurnace.getItemLimit(furnaceSlots.fuel) - freeFurnaceFuelSlotDetails.count
  143.                     term.setTextColour(colours.orange)
  144.                     print("Placing "..availableSlotItem.count.." item(s) into fuel of "..peripheral.getName(freeFurnace))
  145.                     print()
  146.                     term.setTextColour(colours.white)
  147.                     fuel.pushItems(peripheral.getName(freeFurnace), availableSlotNumber, remainingCapacity, furnaceSlots.fuel)
  148.                 end
  149.             end
  150.         else
  151.             sleep(0.5)
  152.         end
  153.     end
  154. end
  155.  
  156. print("Processing started")
  157. parallel.waitForAll(CheckPendingJobs, CheckFinishedJobs, CheckRefueling)
Add Comment
Please, Sign In to add comment