From a1364a4bff37800a4367ee32c240da981fe6f854 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Fri, 24 May 2024 18:41:33 +0200 Subject: [PATCH] fix(coal-creafter): be sure to select slot 1 + fix dropAll --- coal-crafter.lua | 21 +++++++++++++++++---- libs/turtle-utils.lua | 8 ++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/coal-crafter.lua b/coal-crafter.lua index ca9efc5..592328e 100644 --- a/coal-crafter.lua +++ b/coal-crafter.lua @@ -7,6 +7,7 @@ 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' @@ -15,17 +16,29 @@ 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) - if not turtleUtils.dropSlot(i, dropFn) then - error('please empty the turtle inventory', 0) + local errorPrinted = false + while 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) @@ -116,8 +129,6 @@ local function findStorageChestOrientation() end local function main() - turtle.select(1) - findStorageChestOrientation() print('> Waiting for the back inventory (storage)') @@ -129,6 +140,7 @@ local function main() print('> drop all') dropAll(STORAGE_INVENTORY_SIDE, dropInStorageFn) + turtle.select(CRAFTING_SLOT) print('> coal-crafter process started') while true do @@ -149,6 +161,7 @@ local function main() error('cannot suck into front buffer chest: ' .. tostring(suckErr)) end + turtleUtils.ensureSelected(CRAFTING_SLOT) prepareCraftShape() craft() dropInStorageFn() diff --git a/libs/turtle-utils.lua b/libs/turtle-utils.lua index 016ec92..41550ee 100644 --- a/libs/turtle-utils.lua +++ b/libs/turtle-utils.lua @@ -213,6 +213,14 @@ turtleUtils.selectItemBy = function(predicate) return false end +turtleUtils.ensureSelected = function(slot) + if turtle.getSelectedSlot() ~= slot then + return turtle.select(slot) + end + + return true +end + turtleUtils.refuel = function(minFuel, suckFn, sleepTime) suckFn = suckFn or turtle.suck sleepTime = sleepTime or DEFAULT_IDLE_TIME