minecraft-cc-tools/libs/turtle-utils.lua
2024-05-19 16:42:52 +02:00

189 lines
3.4 KiB
Lua

local turtleUtils = {}
local DEFAULT_IDLE_TIME = 2
turtleUtils.getInventory = function(side)
side = side or 'front'
local inv = peripheral.wrap(side)
if peripheral.hasType(inv, 'inventory') then
return inv
end
return nil
end
function waitFor(predicate, sleepTime)
sleepTime = sleepTime or DEFAULT_IDLE_TIME
while true do
local result = predicate()
if result ~= nil and result ~= false then
return result
end
os.sleep(sleepTime)
end
end
turtleUtils.waitForInventory = function(side, sleepTime)
return waitFor(function()
return turtleUtils.getInventory(side)
end, sleepTime)
end
turtleUtils.trySuck = function(suckFn, sleepTime)
suckFn = suckFn or turtle.suck
sleepTime = sleepTime or DEFAULT_IDLE_TIME
while not suckFn() do
os.sleep(sleepTime)
end
end
turtleUtils.trySuckUp = function()
return turtleUtils.trySuck(turtle.suckUp)
end
turtleUtils.trySuckDown = function()
return turtleUtils.trySuck(turtle.suckDown)
end
turtleUtils.tryDrop = function(dropFn, sleepTime)
dropFn = dropFn or turtle.drop
sleepTime = sleepTime or DEFAULT_IDLE_TIME
while true do
if turtle.getItemCount() == 0 then
return false
end
local dropOk = dropFn();
if (dropOk) then
return true
end
os.sleep(sleepTime)
end
end
turtleUtils.tryDropUp = function()
return turtleUtils.tryDrop(turtle.dropUp)
end
turtleUtils.tryDropDown = function()
return turtleUtils.tryDrop(turtle.dropDown)
end
turtleUtils.dropSlot = function(slotIndex, dropFn)
dropFn = dropFn or turtle.drop
if turtle.getItemCount(slotIndex) == 0 then
return false
end
if turtle.getSelectedSlot() ~= slotIndex then
turtle.select(slotIndex)
end
local ok, err = dropFn()
if not ok then
return false
end
return true
end
turtleUtils.digAll = function()
while turtle.dig() do end
end
turtleUtils.countFreeSlots = function()
local freeSlots = 0
for i=1, 16, 1 do
if turtle.getItemCount(i) == 0 then
freeSlots = freeSlots + 1
end
end
return freeSlots
end
turtleUtils.getFuelPercentage = function()
return (turtle.getFuelLevel() / turtle.getFuelLimit()) * 100
end
turtleUtils.isMatureCrop = function(inspectFn)
inspectFn = inspectFn or turtle.inspect
return function()
local isBlock, block = inspectFn()
local blockStateAge = block and block.state and block.state.age
return blockStateAge == 7
end
end
turtleUtils.selectFirstEmptySlot = function()
for i = 1, 16, 1 do
if turtle.getItemCount(i) == 0 then
turtle.select(i)
return true
end
end
return false
end
turtleUtils.selectItemByName = function(itemName)
for i = 1, 16, 1 do
local item = turtle.getItemDetail(i)
if item and item.name == itemName then
turtle.select(i)
return true
end
end
return false
end
turtleUtils.refuel = function(minFuel, suckFn)
suckFn = suckFn or turtle.suck
if not turtleUtils.selectFirstEmptySlot() then
return false
end
while turtle.getFuelLevel() < minFuel do
suckFn()
local ok = turtle.refuel()
if not ok then
return false
end
end
return true
end
turtleUtils.refuelAllFromInventory = function()
local initialSelectedSlot = turtle.getSelectedSlot()
for i=1, 16, 1 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
turtle.refuel()
end
end
if turtle.getSelectedSlot() ~= initialSelectedSlot then
turtle.select(initialSelectedSlot)
end
end
return turtleUtils