From eedc77086fe8e89ca51d6c98d852d15ff089d7cd Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sat, 8 Jan 2022 16:40:10 +0100 Subject: [PATCH] Some cleaning --- init.lua | 153 ++-------------------------------------------- src/callbacks.lua | 144 ++++++++++++++++++++++++++++++++++++++++++- src/gui.lua | 1 - 3 files changed, 147 insertions(+), 151 deletions(-) diff --git a/init.lua b/init.lua index 8dd0ea9..5071f12 100644 --- a/init.lua +++ b/init.lua @@ -12,6 +12,7 @@ print[[ local modpath = core.get_modpath"i3" local http = core.request_http_api() +local storage = core.get_mod_storage() local _loadfile = dofile(modpath .. "/src/operators.lua") local function lf(path) @@ -19,6 +20,8 @@ local function lf(path) end i3 = { + data = core.deserialize(storage:get_string"data") or {}, + settings = { debug_mode = false, max_favs = 6, @@ -93,155 +96,7 @@ i3.files.common() i3.files.api(http) i3.files.compress() i3.files.groups() -i3.files.callbacks() - -local storage = core.get_mod_storage() -local slz, dslz, copy = i3.get("slz", "dslz", "copy") -local set_fs = i3.set_fs - -i3.data = dslz(storage:get_string"data") or {} - -local init_bags = i3.files.bags() -local init_detached = i3.files.detached() -local fill_caches = i3.files.caches(http) -local init_hud = i3.files.hud() - -local function get_lang_code(info) - return info and info.lang_code -end - -local function get_formspec_version(info) - return info and info.formspec_version or 1 -end - -local function outdated(name) - core.show_formspec(name, "i3_outdated", - ("size[6.5,1.3]image[0,0;1,1;i3_book.png]label[1,0;%s]button_exit[2.6,0.8;1,1;;OK]"):format( - "Your Minetest client is outdated.\nGet the latest version on minetest.net to play the game.")) -end - -if rawget(_G, "armor") then - i3.modules.armor = true - armor:register_on_update(set_fs) -end - -if rawget(_G, "skins") then - i3.modules.skins = true -end - -if rawget(_G, "awards") then - i3.modules.awards = true - - core.register_on_craft(function(_, player) - set_fs(player) - end) - - core.register_on_dignode(function(_, _, player) - set_fs(player) - end) - - core.register_on_placenode(function(_, _, player) - set_fs(player) - end) - - core.register_on_chat_message(function(name) - local player = core.get_player_by_name(name) - set_fs(player) - end) -end - -local function disable_inventories() - if rawget(_G, "sfinv") then - function sfinv.set_player_inventory_formspec() return end - sfinv.enabled = false - end - - if rawget(_G, "unified_inventory") then - function unified_inventory.set_inventory_formspec() return end - end -end - -local function init_data(player, info) - local name = player:get_player_name() - i3.data[name] = i3.data[name] or {} - local data = i3.data[name] - - data.player_name = name - data.filter = "" - data.pagenum = 1 - data.items = i3.init_items - data.items_raw = i3.init_items - data.favs = {} - data.sort = "alphabetical" - data.show_setting = "home" - data.ignore_hotbar = false - data.auto_sorting = false - data.reverse_sorting = false - data.inv_compress = true - data.export_counts = {} - data.tab = 1 - data.itab = 1 - data.subcat = 1 - data.scrbar_inv = 0 - data.lang_code = get_lang_code(info) - data.fs_version = info.formspec_version - - local inv = player:get_inventory() - inv:set_size("main", i3.settings.inv_size) - - core.after(0, set_fs, player) -end - -local function save_data(player_name) - local _data = copy(i3.data) - - for name, v in pairs(_data) do - for dat in pairs(v) do - if not i3.saves[dat] then - _data[name][dat] = nil - - if player_name and i3.data[player_name] then - i3.data[player_name][dat] = nil -- To free up some memory - end - end - end - end - - storage:set_string("data", slz(_data)) -end - -core.register_on_mods_loaded(function() - fill_caches() - disable_inventories() -end) - -core.register_on_joinplayer(function(player) - local name = player:get_player_name() - local info = core.get_player_information and core.get_player_information(name) - - if not info or get_formspec_version(info) < i3.settings.min_fs_version then - return outdated(name) - end - - init_data(player, info) - init_bags(player) - init_detached(player) - init_hud(player) -end) - -core.register_on_leaveplayer(function(player) - local name = player:get_player_name() - save_data(name) -end) - -core.register_on_shutdown(save_data) - -local function routine() - save_data() - core.after(i3.settings.save_interval, routine) -end - -core.after(i3.settings.save_interval, routine) +i3.files.callbacks(http, storage) if i3.settings.progressive_mode then i3.files.progressive() diff --git a/src/callbacks.lua b/src/callbacks.lua index de4e632..2d4c6ce 100644 --- a/src/callbacks.lua +++ b/src/callbacks.lua @@ -1,10 +1,15 @@ +local http, storage = ... +local init_bags = i3.files.bags() +local fill_caches = i3.files.caches(http) +local init_detached = i3.files.detached() local _, get_inventory_fs = i3.files.gui() +local init_hud = i3.files.hud() local set_fs = i3.set_fs IMPORT("vec_eq", "vec_round") IMPORT("reg_items", "reg_aliases") IMPORT("sort", "copy", "insert", "remove", "indexof") -IMPORT("S", "min", "random", "translate", "ItemStack") +IMPORT("S", "slz", "min", "random", "translate", "ItemStack") IMPORT("fmt", "find", "match", "sub", "lower", "split", "toupper") IMPORT("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint", "play_sound", "spawn_item") IMPORT("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes", "get_detached_inv") @@ -523,3 +528,140 @@ core.register_on_player_inventory_action(function(player, _, _, info) set_fs(player) end end) + +if core.global_exists("armor") then + i3.modules.armor = true + armor:register_on_update(set_fs) +end + +if core.global_exists("skins") then + i3.modules.skins = true +end + +if core.global_exists("awards") then + i3.modules.awards = true + + core.register_on_craft(function(_, player) + set_fs(player) + end) + + core.register_on_dignode(function(_, _, player) + set_fs(player) + end) + + core.register_on_placenode(function(_, _, player) + set_fs(player) + end) + + core.register_on_chat_message(function(name) + local player = core.get_player_by_name(name) + set_fs(player) + end) +end + +local function disable_inventories() + if rawget(_G, "sfinv") then + function sfinv.set_player_inventory_formspec() return end + sfinv.enabled = false + end + + if rawget(_G, "unified_inventory") then + function unified_inventory.set_inventory_formspec() return end + end +end + +core.register_on_mods_loaded(function() + fill_caches() + disable_inventories() +end) + +local function get_lang_code(info) + return info and info.lang_code +end + +local function get_formspec_version(info) + return info and info.formspec_version or 1 +end + +local function outdated(name) + core.show_formspec(name, "i3_outdated", + ("size[6.5,1.3]image[0,0;1,1;i3_book.png]label[1,0;%s]button_exit[2.6,0.8;1,1;;OK]"):format( + "Your Minetest client is outdated.\nGet the latest version on minetest.net to play the game.")) +end + +local function init_data(player, info) + local name = player:get_player_name() + i3.data[name] = i3.data[name] or {} + local data = i3.data[name] + + data.player_name = name + data.filter = "" + data.pagenum = 1 + data.items = i3.init_items + data.items_raw = i3.init_items + data.favs = {} + data.sort = "alphabetical" + data.show_setting = "home" + data.ignore_hotbar = false + data.auto_sorting = false + data.reverse_sorting = false + data.inv_compress = true + data.export_counts = {} + data.tab = 1 + data.itab = 1 + data.subcat = 1 + data.scrbar_inv = 0 + data.lang_code = get_lang_code(info) + data.fs_version = info.formspec_version + + local inv = player:get_inventory() + inv:set_size("main", i3.settings.inv_size) + + core.after(0, set_fs, player) +end + +local function save_data(player_name) + local _data = copy(i3.data) + + for name, v in pairs(_data) do + for dat in pairs(v) do + if not i3.saves[dat] then + _data[name][dat] = nil + + if player_name and i3.data[player_name] then + i3.data[player_name][dat] = nil -- To free up some memory + end + end + end + end + + storage:set_string("data", slz(_data)) +end + +core.register_on_joinplayer(function(player) + local name = player:get_player_name() + local info = core.get_player_information and core.get_player_information(name) + + if not info or get_formspec_version(info) < i3.settings.min_fs_version then + return outdated(name) + end + + init_data(player, info) + init_bags(player) + init_detached(player) + init_hud(player) +end) + +core.register_on_leaveplayer(function(player) + local name = player:get_player_name() + save_data(name) +end) + +core.register_on_shutdown(save_data) + +local function routine() + save_data() + core.after(i3.settings.save_interval, routine) +end + +core.after(i3.settings.save_interval, routine) diff --git a/src/gui.lua b/src/gui.lua index 0880a61..d3bc061 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -1485,7 +1485,6 @@ local function make_fs(player, data) fs("bg9", 0, 0, data.inv_width, full_height, PNG.bg_full, 10) local tab = i3.tabs[data.tab] - if tab then tab.formspec(player, data, fs) end