minecraft-cc-tools/coal-crafter.lua
2024-05-20 01:53:31 +02:00

106 lines
2.3 KiB
Lua

local turtleUtils = require('libs/turtle-utils')
local WAIT_INVENTORY_TIME = 3
local IDLE_TIME = 10
local MIN_ESSENCE_NEEDED = 8
local COAL_ESSENCE_NAME = 'mysticalagriculture:coal_essence'
-- 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 left 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)
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
prepareCraftShape()
craft()
dropSelected()
end
end
main()