You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
miner/chest.lua

72 lines
1.8 KiB

local component = require("component")
local robot = require("robot")
local sides = require("sides")
local inventory = component.inventory_controller;
local function contains(arr, val)
for _, value in ipairs(arr) do
if value == val then
return true
end
end
return false
end
local function store_stack(slot)
robot.select(slot)
local slots = inventory.getInventorySize(sides.forward)
for s = 1, slots do
local result, _ = inventory.dropIntoSlot(sides.forward, s)
if result then
local stack = inventory.getStackInInternalSlot(slot)
if stack == nil then
break
end
end
end
end
local function store(whitelist)
local slots = robot.inventorySize()
for slot = 2, slots do
local item = inventory.getStackInInternalSlot(slot)
if item then
if whitelist ~= nil then
if contains(whitelist, item.name) then
store_stack(slot)
end
else
store_stack(slot)
end
end
end
end
---Take an item from the facing inventory and put it in the specified internal slot
---@param item string
---@param internal_slot number
---@return boolean success
local function take(item, internal_slot)
local slots = inventory.getInventorySize(sides.forward)
for slot = 1, slots do
local stack = inventory.getStackInSlot(sides.forward, slot)
if stack then
if stack.name == item then
robot.select(internal_slot)
inventory.suckFromSlot(sides.forward, slot, robot.space()) -- take as many items to fill the slot
return true
end
end
end
return false
end
return {
store = store,
take = take,
}