Big speedup

Using a single call to get an iterator using getAllStacks
instead of doing a call for each slot individually
master
D4VID 10 months ago
parent 5577a1d9c5
commit 3e693c2756

@ -85,17 +85,21 @@ end
---@return table|nil ---@return table|nil
local function read_chest_contents() local function read_chest_contents()
local contents = {} local contents = {}
local size = inventory.getInventorySize(sides.forward)
if not size then local iter = inventory.getAllStacks(sides.forward)
return nil if not iter then
return nil -- not facing an inventory
end end
for slot = 1, size do
local stack = inventory.getStackInSlot(sides.forward, slot) local stack = iter()
if stack then while stack do
if stack.name then -- check if the slot has an item in it
local name = stack.name .. ";" .. stack.damage local name = stack.name .. ";" .. stack.damage
add(contents, name, stack.size) add(contents, name, stack.size)
end end
stack = iter()
end end
return contents return contents
end end
@ -105,11 +109,12 @@ local function store_item()
-- check if that item is present in the inventory -- check if that item is present in the inventory
local item = inventory.getStackInInternalSlot() local item = inventory.getStackInInternalSlot()
local size = inventory.getInventorySize(sides.forward) local iter = inventory.getAllStacks(sides.forward)
local slot = 1
local empty_slot = -1 local empty_slot = -1
for slot = 1, size do local stack = iter()
local stack = inventory.getStackInSlot(sides.forward, slot) while stack do
if stack then if stack.name then
if stack.name == item.name then if stack.name == item.name then
inventory.dropIntoSlot(sides.forward, slot) inventory.dropIntoSlot(sides.forward, slot)
-- check if all items have been stored -- check if all items have been stored
@ -124,6 +129,8 @@ local function store_item()
empty_slot = slot empty_slot = slot
end end
end end
stack = iter()
slot = slot + 1
end end
-- item not present in inventory -- item not present in inventory
@ -157,20 +164,23 @@ local function fetch_item(item, output_slot, count)
local name, damage = parse_item(item, ";") local name, damage = parse_item(item, ";")
robot.select(output_slot) robot.select(output_slot)
local size = inventory.getInventorySize(sides.forward) local iter = inventory.getAllStacks(sides.forward)
if not size then if not iter then
print('Not facing a chest') print('Not facing a chest')
return false return false
end end
for slot = 1, size do local stack = iter()
local stack = inventory.getStackInSlot(sides.forward, slot) local slot = 1
if stack then while stack do
if stack.name then
if stack.name == name and stack.damage == damage then if stack.name == name and stack.damage == damage then
inventory.suckFromSlot(sides.forward, slot, count) inventory.suckFromSlot(sides.forward, slot, count)
return true return true
end end
end end
stack = iter()
slot = slot + 1
end end
print('Missing ' .. item) print('Missing ' .. item)

Loading…
Cancel
Save