From 980adeb71fb362b1456a97f13a520d38b73e1ffb Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 25 Oct 2021 20:31:20 +0200 Subject: [PATCH] Some cleaning --- etc/api.lua | 40 +++++++++++----------------------------- etc/bags.lua | 5 ++--- etc/compress.lua | 2 +- etc/groups.lua | 6 ++---- etc/gui.lua | 3 ++- etc/inventory.lua | 21 ++++++++++----------- etc/progressive.lua | 6 +++--- init.lua | 21 +++++++++++---------- 8 files changed, 42 insertions(+), 62 deletions(-) diff --git a/etc/api.lua b/etc/api.lua index 54851f8..9749cf2 100644 --- a/etc/api.lua +++ b/etc/api.lua @@ -9,9 +9,9 @@ local true_str, true_table, is_str, is_func, is_table, clean_name = function i3.register_craft_type(name, def) if not true_str(name) then return err "i3.register_craft_type: name missing" - end - - if not is_str(def.description) then + elseif not true_table(def) then + return err "i3.register_craft_type: definition missing" + elseif not is_str(def.description) then def.description = "" end @@ -173,17 +173,11 @@ end function i3.new_tab(def) if not true_table(def) then return err "i3.new_tab: tab definition missing" - end - - if not true_str(def.name) then + elseif not true_str(def.name) then return err "i3.new_tab: tab name missing" - end - - if not true_str(def.description) then + elseif not true_str(def.description) then return err "i3.new_tab: description missing" - end - - if #i3.tabs == 6 then + elseif #i3.tabs == 6 then return err(fmt("i3.new_tab: cannot add '%s' tab. Limit reached (6).", def.name)) end @@ -235,13 +229,9 @@ end function i3.override_tab(tabname, newdef) if not true_table(newdef) then return err "i3.override_tab: tab definition missing" - end - - if not true_str(newdef.name) then + elseif not true_str(newdef.name) then return err "i3.override_tab: tab name missing" - end - - if not true_str(newdef.description) then + elseif not true_str(newdef.description) then return err "i3.override_tab: description missing" end @@ -279,17 +269,11 @@ end) function i3.compress(item, def) if not true_str(item) then return err "i3.compress: item name missing" - end - - if not true_table(def) then + elseif not true_table(def) then return err "i3.compress: replace definition missing" - end - - if not true_str(def.replace) then + elseif not true_str(def.replace) then return err "i3.compress: replace string missing" - end - - if not is_table(def.by) then + elseif not is_table(def.by) then return err "i3.compress: replace substrings missing" end @@ -305,5 +289,3 @@ function i3.compress(item, def) i3.compressed[it] = true end end - -return i3.set_fs, i3.set_tab diff --git a/etc/bags.lua b/etc/bags.lua index c92aacc..126b2e2 100644 --- a/etc/bags.lua +++ b/etc/bags.lua @@ -1,4 +1,3 @@ -local set_fs = i3.files.api() local S, fmt, msg, spawn_item = i3.get("S", "fmt", "msg", "spawn_item") local function init_backpack(player) @@ -28,7 +27,7 @@ local function init_backpack(player) data.bag_size = minetest.get_item_group(stackname, "bag") inv:set_size("main", i3.BAG_SIZES[data.bag_size]) - set_fs(player) + i3.set_fs(player) end, on_take = function() @@ -44,7 +43,7 @@ local function init_backpack(player) data.bag_size = nil inv:set_size("main", i3.INV_SIZE) - set_fs(player) + i3.set_fs(player) end, }) diff --git a/etc/compress.lua b/etc/compress.lua index 202c122..4b09706 100644 --- a/etc/compress.lua +++ b/etc/compress.lua @@ -316,4 +316,4 @@ for _, v2 in ipairs(v) do end end -return compressed, _compressed +i3.compress_groups, i3.compressed = compressed, _compressed diff --git a/etc/groups.lua b/etc/groups.lua index 817ac6e..88a9c7f 100644 --- a/etc/groups.lua +++ b/etc/groups.lua @@ -1,6 +1,6 @@ local S = i3.get("S") -local group_stereotypes = { +i3.group_stereotypes = { dye = "dye:white", wool = "wool:white", wood = "default:wood", @@ -17,7 +17,7 @@ local group_stereotypes = { mesecon_conductor_craftable = "mesecons:wire_00000000_off", } -local group_names = { +i3.group_names = { dye = S"Any dye", coal = S"Any coal", sand = S"Any sand", @@ -58,5 +58,3 @@ local group_names = { ["color_dark_grey,dye"] = S"Any dark grey dye", ["color_dark_green,dye"] = S"Any dark green dye", } - -return group_stereotypes, group_names diff --git a/etc/gui.lua b/etc/gui.lua index 29b6529..0bed973 100644 --- a/etc/gui.lua +++ b/etc/gui.lua @@ -11,7 +11,8 @@ local reg_items, reg_tools, reg_entities = i3.get("reg_items", "reg_tools", "reg local maxn, sort, concat, copy, insert, remove = i3.get("maxn", "sort", "concat", "copy", "insert", "remove") local true_str, is_fav, is_num = i3.get("true_str", "is_fav", "is_num") -local is_group, extract_groups, item_has_groups = i3.get("is_group", "extract_groups", "item_has_groups") +local is_group, extract_groups, item_has_groups = + i3.get("is_group", "extract_groups", "item_has_groups") local groups_to_items, compression_active, compressible = i3.get("groups_to_items", "compression_active", "compressible") diff --git a/etc/inventory.lua b/etc/inventory.lua index 6148d9d..c9652a8 100644 --- a/etc/inventory.lua +++ b/etc/inventory.lua @@ -1,4 +1,3 @@ -local set_fs, set_tab = i3.files.api() local _, get_inventory_fs = i3.files.gui() local S, clr = i3.get("S", "clr") @@ -295,7 +294,7 @@ i3.new_tab { data.scrbar_inv = data.scrbar_inv + 1000 end - return set_fs(player) + return i3.set_fs(player) end, } @@ -450,7 +449,7 @@ local function rcp_fields(player, data, fields) elseif fields.key_enter_field == "filter" or fields.search then if fields.filter == "" then reset_data(data) - return set_fs(player) + return i3.set_fs(player) end local str = lower(fields.filter) @@ -546,7 +545,7 @@ core.register_on_player_receive_fields(function(player, formname, fields) for f in pairs(fields) do if sub(f, 1, 4) == "tab_" then local tabname = sub(f, 5) - set_tab(player, tabname) + i3.set_tab(player, tabname) break elseif sub(f, 1, 5) == "itab_" then data.pagenum = 1 @@ -563,7 +562,7 @@ core.register_on_player_receive_fields(function(player, formname, fields) return true, tab.fields(player, data, fields) end - return true, set_fs(player) + return true, i3.set_fs(player) end) core.register_on_player_hpchange(function(player, hpchange) @@ -574,7 +573,7 @@ core.register_on_player_hpchange(function(player, hpchange) local hp_max = player:get_properties().hp_max data.hp = min(hp_max, player:get_hp() + hpchange) - set_fs(player) + i3.set_fs(player) end) core.register_on_dieplayer(function(player) @@ -591,12 +590,12 @@ core.register_on_dieplayer(function(player) inv:set_size("main", i3.INV_SIZE) end - set_fs(player) + i3.set_fs(player) end) core.register_on_chatcommand(function(name) local player = core.get_player_by_name(name) - core.after(0, set_fs, player) + core.after(0, i3.set_fs, player) end) core.register_on_priv_grant(function(name, _, priv) @@ -606,7 +605,7 @@ core.register_on_priv_grant(function(name, _, priv) data.favs = {} local player = core.get_player_by_name(name) - core.after(0, set_fs, player) + core.after(0, i3.set_fs, player) end end) @@ -617,7 +616,7 @@ core.register_on_player_inventory_action(function(player, _, _, info) ((info.from_list == "main" and info.to_list == "craft") or (info.from_list == "craft" and info.to_list == "main") or (info.from_list == "craftresult" and info.to_list == "main")) then - set_fs(player) + i3.set_fs(player) end end) @@ -631,7 +630,7 @@ local trash = core.create_detached_inventory("i3_trash", { local name = player:get_player_name() if not core.is_creative_enabled(name) then - set_fs(player) + i3.set_fs(player) end end, }) diff --git a/etc/progressive.lua b/etc/progressive.lua index 1d68f42..74ffad8 100644 --- a/etc/progressive.lua +++ b/etc/progressive.lua @@ -1,7 +1,7 @@ -local set_fs = i3.files.api() local singleplayer = core.is_singleplayer() -local fmt, search, table_merge, array_diff = i3.get("fmt", "search", "table_merge", "array_diff") +local fmt, search, table_merge, array_diff = + i3.get("fmt", "search", "table_merge", "array_diff") local is_group, extract_groups, item_has_groups, apply_recipe_filters = i3.get("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters") @@ -225,7 +225,7 @@ local function poll_new_items() data.current_itab = 1 search(data) - set_fs(player) + i3.set_fs(player) end end diff --git a/init.lua b/init.lua index 28b2966..99ace14 100644 --- a/init.lua +++ b/init.lua @@ -66,17 +66,18 @@ i3 = { } i3.files.common() +i3.files.api() +i3.files.compress() +i3.files.groups() +i3.files.inventory() + local storage = core.get_mod_storage() local fmt, copy, slz, dslz = i3.get("fmt", "copy", "slz", "dslz") i3.data = dslz(storage:get_string "data") or {} -i3.compress_groups, i3.compressed = i3.files.compress() -i3.group_stereotypes, i3.group_names = i3.files.groups() -local set_fs = i3.files.api() local init_backpack = i3.files.bags() local init_recipes = i3.files.recipes() -i3.files.inventory() local function get_lang_code(info) return info and info.lang_code @@ -95,7 +96,7 @@ end if rawget(_G, "armor") then i3.modules.armor = true - armor:register_on_update(set_fs) + armor:register_on_update(i3.set_fs) end if rawget(_G, "skins") then @@ -106,20 +107,20 @@ if rawget(_G, "awards") then i3.modules.awards = true core.register_on_craft(function(_, player) - set_fs(player) + i3.set_fs(player) end) core.register_on_dignode(function(_, _, player) - set_fs(player) + i3.set_fs(player) end) core.register_on_placenode(function(_, _, player) - set_fs(player) + i3.set_fs(player) end) core.register_on_chat_message(function(name) local player = core.get_player_by_name(name) - set_fs(player) + i3.set_fs(player) end) end @@ -152,7 +153,7 @@ local function init_data(player, info) data.lang_code = get_lang_code(info) data.fs_version = info.formspec_version - core.after(0, set_fs, player) + core.after(0, i3.set_fs, player) end local function init_waypoints(player)