From d27320c135b0d1210e335f4b9c0540cb6a29fb1d Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Fri, 5 May 2023 09:36:29 +0200 Subject: [PATCH] Organized slot functions. --- src/itemscript.lua | 60 ++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index bd26f71..0f12296 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -248,44 +248,52 @@ function itemscript.filterize(value) end end - --- Gets the total number of items in the turtle's inventory that match the given expression. -function itemscript.totalCount(filterExpr) +-- Finds the first matching slot for the given filter expression. +function itemscript.findSlot(filterExpr) local filter = itemscript.filterize(filterExpr) - local count = 0 + for i = 1, 16 do + local item = turtle.getItemDetail(i) + if filter(i) then return i end + end + return nil +end + +-- Gets a list of all inventory slots that match the given filter expression. +function itemscript.findSlots(filterExpr) + local filter = itemscript.filterize(filterExpr) + local slots = {} for i = 1, 16 do local item = turtle.getItemDetail(i) if filter(item) then - count = count + item.count + table.insert(slots, i) end end +end + +-- Gets the total number of items in the turtle's inventory that match the given expression. +function itemscript.totalCount(filterExpr) + local count = 0 + for _, slot in pairs(itemscript.findSlots(filterExpr)) do + local item = turtle.getItemDetail(slot) + count = count + item.count + end return count end -- Select the first slot containing a matching item stack for a filter. -- Returns a boolean indicating whether we could find and select the item. function itemscript.select(filterExpr) - local filter = itemscript.filterize(filterExpr) - for i = 1, 16 do - local item = turtle.getItemDetail(i) - if filter(item) then - turtle.select(i) - return true - end + local slot = itemscript.findSlot(filterExpr) + if slot ~= nil then + turtle.select(slot) + return true end return false end -- Selects a random slot containing a matching item stack. function itemscript.selectRandom(filterExpr) - local filter = itemscript.filterize(filterExpr) - local eligibleSlots = {} - for i = 1, 16 do - local item = turtle.getItemDetail(i) - if filter(item) then - table.insert(eligibleSlots, i) - end - end + local eligibleSlots = itemscript.findSlots(filterExpr) if #eligibleSlots == 0 then return false end local slot = eligibleSlots[math.random(1, #eligibleSlots)] turtle.select(slot) @@ -330,18 +338,6 @@ function itemscript.selectEmptyOrWait() end end --- Gets a list of all inventory slots that match the given filter expression. -function itemscript.findSlots(filterExpr) - local filter = itemscript.filterize(filterExpr) - local slots = {} - for i = 1, 16 do - local item = turtle.getItemDetail(i) - if filter(item) then - table.insert(slots, i) - end - end -end - -- Helper function to drop items in a flexible way, using a drop function and filtering function. local function dropFiltered(dropFunction, filterExpr) local filter = itemscript.filterize(filterExpr)