Inlined item matching function.

This commit is contained in:
Andrew Lalis 2022-12-21 14:52:19 +01:00
parent 57b9c38498
commit 74821942cb
1 changed files with 6 additions and 21 deletions

View File

@ -22,18 +22,6 @@ local function stackMatches(itemStack, name, fuzzy)
) )
end 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) local function notFilter(filter)
return function(item) return function(item)
return not filter(item) return not filter(item)
@ -90,11 +78,13 @@ local function parseItemFilterExpression(expr)
if namespaceSeparatorIdx == nil and not fuzzy then if namespaceSeparatorIdx == nil and not fuzzy then
expr = "minecraft:" .. expr expr = "minecraft:" .. expr
end end
local filter = makeItemNamesFilter({expr}, fuzzy) return function(item)
if negated then local matches = stackMatches(item, expr, fuzzy)
filter = notFilter(filter) if negated then
matches = not matches
end
return matches
end end
return filter
end end
-- Converts an arbitrary variable into a filter; useful for any function that's public, so users can supply any filter. -- 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
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. -- Gets the total number of items in the turtle's inventory that match the given expression.
function itemscript.totalCount(filterExpr) function itemscript.totalCount(filterExpr)
local filter = convertToFilter(filterExpr) local filter = convertToFilter(filterExpr)