Allow detaching chests from storage
This commit is contained in:
parent
b760dcdbbb
commit
25244a9355
@ -50,23 +50,31 @@ function Storage:fromSerializable(ser)
|
||||
return obj
|
||||
end
|
||||
|
||||
function Storage:hasChest(name)
|
||||
for _,chest in ipairs(self) do
|
||||
function Storage:isAttached(name)
|
||||
for index,chest in ipairs(self) do
|
||||
if chest:getName() == name then
|
||||
return true, chest
|
||||
return true, chest, index
|
||||
end
|
||||
end
|
||||
return false, nil
|
||||
return false, nil, nil
|
||||
end
|
||||
|
||||
function Storage:attach(name, homogeneous)
|
||||
if self:hasChest(name) then
|
||||
if self:isAttached(name) then
|
||||
return
|
||||
end
|
||||
|
||||
table.insert(self, Chest:fromPeripheral(name, homogeneous))
|
||||
end
|
||||
|
||||
function Storage:detach(name)
|
||||
local attached, _, index = self:isAttached(name)
|
||||
if attached then
|
||||
return table.remove(self, index)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Storage:find(query)
|
||||
local result = {}
|
||||
for _,chest in ipairs(self) do
|
||||
@ -90,10 +98,10 @@ end
|
||||
|
||||
function Storage:findExtractTargets(query)
|
||||
local result = self:find(query)
|
||||
|
||||
|
||||
-- Extraction should prioritize emptying populated stacks
|
||||
table.sort(result, function(a, b) return a:getcount() < b:getCount() end)
|
||||
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user