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

96 lines
2.1 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 prepareCraftShape()
turtle.transferTo(2, 1)
turtle.transferTo(3, 1)
turtle.transferTo(5, 1)
turtle.transferTo(7, 1)
turtle.transferTo(9, 1)
turtle.transferTo(10, 1)
turtle.transferTo(11, 1)
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)
if not pushOk then
error('cannot pushItems from storage to buffer')
end
prepareCraftShape()
craft()
dropSelected()
end
end
main()