From 3a83e015ae9fe72b4bea6a2488cfa838b9b4f223 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 25 Oct 2021 07:09:21 +0200 Subject: [PATCH] Small cleaning --- etc/api.lua | 12 ++++----- etc/bags.lua | 2 +- etc/common.lua | 16 +++++++++++- etc/compress.lua | 2 +- etc/groups.lua | 2 +- etc/gui.lua | 18 ++++++------- etc/inventory.lua | 20 +++++++------- etc/progressive.lua | 4 +-- etc/recipes.lua | 47 ++++++++++++++++++++++++++++----- init.lua | 63 +++------------------------------------------ 10 files changed, 89 insertions(+), 97 deletions(-) diff --git a/etc/api.lua b/etc/api.lua index aca7662..54851f8 100644 --- a/etc/api.lua +++ b/etc/api.lua @@ -1,10 +1,10 @@ local make_fs = i3.files.gui() -local gmatch, match, split = i3.need("gmatch", "match", "split") -local S, err, fmt, reg_items = i3.need("S", "err", "fmt", "reg_items") -local sort, concat, copy, insert, remove = i3.need("sort", "concat", "copy", "insert", "remove") +local gmatch, match, split = i3.get("gmatch", "match", "split") +local S, err, fmt, reg_items = i3.get("S", "err", "fmt", "reg_items") +local sort, concat, copy, insert, remove = i3.get("sort", "concat", "copy", "insert", "remove") local true_str, true_table, is_str, is_func, is_table, clean_name = - i3.need("true_str", "true_table", "is_str", "is_func", "is_table", "clean_name") + i3.get("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 @@ -170,8 +170,6 @@ function i3.set_fs(player, _fs) player:set_inventory_formspec(fs) end -local set_fs = i3.set_fs - function i3.new_tab(def) if not true_table(def) then return err "i3.new_tab: tab definition missing" @@ -308,4 +306,4 @@ function i3.compress(item, def) end end -return set_fs, i3.set_tab +return i3.set_fs, i3.set_tab diff --git a/etc/bags.lua b/etc/bags.lua index e71f949..c92aacc 100644 --- a/etc/bags.lua +++ b/etc/bags.lua @@ -1,5 +1,5 @@ local set_fs = i3.files.api() -local S, fmt, msg, spawn_item = i3.need("S", "fmt", "msg", "spawn_item") +local S, fmt, msg, spawn_item = i3.get("S", "fmt", "msg", "spawn_item") local function init_backpack(player) local name = player:get_player_name() diff --git a/etc/common.lua b/etc/common.lua index a154416..52d69d4 100644 --- a/etc/common.lua +++ b/etc/common.lua @@ -351,7 +351,9 @@ local function spawn_item(player, stack) core.add_item(look_at, stack) end -return { +------------------------------------------------------------------------------- + +local registry = { -- Groups is_group = is_group, extract_groups = extract_groups, @@ -382,6 +384,8 @@ return { -- Core functions clr = core.colorize, + slz = core.serialize, + dslz = core.deserialize, ESC = core.formspec_escape, check_privs = core.check_player_privs, @@ -437,3 +441,13 @@ return { vec_eq = vector.equals, vec_mul = vector.multiply, } + +function i3.get(...) + local t = {} + + for i, var in ipairs{...} do + t[i] = registry[var] + end + + return unpack(t) +end diff --git a/etc/compress.lua b/etc/compress.lua index b7cd6c8..202c122 100644 --- a/etc/compress.lua +++ b/etc/compress.lua @@ -1,4 +1,4 @@ -local fmt, copy, insert = i3.need("fmt", "copy", "insert") +local fmt, copy, insert = i3.get("fmt", "copy", "insert") local wood_types = { "acacia_wood", "aspen_wood", "junglewood", "pine_wood", diff --git a/etc/groups.lua b/etc/groups.lua index 32d7297..817ac6e 100644 --- a/etc/groups.lua +++ b/etc/groups.lua @@ -1,4 +1,4 @@ -local S = i3.need("S") +local S = i3.get("S") local group_stereotypes = { dye = "dye:white", diff --git a/etc/gui.lua b/etc/gui.lua index 2421ee2..29b6529 100644 --- a/etc/gui.lua +++ b/etc/gui.lua @@ -3,17 +3,17 @@ local damage_enabled = core.settings:get_bool "enable_damage" local model_aliases = i3.files.model_alias() local PNG, styles, fs_elements = i3.files.styles() -local S, ES, translate = i3.need("S", "ES", "translate") -local clr, ESC, check_privs = i3.need("clr", "ESC", "check_privs") -local min, max, floor, ceil, round = i3.need("min", "max", "floor", "ceil", "round") -local sprintf, find, match, sub, upper = i3.need("fmt", "find", "match", "sub", "upper") -local reg_items, reg_tools, reg_entities = i3.need("reg_items", "reg_tools", "reg_entities") -local maxn, sort, concat, copy, insert, remove = i3.need("maxn", "sort", "concat", "copy", "insert", "remove") +local S, ES, translate = i3.get("S", "ES", "translate") +local clr, ESC, check_privs = i3.get("clr", "ESC", "check_privs") +local min, max, floor, ceil, round = i3.get("min", "max", "floor", "ceil", "round") +local sprintf, find, match, sub, upper = i3.get("fmt", "find", "match", "sub", "upper") +local reg_items, reg_tools, reg_entities = i3.get("reg_items", "reg_tools", "reg_entities") +local maxn, sort, concat, copy, insert, remove = i3.get("maxn", "sort", "concat", "copy", "insert", "remove") -local true_str, is_fav, is_num = i3.need("true_str", "is_fav", "is_num") -local is_group, extract_groups, item_has_groups = i3.need("is_group", "extract_groups", "item_has_groups") +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 groups_to_items, compression_active, compressible = - i3.need("groups_to_items", "compression_active", "compressible") + i3.get("groups_to_items", "compression_active", "compressible") local function fmt(elem, ...) if not fs_elements[elem] then diff --git a/etc/inventory.lua b/etc/inventory.lua index 5cd36d5..6148d9d 100644 --- a/etc/inventory.lua +++ b/etc/inventory.lua @@ -1,20 +1,20 @@ local set_fs, set_tab = i3.files.api() local _, get_inventory_fs = i3.files.gui() -local S, clr = i3.need("S", "clr") -local min, ceil, random = i3.need("min", "ceil", "random") -local reg_items, reg_aliases = i3.need("reg_items", "reg_aliases") -local fmt, find, match, sub, lower = i3.need("fmt", "find", "match", "sub", "lower") -local vec_new, vec_mul, vec_eq, vec_round = i3.need("vec_new", "vec_mul", "vec_eq", "vec_round") -local sort, copy, insert, remove, indexof = i3.need("sort", "copy", "insert", "remove", "indexof") +local S, clr = i3.get("S", "clr") +local min, ceil, random = i3.get("min", "ceil", "random") +local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases") +local fmt, find, match, sub, lower = i3.get("fmt", "find", "match", "sub", "lower") +local vec_new, vec_mul, vec_eq, vec_round = i3.get("vec_new", "vec_mul", "vec_eq", "vec_round") +local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof") -local msg, is_str, is_fav = i3.need("msg", "is_str", "is_fav") +local msg, is_str, is_fav = i3.get("msg", "is_str", "is_fav") local is_group, extract_groups, groups_to_items = - i3.need("is_group", "extract_groups", "groups_to_items") + i3.get("is_group", "extract_groups", "groups_to_items") local search, sort_by_category, apply_recipe_filters = - i3.need("search", "sort_by_category", "apply_recipe_filters") + i3.get("search", "sort_by_category", "apply_recipe_filters") local show_item, spawn_item, clean_name, compressible, check_privs = - i3.need("show_item", "spawn_item", "clean_name", "compressible", "check_privs") + i3.get("show_item", "spawn_item", "clean_name", "compressible", "check_privs") local old_is_creative_enabled = core.is_creative_enabled diff --git a/etc/progressive.lua b/etc/progressive.lua index 4076b31..1d68f42 100644 --- a/etc/progressive.lua +++ b/etc/progressive.lua @@ -1,9 +1,9 @@ local set_fs = i3.files.api() local singleplayer = core.is_singleplayer() -local fmt, search, table_merge, array_diff = i3.need("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.need("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters") + i3.get("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters") local POLL_FREQ = 0.25 local HUD_TIMER_MAX = 1.5 diff --git a/etc/recipes.lua b/etc/recipes.lua index 0780952..75a7af7 100644 --- a/etc/recipes.lua +++ b/etc/recipes.lua @@ -1,13 +1,12 @@ local replacements = {fuel = {}} -local fmt, match = i3.need("fmt", "match") -local reg_items, reg_aliases = i3.need("reg_items", "reg_aliases") -local maxn, copy, insert = i3.need("maxn", "copy", "insert") +local fmt, reg_items, reg_aliases = i3.get("fmt", "reg_items", "reg_aliases") +local maxn, copy, insert, sort, match = i3.get("maxn", "copy", "insert", "sort", "match") local is_group, extract_groups, item_has_groups, groups_to_items = - i3.need("is_group", "extract_groups", "item_has_groups", "groups_to_items") + i3.get("is_group", "extract_groups", "item_has_groups", "groups_to_items") local true_str, is_table, show_item, table_merge, table_replace, table_eq = - i3.need("true_str", "is_table", "show_item", "table_merge", "table_replace", "table_eq") + i3.get("true_str", "is_table", "show_item", "table_merge", "table_replace", "table_eq") local function get_burntime(item) return core.get_craft_result{method = "fuel", items = {item}}.time @@ -250,4 +249,40 @@ local function resolve_aliases(hash) end end -return cache_drops, cache_fuel, cache_recipes, cache_usages, resolve_aliases +local function init_recipes() + local _select, _preselect = {}, {} + + for name, def in pairs(reg_items) do + if name ~= "" and show_item(def) then + cache_drops(name, def.drop) + cache_fuel(name) + cache_recipes(name) + + _preselect[name] = true + end + end + + for name in pairs(_preselect) do + cache_usages(name) + + i3.init_items[#i3.init_items + 1] = name + _select[name] = true + end + + resolve_aliases(_select) + sort(i3.init_items) + + if i3.http and type(i3.export_url) == "string" then + local post_data = { + recipes = i3.recipes_cache, + usages = i3.usages_cache, + } + + i3.http.fetch_async { + url = i3.export_url, + post_data = core.write_json(post_data), + } + end +end + +return init_recipes diff --git a/init.lua b/init.lua index e2e1326..28b2966 100644 --- a/init.lua +++ b/init.lua @@ -65,25 +65,9 @@ i3 = { item_compression = core.settings:get_bool("i3_item_compression", true), } -local common = i3.files.common() - -function i3.need(...) - local t = {} - - for _, var in ipairs {...} do - for name, func in pairs(common) do - if var == name then - t[#t + 1] = func - break - end - end - end - - return unpack(t) -end - +i3.files.common() local storage = core.get_mod_storage() -local slz, dslz = core.serialize, core.deserialize +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() @@ -91,11 +75,8 @@ 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 cache_drops, cache_fuel, cache_recipes, cache_usages, resolve_aliases = i3.files.recipes() - -local fmt, sort, copy = i3.need("fmt", "sort", "copy") -local show_item, reg_items = i3.need("show_item", "reg_items") local function get_lang_code(info) return info and info.lang_code @@ -142,42 +123,6 @@ if rawget(_G, "awards") then end) end -local function get_init_items() - local _select, _preselect = {}, {} - - for name, def in pairs(reg_items) do - if name ~= "" and show_item(def) then - cache_drops(name, def.drop) - cache_fuel(name) - cache_recipes(name) - - _preselect[name] = true - end - end - - for name in pairs(_preselect) do - cache_usages(name) - - i3.init_items[#i3.init_items + 1] = name - _select[name] = true - end - - resolve_aliases(_select) - sort(i3.init_items) - - if i3.http and type(i3.export_url) == "string" then - local post_data = { - recipes = i3.recipes_cache, - usages = i3.usages_cache, - } - - i3.http.fetch_async { - url = i3.export_url, - post_data = core.write_json(post_data), - } - end -end - local function disable_inventories() if rawget(_G, "sfinv") then function sfinv.set_player_inventory_formspec() return end @@ -257,7 +202,7 @@ local function save_data(player_name) end core.register_on_mods_loaded(function() - get_init_items() + init_recipes() disable_inventories() end)