local turtleUtils = require('libs/turtle-utils') local NB_NEEDED_COAL = 64 * 2 local WAIT_INVENTORY_TIME = 3 local IDLE_TIME = 30 local IDLE_TIME_ENOUGH_COAL = 120 local MIN_ESSENCE_NEEDED = 8 local COAL_ESSENCE_NAME = 'mysticalagriculture:coal_essence' local COAL_NAME = 'minecraft:coal' local function countCoalItems(inventory) local total = 0 for slot, item in pairs(inventory.list()) do if item.name == COAL_NAME then total = total + item.count end end return total end local function waitForNotEnoughCoal(inventory) while true do local nbCoals = countCoalItems(inventory) if nbCoals < NB_NEEDED_COAL then return end os.sleep(IDLE_TIME_ENOUGH_COAL) end end -- the slot of the coal essence is returned local function waitForCoalEssence(inventory) while true do for slot, item in pairs(inventory.list()) do if item.name == COAL_ESSENCE_NAME and item.count >= MIN_ESSENCE_NEEDED then return slot end end os.sleep(IDLE_TIME) end end local function strictTransferOne(slot) local ok = turtle.transferTo(slot, 1) if not ok then error('cannot transfer item') end return ok end local function prepareCraftShape() strictTransferOne(2) strictTransferOne(3) strictTransferOne(5) strictTransferOne(7) strictTransferOne(9) strictTransferOne(10) strictTransferOne(11) end local function craft() local craftOk = turtle.craft() if not craftOk then error('cannot craft') end return craftOk end local function dropSelected() turtle.turnLeft() turtle.turnLeft() turtle.drop() turtle.turnRight() turtle.turnRight() end local function findBufferChestOrientation() for i=1, 3, 1 do local inv = turtleUtils.getInventory('front') if inv and #inv.list() == 0 then return end turtle.turnRight() end error('buffer inventory not found (empty chest expected)') end local function main() turtle.select(1) findBufferChestOrientation() print('> Waiting for the back inventory (storage)') local storageInventory = turtleUtils.waitForInventory('back', WAIT_INVENTORY_TIME) print('> Waiting for the front inventory (buffer chest)') local bufferInventory = turtleUtils.waitForInventory('front', WAIT_INVENTORY_TIME) print('> coal-crafter process started') while true do storageInventory = turtleUtils.waitForInventory('back', WAIT_INVENTORY_TIME) bufferInventory = turtleUtils.waitForInventory('front', WAIT_INVENTORY_TIME) waitForNotEnoughCoal(storageInventory) local coalEssenceSlot = waitForCoalEssence(storageInventory) local pushOk = storageInventory.pushItems(peripheral.getName(bufferInventory), coalEssenceSlot, MIN_ESSENCE_NEEDED) if not pushOk then error('cannot pushItems from storage to buffer') end local suckOk, suckErr = turtle.suck() if not suckOk then error('cannot suck into front buffer chest: ' .. tostring(suckErr)) end prepareCraftShape() craft() dropSelected() end end main()