Change to use ItemCollection

This commit is contained in:
Cody Young 2023-03-21 14:45:43 -05:00
parent cb19161ddd
commit 523696db16
1 changed files with 27 additions and 9 deletions

View File

@ -3,7 +3,7 @@ local component = require("component")
local driver = {} local driver = {}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
---------------------------------- Item Class ---------------------------------- ------------------------------- ItemStack Class -------------------------------
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local ItemStack = {} local ItemStack = {}
@ -11,12 +11,12 @@ ItemStack.__index = ItemStack
local function ItemStack_new(item) local function ItemStack_new(item)
checkArg(1, item, "table"); checkArg(1, item, "table");
local out = { local output = {
data = item.getValue1(), data = item.getValue1(),
quantity = item.getValue2() quantity = item.getValue2()
} }
setmetatable(out, ItemStack) setmetatable(output, ItemStack)
return out return output
end end
function ItemStack:getName() function ItemStack:getName()
@ -30,6 +30,28 @@ function ItemStack:getName()
return self.data.getName() return self.data.getName()
end end
--------------------------------------------------------------------------------
----------------------------- ItemCollection Class -----------------------------
--------------------------------------------------------------------------------
local ItemCollection = {}
ItemCollection.__index = ItemCollection
local function ItemCollection_new(items)
checkArg(1, item, "table");
local output = {}
output.searchTable = {}
for i,item in pairs(items) do
local itemStack = ItemStack_new(item)
table[i] = itemStack
output.searchTable[itemStack:getName()] = i
end
setmetatable(output, ItemCollection);
return output
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-------------------------------- Private Driver -------------------------------- -------------------------------- Private Driver --------------------------------
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -49,11 +71,7 @@ function driver.getItems()
if not pipe.getAvailableItems and not pipe.makeRequest then if not pipe.getAvailableItems and not pipe.makeRequest then
error("Logistics pipe is not a a requesting pipe") error("Logistics pipe is not a a requesting pipe")
end end
local output = {} return ItemCollection_new(pipe.getAvailableItems())
for _,item in pairs(pipe.getAvailableItems()) do
table.insert(output, ItemStack_new(item))
end
return output
end end
return driver return driver