From 309ff6b8d463b3555780ee45d5ce2358fc1a11a5 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sat, 13 Jun 2020 08:04:54 -0400 Subject: [PATCH] Move storage code out of init.lua --- init.lua | 29 +---------------------------- storage.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 storage.lua diff --git a/init.lua b/init.lua index 4ad9a39..2470b57 100644 --- a/init.lua +++ b/init.lua @@ -1,35 +1,8 @@ -- The path to this mod, for including files local modpath = minetest.get_modpath("cartographer"); - --- Storage and saving -local mod_storage = minetest.get_mod_storage(); -local map_data = { - generated = minetest.deserialize(mod_storage:get_string("map")) or {}, - - maps = minetest.deserialize(mod_storage:get_string("maps")) or {}, - next_map_id = mod_storage:get_int("next_map_id"), -}; - -if map_data.next_map_id == 0 then - map_data.next_map_id = 1; -end - -local function save() - mod_storage:set_string("maps", minetest.serialize(map_data.maps)); - mod_storage:set_int("next_map_id", map_data.next_map_id); - mod_storage:set_string("map", minetest.serialize(map_data.generated)); -end -minetest.register_on_shutdown(save); -minetest.register_on_leaveplayer(save); - -local function periodic_save() - save(); - minetest.after(60, periodic_save); -end -minetest.after(60, periodic_save); - -- Includes +local map_data = loadfile(modpath .. "/storage.lua") (); local chunk = loadfile(modpath .. "/chunk_api.lua") (); local skin = loadfile(modpath .. "/skin_api.lua") (); local gui = loadfile(modpath .. "/formspec.lua") (); diff --git a/storage.lua b/storage.lua new file mode 100644 index 0000000..7f498b6 --- /dev/null +++ b/storage.lua @@ -0,0 +1,28 @@ +-- Storage and saving +local mod_storage = minetest.get_mod_storage(); +local map_data = { + generated = minetest.deserialize(mod_storage:get_string("map")) or {}, + + maps = minetest.deserialize(mod_storage:get_string("maps")) or {}, + next_map_id = mod_storage:get_int("next_map_id"), +}; + +if map_data.next_map_id == 0 then + map_data.next_map_id = 1; +end + +local function save() + mod_storage:set_string("maps", minetest.serialize(map_data.maps)); + mod_storage:set_int("next_map_id", map_data.next_map_id); + mod_storage:set_string("map", minetest.serialize(map_data.generated)); +end +minetest.register_on_shutdown(save); +minetest.register_on_leaveplayer(save); + +local function periodic_save() + save(); + minetest.after(60, periodic_save); +end +minetest.after(60, periodic_save); + +return map_data;