Fix item controller state loading

This commit is contained in:
Gabriel Tofvesson 2024-10-04 01:03:23 +02:00
parent 437b93a688
commit 93657b8bb0

View File

@ -43,29 +43,27 @@ local function saveState(fname, ctrl)
end
local function loadState(fname, node)
local controller = nil
if not isLocked() then
print("Not locked! Loading cache...")
local file = fs.open(fname, "r")
if file ~= nil then
local ser = textutils.unserialize(file.readAll())
file.close()
return Storage:fromSerializable(ser)
end
controller = loadState(fname)
end
if controller == nil then
local nodeName = peripheral.getName(node)
local storageChests = {peripheral.find("inventory")}
for i,v in ipairs(storageChests) do
if peripheral.getName(v) == nodeName then
table.remove(storageChests, i)
break
end
print("Controller must scan chests...")
local nodeName = peripheral.getName(node)
local storageChests = {peripheral.find("inventory")}
for i,v in ipairs(storageChests) do
if peripheral.getName(v) == nodeName then
table.remove(storageChests, i)
break
end
controller = Storage:fromPeripherals(Storage.assumeHomogeneous(storageChests))
saveState(fname, controller)
end
local controller = Storage:fromPeripherals(Storage.assumeHomogeneous(storageChests))
saveState(fname, controller)
return controller
end