movescript/src/itemscript.lua

132 lines
3.7 KiB
Lua
Raw Normal View History

2022-12-17 15:15:34 +00:00
--[[
Itemscript - A simplified set of methods for item manipulation.
Author: Andrew Lalis <andrewlalisofficial@gmail.com>
]]--
2022-12-17 15:35:11 +00:00
VERSION = "0.0.1"
2022-12-17 15:15:34 +00:00
local t = turtle
-- The itemscript module. Functions defined within this table are exported.
local itemscript = {}
-- Determines if an item stack matches the given name.
-- If fuzzy, then the item name will be matched against the given name.
local function stackMatches(itemStack, name, fuzzy)
2022-12-17 15:15:34 +00:00
return itemStack ~= nil and
(
(not fuzzy and itemStack.name == name) or
string.find(itemStack.name, name)
)
end
--[[
The following describes an item filter:
A table containing a filter mechanism.
{
filterFunction = stackMatches,
fuzzy = false,
whitelist = true
}
The filterFunction is defined like so:
function filterFunction(item, filter)
return true | false
end
]]--
local function makeItemFilter(var, fuzzy, whitelist)
local filter = {
filterFunction = nil,
fuzzy = fuzzy or false,
whitelist = whitelist
}
if type(var) == "string" then
-- If the filter is a single item name, define a single-item filter that matches against the name.
filter.filterFunction = function (item, filter)
local matches = stackMatches(item, var, filter.fuzzy)
if filter.whitelist then
return matches
else
return not matches
end
end
elseif type(var) == "table" then
-- If the filter is a list of item names, define a multi-item filter.
filter.filterFunction = function (item, filter)
for _, itemName in pairs(var) do
if filter.whitelist and stackMatches(item, itemName, filter.fuzzy) then
return true
elseif not filter.whitelist and not stackMatches(item, itemName, filter.fuzzy) then
return false
end
end
-- If whitelist and we couldn't find a match, return false.
-- If blacklist and we couldn't find a non-match, return true.
return not filter.whitelist
end
elseif type(var) == "function" then
-- Otherwise, just use the provided filter.
filter.filterFunction = var
end
filter.apply = function(item)
return filter.filterFunction(item, filter)
end
return filter
end
2022-12-17 15:15:34 +00:00
-- Gets the total number of items of a certain type in the turtle's inventory.
function itemscript.totalCount(filter)
2022-12-17 15:15:34 +00:00
local count = 0
for i = 1, 16 do
local item = t.getItemDetail(i)
if filter.apply(item) then
2022-12-17 15:15:34 +00:00
count = count + item.count
end
end
return count
end
-- Selects a slot containing at least one of the given item type.
-- Returns a boolean indicating whether we could find and select the item.
function itemscript.select(filter)
2022-12-17 15:15:34 +00:00
for i = 1, 16 do
local item = t.getItemDetail(i)
if filter.apply(item) then
2022-12-17 15:15:34 +00:00
t.select(i)
return true
end
end
return false
end
-- Helper function to drop items in a flexible way, using a drop function and filtering function.
local function dropFiltered(dropFunction, filter)
2022-12-17 15:15:34 +00:00
for i = 1, 16 do
local item = t.getItemDetail(i)
if filter.apply(item) then
2022-12-17 15:15:34 +00:00
t.select(i)
dropFunction()
end
end
end
function itemscript.dropAll(filter)
dropFiltered(t.drop, filter)
2022-12-17 15:15:34 +00:00
end
function itemscript.dropAllDown(filter)
dropFiltered(t.dropDown, filter)
2022-12-17 20:46:14 +00:00
end
function itemscript.dropAllUp(filter)
dropFiltered(t.dropUp, filter)
2022-12-17 15:15:34 +00:00
end
-- Cleans up the turtle's inventory by compacting all stacks of items.
function itemscript.organize()
error("Not yet implemented.")
end
return itemscript