diff --git a/main.lua b/main.lua index a441e07..9daa102 100644 --- a/main.lua +++ b/main.lua @@ -33,19 +33,14 @@ end local function main() local main_view = require("views.main") - local bundles = { - green = { - ["minecraft:kelp"] = 64, - ["minecraft:grass_block"] = 64 - }, - terraform = { - ["minecraft:grass_block"] = 64, - ["minecraft:stone"] = 64, - ["#green"] = 1 - } - } - local inventories = list_inventories() - main_view:prepare(inventories, bundles, "minecraft:barrel_4") + local save_file = "/storage-config.txt" + if fs.exists(save_file) then + main_view:load_from_file(save_file) + else + local inventories = list_inventories() + main_view:prepare(inventories, {}, "minecraft:barrel_4") + end + main_view.save_file = save_file local update_interval = 0.1 local update_timer = os.startTimer(update_interval) diff --git a/views/main.lua b/views/main.lua index d55a389..8f49d8c 100644 --- a/views/main.lua +++ b/views/main.lua @@ -296,6 +296,29 @@ function main_view:prepare(inventories, bundles, result_inventory) end end +function main_view:save_to_file(filename) + local f, err = io.open(filename, "w") + if not f then return nil, err end + + f:write(textutils.serialise{ + inventories = self.inventories, + bundles = self.bundles, + result_inventory = self.result_inventory + }) + + f:close() +end + +function main_view:load_from_file(filename) + local f, err = io.open(filename, "r") + if not f then return nil, err end + + local data = textutils.unserialise(f:read("a")) + + main_view:prepare(data.inventories, data.bundles, data.result_inventory) + + f:close() +end @@ -589,6 +612,10 @@ function main_view:save_bundle(name, items, bundles) self.left_store.bundles = derive_available_bundles(self.left_store.items, self.bundle_details) self:refresh_filtered_names(self.left_store, self.left_area) + if self.save_file then + self:save_to_file(self.save_file) + end + return true end