trainworld_computercraft/computer/13/inventory.lua
2025-05-22 20:51:25 +02:00

101 lines
2.3 KiB
Lua

pp = require("cc.pretty")
pFront = peripheral.wrap("front")
pDown = peripheral.wrap("bottom")
function findItems(item_list, target)
for i, v in pairs(item_list) do
if stripModname(v.name) == target then
return i
end
end
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 insertForwardDepot(count)
turtle.dropDown(count)
peripheral.call("front", "pullItem", "bottom")
end
function takeForwardDepot()
-- pDown.pullItems("front", slot, 64, 1)
peripheral.call("front", "pushItem", "bottom")
turtle.suckDown()
end
function takeItems(type, count)
item_list = pFront.list()
slot = findItems(item_list, type)
if slot == nil then
printError("could not find item " .. type .. " in chest")
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
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
if turtle.getItemCount(i) == 0 then
turtle.select(i)
return
end
end
printError("no empty slot found")
return
end
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
turtle.select(i)
return
end
end
end
printError("missing item", name, "nbt=", has_nbt)
end
function getMissing(needed_items)
missing = {}
for name,count in pairs(needed_items) do
missing[name] = count
end
target_contents = peripheral.call("front", "list")
for _, target_item in pairs(target_contents) do
for name, missing_count in pairs(missing) do
if stripModname(target_item.name) == name then
missing[name] = missing_count - target_item.count
if missing[name] < 1 then
missing[name] = nil
end
break
end
end
end
pp.pretty_print(missing)
return missing
end