Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local furnaces = { peripheral.find("minecraft:furnace") }
- local furnaceSlots = {
- input = 1,
- fuel = 2,
- output = 3
- }
- local input = peripheral.wrap("minecraft:barrel_21")
- local fuel = peripheral.wrap("minecraft:barrel_22")
- local output = peripheral.wrap("minecraft:barrel_23")
- local function CountNonEmptySlots(inventory)
- local count = 0
- for _, _ in pairs(inventory.list()) do
- count = count + 1
- end
- return count
- end
- local function FindFreeFurnace(itemName)
- local bestFurnace = nil --Find a furnace which can preferrably take more of the same item, otherwise is empty, otherwise no best furnace
- for index, furnace in pairs(furnaces) do
- local furnaceInputDetails = furnace.getItemDetail(furnaceSlots.input)
- if furnaceInputDetails ~= nil and furnaceInputDetails.name == itemName and furnaceInputDetails.count < 64 then
- --Furnace has space for more of this item
- bestFurnace = furnace
- break;
- elseif furnaceInputDetails == nil or furnaceInputDetails.count == 0 then
- --Empty furnace which can take any item
- bestFurnace = furnace
- end
- end
- return bestFurnace
- end
- local function FindFuellessFurnace(fuelItemName)
- local fuellessFurnace = nil
- for index, furnace in pairs(furnaces) do
- local furnaceFuelDetails = furnace.getItemDetail(furnaceSlots.fuel)
- if furnaceFuelDetails == nil or furnaceFuelDetails.count == 0 then
- --Furnace is empty so should be next target
- fuellessFurnace = furnace
- break;
- elseif furnaceFuelDetails ~= nil and furnaceFuelDetails.name == fuelItemName and furnaceFuelDetails.count < 64 then
- fuellessFurnace = furnace
- end
- end
- return fuellessFurnace
- end
- local function FindPendingOutputFurnace()
- local pendingOutputFurnace = nil
- for index, furnace in pairs(furnaces) do
- local furnaceOutputDetails = furnace.getItemDetail(furnaceSlots.output)
- if furnaceOutputDetails ~= nil and furnaceOutputDetails.count >= 1 then
- pendingOutputFurnace = furnace
- break;
- end
- end
- return pendingOutputFurnace
- end
- local function CheckPendingJobs()
- while true do
- local pendingItemSlots = input.list()
- local pendingItemSlotCount = CountNonEmptySlots(input)
- if pendingItemSlotCount > 0 then
- --There is at least 1 pending slot
- print("Pending input slots: "..pendingItemSlotCount)
- for pendingSlotNumber, pendingSlotItem in pairs(pendingItemSlots) do
- local freeFurnace = FindFreeFurnace(pendingSlotItem.name)
- if freeFurnace ~= nil then
- --We have a furnace we can ram these items into
- local freeFurnaceInputSlotDetails = freeFurnace.getItemDetail(furnaceSlots.input)
- if freeFurnaceInputSlotDetails == nil then freeFurnaceInputSlotDetails = { count = 0 } end
- local remainingCapacity = freeFurnace.getItemLimit(furnaceSlots.input) - freeFurnaceInputSlotDetails.count
- term.setTextColour(colours.lightblue)
- print("Placing "..pendingSlotItem.count.." item(s) into input of "..peripheral.getName(freeFurnace))
- print()
- term.setTextColour(colours.white)
- input.pushItems(peripheral.getName(freeFurnace), pendingSlotNumber, remainingCapacity, furnaceSlots.input)
- end
- end
- else
- sleep(0.5)
- end
- end
- end
- local function CheckFinishedJobs()
- while true do
- local pendingOutputFurnace = FindPendingOutputFurnace()
- if pendingOutputFurnace ~= nil then
- term.setTextColour(colours.green)
- print("Pulling items out of "..peripheral.getName(pendingOutputFurnace))
- print()
- output.pullItems(peripheral.getName(pendingOutputFurnace), furnaceSlots.output)
- else
- sleep(0.5)
- end
- end
- end
- local function CheckRefueling()
- while true do
- local availableFuelSlots = fuel.list()
- local availableFuelSlotCount = CountNonEmptySlots(fuel)
- if availableFuelSlotCount > 0 then
- --There is at least 1 available slot
- --print("Available fuel slots: "..availableFuelSlotCount)
- for availableSlotNumber, availableSlotItem in pairs(availableFuelSlots) do
- local freeFurnace = FindFuellessFurnace(availableSlotItem.name)
- if freeFurnace ~= nil then
- --We have a furnace we can ram these items into
- local freeFurnaceFuelSlotDetails = freeFurnace.getItemDetail(furnaceSlots.fuel)
- if freeFurnaceFuelSlotDetails == nil then freeFurnaceFuelSlotDetails = { count = 0 } end
- local remainingCapacity = freeFurnace.getItemLimit(furnaceSlots.fuel) - freeFurnaceFuelSlotDetails.count
- term.setTextColour(colours.orange)
- print("Placing "..availableSlotItem.count.." item(s) into fuel of "..peripheral.getName(freeFurnace))
- print()
- term.setTextColour(colours.white)
- fuel.pushItems(peripheral.getName(freeFurnace), availableSlotNumber, remainingCapacity, furnaceSlots.fuel)
- end
- end
- else
- sleep(0.5)
- end
- end
- end
- print("Processing started")
- parallel.waitForAll(CheckPendingJobs, CheckFinishedJobs, CheckRefueling)
Add Comment
Please, Sign In to add comment