ComputerCraft/Modules/hare.lua

53 lines
1.3 KiB
Lua
Raw Normal View History

2020-11-06 11:00:34 +00:00
--[[Function Module program by Al Sweigart
Provides useful utility functions.]]
-- selectItem() selects the inventory
-- slot with the named item, returns
-- true if found and false if not
function selectItem(name)
-- check all inventory slots
local item
for slot = 1, 16 do
item = turtle.getItemDetail(slot)
if item ~= nil and item['name'] == name then
turtle.select(slot)
return true
end
end
return false -- couldn't find item
end
-- selectEmptySlot() selects inventory
-- slot that is empty, returns true if
-- found, false if no empty spaces
function selectEmptySlot()
-- loop through all slots
for slot = 1, 16 do
if turtle.getItemCount(slot) == 0 then
turtle.select(slot)
return true
end
end
return false -- couldn't find empty space
end
2020-11-06 19:36:05 +00:00
-- getItemFromChest(chestItem) selects
-- something from a chest in front of the turtle
-- and pushes it into the turtle.
function getItemFromChest(chestItem)
sapling = peripheral.wrap('front') -- This find the chest at the front and allow you do things to it
selectEmptySlot()
for slot, item in pairs(sapling.getAllStacks()) do
if item.id == chestItem then
-- If the chest is on the north side of the turtle, the turtle is on the south side of the peripheral
sapling.pushItem('south', slot)
end
end
end