From 8525633d4ca70e4332ce99d436bdb064c4d6c782 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sun, 19 Jun 2022 02:29:08 +0200 Subject: [PATCH] Improve groups caching --- src/caches.lua | 27 ++++++++++++++------------- src/common.lua | 26 +++++++++++--------------- src/gui.lua | 13 +++++++------ 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/caches.lua b/src/caches.lua index fd67726..f4cdec7 100644 --- a/src/caches.lua +++ b/src/caches.lua @@ -2,9 +2,9 @@ local replacements = {fuel = {}} local http = ... IMPORT("maxn", "copy", "insert", "sort", "match", "sub") -IMPORT("is_group", "extract_groups", "item_has_groups", "groups_to_items") -IMPORT("fmt", "reg_items", "reg_aliases", "reg_nodes", "is_cube", "get_cube", "ItemStack") IMPORT("true_str", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq") +IMPORT("fmt", "reg_items", "reg_aliases", "reg_nodes", "is_cube", "get_cube", "ItemStack") +IMPORT("is_group", "extract_groups", "item_has_groups", "groups_to_items", "get_group_stereotype") local function get_burntime(item) return core.get_craft_result{method = "fuel", items = {item}}.time @@ -22,12 +22,13 @@ local function cache_fuel(item) end end -local function cache_groups(groupname, groups) - i3.groups[groupname] = {} - i3.groups[groupname].groups = groups - i3.groups[groupname].items = groups_to_items(groups, true) +local function cache_groups(group, groups) + i3.groups[group] = {} + i3.groups[group].groups = groups + i3.groups[group].stereotype = get_group_stereotype(groups[1]) + i3.groups[group].items = groups_to_items(groups) - local items = i3.groups[groupname].items + local items = i3.groups[group].items if #items <= 1 then return end local c = 0 @@ -52,18 +53,18 @@ local function cache_groups(groupname, groups) sprite = sprite:gsub("WxH", px .. "x" .. px * c) - i3.groups[groupname].sprite = sprite - i3.groups[groupname].count = c + i3.groups[group].sprite = sprite + i3.groups[group].count = c end local function get_item_usages(item, recipe, added) if is_group(item) then - local groupname = item:sub(7) - local group_cache = i3.groups[groupname] + local group = item:sub(7) + local group_cache = i3.groups[group] local groups = group_cache and group_cache.groups or extract_groups(item) if not group_cache then - cache_groups(groupname, groups) + cache_groups(group, groups) end for name, def in pairs(reg_items) do @@ -220,7 +221,7 @@ core.register_craft = function(def) if is_group(output[1]) then groups = extract_groups(output[1]) - output = groups_to_items(groups, true) + output = groups_to_items(groups) end for i = 1, #output do diff --git a/src/common.lua b/src/common.lua index d95aa2f..322b99a 100644 --- a/src/common.lua +++ b/src/common.lua @@ -253,30 +253,25 @@ local function valid_item(def) def.description and def.description ~= "" end -local function groups_to_items(groups, get_all) - if not get_all and #groups == 1 then - local group = groups[1] - local stereotype = i3.group_stereotypes[group] - local def = reg_items[stereotype] +local function get_group_stereotype(group) + local stereotype = i3.group_stereotypes[group] + local def = reg_items[stereotype] - if valid_item(def) then - return stereotype - end + if valid_item(def) then + return stereotype end +end +local function groups_to_items(groups) local names = {} for name, def in pairs(reg_items) do if valid_item(def) and item_has_groups(def.groups, groups) then - if get_all then - insert(names, name) - else - return name - end + insert(names, name) end end - return get_all and names or "" + return names end local function is_cube(drawtype) @@ -413,7 +408,7 @@ local function craft_stack(player, data, craft_rcp) items = {} local groups = extract_groups(name) local groupname = name:sub(7) - local item_groups = i3.groups[groupname].items or groups_to_items(groups, true) + local item_groups = i3.groups[groupname].items or groups_to_items(groups) local remaining = count for _, item in ipairs(item_groups) do @@ -669,6 +664,7 @@ local _ = { extract_groups = extract_groups, item_has_groups = item_has_groups, groups_to_items = groups_to_items, + get_group_stereotype = get_group_stereotype, -- Compression compressible = compressible, diff --git a/src/gui.lua b/src/gui.lua index 7b8de2d..95b33f7 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -13,12 +13,12 @@ IMPORT("vec_new", "vec_sub", "vec_round") IMPORT("clr", "ESC", "msg", "check_privs") IMPORT("min", "max", "floor", "ceil", "round") IMPORT("reg_items", "reg_tools", "reg_entities") -IMPORT("get_bag_description", "get_detached_inv") +IMPORT("true_str", "is_fav", "is_num", "str_to_pos") IMPORT("S", "ES", "translate", "ItemStack", "toupper") -IMPORT("groups_to_items", "compression_active", "compressible") -IMPORT("true_str", "is_fav", "is_num", "get_group", "str_to_pos") +IMPORT("get_sorting_idx", "compression_active", "compressible") +IMPORT("get_bag_description", "get_detached_inv", "get_recipes") IMPORT("maxn", "sort", "concat", "copy", "insert", "remove", "unpack") -IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups", "get_recipes") +IMPORT("get_group_stereotype", "extract_groups", "is_group", "item_has_groups", "get_group") local function fmt(elem, ...) if not fs_elements[elem] then @@ -997,7 +997,8 @@ local function get_grid_fs(fs, data, rcp, is_recipe) if is_group(name) then groups = group_cache and group_cache.groups or extract_groups(name) - name = group_cache and group_cache.items[1] or groups_to_items(groups) + name = group_cache and group_cache.stereotype or + get_group_stereotype(groups[1]) or group_cache.items[1] end local label = groups and "\nG" or "" @@ -1039,7 +1040,7 @@ local function get_grid_fs(fs, data, rcp, is_recipe) fs("label", X + 0.45, Y + 0.18, label) if _count > 1 then - fs("label", X + 0.8, Y + 0.9, tostring(_count)) + fs("label", X + 0.8, Y + 0.9, _count) end else fs("item_image_button", X, Y, btn_size, btn_size, fmt("%s %u", name, _count), btn_name, label)