From 74821942cbe8b5f14a0f77b704c8e6bc20a2fd54 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:52:19 +0100 Subject: [PATCH] Inlined item matching function. --- src/itemscript.lua | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 538934e..305e9ac 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -22,18 +22,6 @@ local function stackMatches(itemStack, name, fuzzy) ) end --- Creates a filter function that filters on items whose names match the given list of names. -local function makeItemNamesFilter(names, fuzzy) - return function(item) - for _, itemName in pairs(names) do - if stackMatches(item, itemName, fuzzy) then - return true - end - end - return false - end -end - local function notFilter(filter) return function(item) return not filter(item) @@ -90,11 +78,13 @@ local function parseItemFilterExpression(expr) if namespaceSeparatorIdx == nil and not fuzzy then expr = "minecraft:" .. expr end - local filter = makeItemNamesFilter({expr}, fuzzy) - if negated then - filter = notFilter(filter) + return function(item) + local matches = stackMatches(item, expr, fuzzy) + if negated then + matches = not matches + end + return matches end - return filter end -- Converts an arbitrary variable into a filter; useful for any function that's public, so users can supply any filter. @@ -118,11 +108,6 @@ local function convertToFilter(var) end end --- Convenience function for creating a filter function that allows specifying fuzziness. -function itemscript.nameFilter(name, fuzzy) - return makeItemNamesFilter({name}, fuzzy) -end - -- Gets the total number of items in the turtle's inventory that match the given expression. function itemscript.totalCount(filterExpr) local filter = convertToFilter(filterExpr)