This commit is contained in:
Crispy 2025-05-23 15:45:14 +02:00
parent c78597a8fb
commit 760729ee4b
14 changed files with 126 additions and 102 deletions

View file

@ -1,7 +1,16 @@
pp = require("cc.pretty")
pFront = peripheral.wrap("front")
pDown = peripheral.wrap("bottom")
function pFront(fn, ...)
return peripheral.call("front", fn, unpack(arg))
end
function pBottom(fn, ...)
return peripheral.call("bottom", fn, unpack(arg))
end
function pTop(fn, ...)
return peripheral.call("top", fn, unpack(arg))
end
function findItems(item_list, target)
for i, v in pairs(item_list) do
@ -12,31 +21,8 @@ function findItems(item_list, target)
return nil
end
-- uses selected item
function insertForward(slot, count)
turtle.dropDown(count)
pFront.pullItems("bottom", 1, 64, slot)
end
function takeForward(slot)
-- pDown.pullItems("front", slot, 64, 1)
peripheral.call("bottom", "pullItems", "front", slot or 1)
turtle.suckDown()
end
function insertDepot(count)
turtle.dropDown(count)
peripheral.call("front", "pullItem", "bottom")
end
function takeDepot()
peripheral.call("front", "pushItem", "bottom")
turtle.suckDown()
end
function takeItems(type, count)
local item_list = pFront.list()
local item_list = pFront("list")
local slot = findItems(item_list, type)
if slot == nil then
@ -44,18 +30,31 @@ function takeItems(type, count)
return false
end
empty_slot = pFront.size()
-- todo error if not empty
pFront.pullItems("front", 1, 64, empty_slot) -- empty first slot
pFront.pullItems("front", slot, 64, 1) -- get target item to first slot
pFront.pullItems("front", empty_slot, 64, slot) -- empty last slot for next time
if slot ~= 1 then
local size = pFront("size")
local empty_slot = nil
for i = 1, size do
if pFront("getItemDetail", i) == nil then
empty_slot = i
break
end
end
if empty_slot == nil then
printError("no empty slot in chest, pls fix")
exit()
end
if empty_slot ~= 1 then
pFront("pullItems", "front", 1, 64, empty_slot) -- empty first slot
end
pFront("pullItems", "front", slot, 64, 1) -- get target item to first slot
end
return turtle.suck(count)
end
function selectItem(name, has_nbt)
has_nbt = has_nbt or false
if name == nil or name == "nil" then
for i = 1,16 do
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
turtle.select(i)
return
@ -64,7 +63,7 @@ function selectItem(name, has_nbt)
printError("no empty slot found")
return
end
for i = 1,16 do
for i = 1, 16 do
detail = turtle.getItemDetail(i, true);
if detail and stripModname(detail.name) == name then
if has_nbt == (detail.nbt ~= nil) then
@ -78,10 +77,10 @@ end
function getMissing(needed_items)
missing = {}
for name,count in pairs(needed_items) do
for name, count in pairs(needed_items) do
missing[name] = count
end
target_contents = peripheral.call("front", "list")
target_contents = pFront("list")
for _, target_item in pairs(target_contents) do
for name, missing_count in pairs(missing) do
@ -98,3 +97,23 @@ function getMissing(needed_items)
return missing
end
function insertForward(slot, count)
turtle.dropDown(count)
pFront("pullItems", "bottom", 1, 64, slot)
end
function takeForward(slot)
pBottom("pullItems", "front", slot or 1)
turtle.suckDown()
end
function insertDepot(count)
turtle.dropDown(count)
pFront("pullItem", "bottom")
end
function takeDepot()
pFront("pushItem", "bottom")
turtle.suckDown()
end