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 CRAFTING_SLOT = 1 local STORAGE_BUFFER_SIDE = 'bottom' local STORAGE_INVENTORY_SIDE = 'front' local dropInStorageFn = turtle.drop local suckFromBufferFn = turtle.suckDown local function dropAll(side, dropFn) local previousSelectedSlot = turtle.getSelectedSlot() for i=1, 16, 1 do local count = turtle.getItemCount(i) if count > 0 then turtleUtils.waitForInventory(side, IDLE_TIME) local errorPrinted = false while turtle.getItemCount(i) > 0 and not turtleUtils.dropSlot(i, dropFn) do if not errorPrinted then print('> please empty the turtle inventory') errorPrinted = true end os.sleep(WAIT_INVENTORY_TIME) end end end if previousSelectedSlot ~= turtle.getSelectedSlot() then turtle.select(previousSelectedSlot) end end 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() dropInStorageFn() end local function findStorageChestOrientation() for i=1, 4, 1 do local inv = turtleUtils.getInventory(STORAGE_INVENTORY_SIDE) if inv then return end turtle.turnRight() end error('storage chest inventory not found') end local function main() findStorageChestOrientation() print('> Waiting for the back inventory (storage)') local storageInventory = turtleUtils.waitForInventory(STORAGE_INVENTORY_SIDE, WAIT_INVENTORY_TIME) print('> Waiting for the front inventory (buffer chest)') local bufferInventory = turtleUtils.waitForInventory(STORAGE_BUFFER_SIDE, WAIT_INVENTORY_TIME) print('> drop all') dropAll(STORAGE_INVENTORY_SIDE, dropInStorageFn) if not turtleUtils.isInventoryEmpty() then error('Error: inventory is not empty', 0) end turtle.select(CRAFTING_SLOT) print('> coal-crafter process started') while true do storageInventory = turtleUtils.waitForInventory(STORAGE_INVENTORY_SIDE, WAIT_INVENTORY_TIME) bufferInventory = turtleUtils.waitForInventory(STORAGE_BUFFER_SIDE, 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 = suckFromBufferFn() if not suckOk then error('cannot suck into front buffer chest: ' .. tostring(suckErr)) end turtleUtils.ensureSelected(CRAFTING_SLOT) prepareCraftShape() craft() dropInStorageFn() end end main()