From 8f49914c9ff1244da19ca577f31c1fdb934ecc13 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 15 Nov 2021 23:50:48 +0100 Subject: [PATCH] Minor optimizations --- init.lua | 13 ++++++------ src/bags.lua | 8 +++++--- src/callbacks.lua | 18 ++++++++-------- src/common.lua | 49 ++++++++++++++++++++++++++++++++------------ src/detached_inv.lua | 3 ++- src/gui.lua | 49 ++++++++++++++++++++++---------------------- src/progressive.lua | 3 ++- src/recipes.lua | 5 +++-- 8 files changed, 90 insertions(+), 58 deletions(-) diff --git a/init.lua b/init.lua index d317dc5..3130485 100644 --- a/init.lua +++ b/init.lua @@ -73,6 +73,7 @@ i3.files.callbacks() local storage = core.get_mod_storage() local slz, dslz, str_to_pos, add_hud_waypoint = i3.get("slz", "dslz", "str_to_pos", "add_hud_waypoint") +local set_fs = i3.set_fs i3.data = dslz(storage:get_string "data") or {} @@ -97,7 +98,7 @@ end if rawget(_G, "armor") then i3.modules.armor = true - armor:register_on_update(i3.set_fs) + armor:register_on_update(set_fs) end if rawget(_G, "skins") then @@ -108,20 +109,20 @@ if rawget(_G, "awards") then i3.modules.awards = true core.register_on_craft(function(_, player) - i3.set_fs(player) + set_fs(player) end) core.register_on_dignode(function(_, _, player) - i3.set_fs(player) + set_fs(player) end) core.register_on_placenode(function(_, _, player) - i3.set_fs(player) + set_fs(player) end) core.register_on_chat_message(function(name) local player = core.get_player_by_name(name) - i3.set_fs(player) + set_fs(player) end) end @@ -164,7 +165,7 @@ local function init_data(player, info) local inv = player:get_inventory() inv:set_size("main", i3.INV_SIZE) - core.after(0, i3.set_fs, player) + core.after(0, set_fs, player) end local function init_waypoints(player) diff --git a/src/bags.lua b/src/bags.lua index e819e9e..a78fc08 100644 --- a/src/bags.lua +++ b/src/bags.lua @@ -1,3 +1,5 @@ +local set_fs = i3.set_fs +local ItemStack = ItemStack local S, ES, fmt, msg, slz, dslz = i3.get("S", "ES", "fmt", "msg", "slz", "dslz") local play_sound, create_inventory = i3.get("play_sound", "create_inventory") @@ -82,7 +84,7 @@ local function init_bags(player) inv:set_list("main", get_content(content)) end - i3.set_fs(player) + set_fs(player) end, on_take = function() @@ -92,7 +94,7 @@ local function init_bags(player) local content = get_content_inv(name) content:set_list("main", {}) - i3.set_fs(player) + set_fs(player) end, }, name) @@ -141,7 +143,7 @@ local function init_bags(player) bag:set_stack("main", 1, bagstack) data.bag_item = safe_format(bagstack) - i3.set_fs(player) + set_fs(player) end local bag_content = create_inventory(fmt("i3_bag_content_%s", name), { diff --git a/src/callbacks.lua b/src/callbacks.lua index 9256bc0..7710776 100644 --- a/src/callbacks.lua +++ b/src/callbacks.lua @@ -1,5 +1,7 @@ local _, get_inventory_fs = i3.files.gui() +local set_fs = i3.set_fs +local ItemStack = ItemStack local S, clr = i3.get("S", "clr") local min, random = i3.get("min", "random") local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases") @@ -201,7 +203,7 @@ i3.new_tab("inventory", { data.scrbar_inv = data.scrbar_inv + 1000 end - return i3.set_fs(player) + return set_fs(player) end, }) @@ -308,7 +310,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 i3.set_fs(player) + return set_fs(player) end local str = lower(fields.filter) @@ -430,7 +432,7 @@ core.register_on_player_receive_fields(function(player, formname, fields) return true, tab.fields(player, data, fields) end - return true, i3.set_fs(player) + return true, set_fs(player) end) core.register_on_player_hpchange(function(player, hpchange) @@ -441,7 +443,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) - i3.set_fs(player) + set_fs(player) end) core.register_on_dieplayer(function(player) @@ -449,12 +451,12 @@ core.register_on_dieplayer(function(player) local data = i3.data[name] if not data then return end - i3.set_fs(player) + set_fs(player) end) core.register_on_chatcommand(function(name) local player = core.get_player_by_name(name) - core.after(0, i3.set_fs, player) + core.after(0, set_fs, player) end) core.register_on_priv_grant(function(name, _, priv) @@ -464,7 +466,7 @@ core.register_on_priv_grant(function(name, _, priv) data.favs = {} local player = core.get_player_by_name(name) - core.after(0, i3.set_fs, player) + core.after(0, set_fs, player) end end) @@ -475,6 +477,6 @@ 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 - i3.set_fs(player) + set_fs(player) end end) diff --git a/src/common.lua b/src/common.lua index 5ea32f0..d2697e3 100644 --- a/src/common.lua +++ b/src/common.lua @@ -1,3 +1,7 @@ +local ItemStack = ItemStack +local loadstring = loadstring +local sort, concat, insert = table.sort, table.concat, table.insert +local min, floor, ceil = math.min, math.floor, math.ceil local fmt, find, match, gmatch, sub, split, lower = string.format, string.find, string.match, string.gmatch, string.sub, string.split, string.lower @@ -54,7 +58,7 @@ end local function round(num, decimal) local mul = 10 ^ decimal - return math.floor(num * mul + 0.5) / mul + return floor(num * mul + 0.5) / mul end local function search(data) @@ -243,7 +247,7 @@ local function groups_to_items(groups, get_all) for name, def in pairs(core.registered_items) do if show_item(def) and item_has_groups(def.groups, groups) then if get_all then - names[#names + 1] = name + insert(names, name) else return name end @@ -304,7 +308,7 @@ local function sort_by_category(data) end if to_add then - new[#new + 1] = item + insert(new, item) end end @@ -369,7 +373,7 @@ local function craft_stack(player, data, craft_rcp) for _, item in ipairs(item_groups) do for _name, _count in pairs(data.export_counts[rcp_usg].inv) do if item == _name and remaining > 0 then - local c = math.min(remaining, _count) + local c = min(remaining, _count) items[item] = c remaining = remaining - c end @@ -385,11 +389,11 @@ local function craft_stack(player, data, craft_rcp) end local count = stackcount * scrbar_val - local iter = math.ceil(count / stackmax) + local iter = ceil(count / stackmax) local leftover = count for _ = 1, iter do - local c = math.min(stackmax, leftover) + local c = min(stackmax, leftover) local stack = ItemStack(fmt("%s %s", stackname, c)) get_stack(player, stack) leftover = leftover - stackmax @@ -423,7 +427,7 @@ local function get_sorting_idx(name) end local function sorter(inv, reverse, mode) - table.sort(inv, function(a, b) + sort(inv, function(a, b) if mode == 1 then a, b = a:get_name(), b:get_name() else @@ -449,9 +453,9 @@ local function pre_sorting(list, start_i) if not empty then if next(meta.fields) or wear then - special[#special + 1] = stack + insert(special, stack) else - new_inv[#new_inv + 1] = stack + insert(new_inv, stack) end end end @@ -474,7 +478,7 @@ local function compress_items(list, start_i) if not empty then if next(meta.fields) or wear or count >= stackmax then - special[#special + 1] = stack + insert(special, stack) else hash[name] = hash[name] or 0 hash[name] = hash[name] + count @@ -484,11 +488,11 @@ local function compress_items(list, start_i) for name, count in pairs(hash) do local stackmax = ItemStack(name):get_stack_max() - local iter = math.ceil(count / stackmax) + local iter = ceil(count / stackmax) local leftover = count for _ = 1, iter do - new_inv[#new_inv + 1] = ItemStack(fmt("%s %u", name, math.min(stackmax, leftover))) + insert(new_inv, ItemStack(fmt("%s %u", name, min(stackmax, leftover)))) leftover = leftover - stackmax end end @@ -555,6 +559,24 @@ local function add_hud_waypoint(player, name, pos, color) } end +local function createunpack(n) + local ret = {"local t = ... return "} + + for k = 2, n do + ret[2 + (k - 2) * 4] = "t[" + ret[3 + (k - 2) * 4] = k - 1 + ret[4 + (k - 2) * 4] = "]" + + if k ~= n then + ret[5 + (k - 2) * 4] = "," + end + end + + return loadstring(concat(ret)) +end + +local newunpack = createunpack(33) + ------------------------------------------------------------------------------- local _ = { @@ -640,6 +662,7 @@ local _ = { insert = table.insert, remove = table.remove, indexof = table.indexof, + unpack = newunpack, is_table = is_table, table_merge = table_merge, table_replace = table_replace, @@ -668,5 +691,5 @@ function i3.get(...) t[i] = _[var] end - return unpack(t) + return newunpack(t) end diff --git a/src/detached_inv.lua b/src/detached_inv.lua index 9b4ceb2..24879bb 100644 --- a/src/detached_inv.lua +++ b/src/detached_inv.lua @@ -1,3 +1,4 @@ +local set_fs = i3.set_fs local fmt, play_sound, create_inventory = i3.get("fmt", "play_sound", "create_inventory") local trash = create_inventory("i3_trash", { @@ -12,7 +13,7 @@ local trash = create_inventory("i3_trash", { play_sound(name, "i3_trash", 1.0) if not core.is_creative_enabled(name) then - i3.set_fs(player) + set_fs(player) end end, }) diff --git a/src/gui.lua b/src/gui.lua index 57eea9f..27bab22 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -3,13 +3,14 @@ local damage_enabled = core.settings:get_bool "enable_damage" local model_aliases = i3.files.model_alias() local PNG, styles, fs_elements, colors = i3.files.styles() +local ItemStack = ItemStack 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 maxn, sort, concat, copy, insert, remove, unpack = + i3.get("maxn", "sort", "concat", "copy", "insert", "remove", "unpack") local true_str, is_fav, is_num = i3.get("true_str", "is_fav", "is_num") local groups_to_items, compression_active, compressible = @@ -193,19 +194,19 @@ local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlo icon = fmt("%s^\\[colorize:#000:180", icon) end - fs[#fs + 1] = fmt("image", 0, y + 0.01, icon_size, icon_size, icon) - fs[#fs + 1] = "style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]" - fs[#fs + 1] = fmt("box", icon_size + 0.1, y, box_len, icon_size, "") + insert(fs, fmt("image", 0, y + 0.01, icon_size, icon_size, icon)) + insert(fs, "style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]") + insert(fs, fmt("box", icon_size + 0.1, y, box_len, icon_size, "")) if progress then local current, target = progress.current, progress.target local curr_bar = (current * box_len) / target - fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, box_len, 0.3, "#101010") - fs[#fs + 1] = "style_type[box;colors=#9dc34c80,#9dc34c,#9dc34c,#9dc34c80]" - fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, "") - fs[#fs + 1] = "style_type[label;font_size=14]" - fs[#fs + 1] = fmt("label", icon_size + 0.5, y + 0.97, fmt("%u / %u", current, target)) + insert(fs, fmt("box", icon_size + 0.1, y + 0.8, box_len, 0.3, "#101010")) + insert(fs, "style_type[box;colors=#9dc34c80,#9dc34c,#9dc34c,#9dc34c80]") + insert(fs, fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, "")) + insert(fs, "style_type[label;font_size=14]") + insert(fs, fmt("label", icon_size + 0.5, y + 0.97, fmt("%u / %u", current, target))) y = y - 0.14 end @@ -213,11 +214,11 @@ local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlo title = _title or title desc = _desc or desc - fs[#fs + 1] = "style_type[label;font=bold;font_size=17]" - fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.4, title) - fs[#fs + 1] = "style_type[label;font=normal;font_size=15]" - fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.75, clr("#bbbbbb", desc)) - fs[#fs + 1] = "style_type[label;font_size=16]" + insert(fs, "style_type[label;font=bold;font_size=17]") + insert(fs, fmt("label", icon_size + 0.2, y + 0.4, title)) + insert(fs, "style_type[label;font=normal;font_size=15]") + insert(fs, fmt("label", icon_size + 0.2, y + 0.75, clr("#bbbbbb", desc))) + insert(fs, "style_type[label;font_size=16]") end end @@ -385,7 +386,7 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa id = i end - sks[#sks + 1] = skin.name + insert(sks, skin.name) end sks = concat(sks, ","):gsub(";", "") @@ -527,7 +528,7 @@ local function get_inventory_fs(player, data, fs) local t = {} for _, v in ipairs(props.textures) do - t[#t + 1] = ESC(v):gsub(",", "!") + insert(t, (ESC(v):gsub(",", "!"))) end local textures = concat(t, ","):gsub("!", ",") @@ -864,7 +865,7 @@ local function get_grid_fs(fs, data, rcp, is_recipe) if not added then label = fmt("%s%s\nR", label ~= "" and "\n" or "", label) - replace.items[#replace.items + 1] = replacement[2] + insert(replace.items, replacement[2]) end end end @@ -980,11 +981,11 @@ local function get_model_fs(fs, data, def, model_alias) _name = fmt("%s^[verticalframe:%u:0", v.name, v.animation.frames_h or v.animation.aspect_h) end - t[#t + 1] = _name or v.name or v + insert(t, _name or v.name or v) end while #t < 6 do - t[#t + 1] = t[#t] + insert(t, t[#t]) end fs("model", data.inv_width + 6.6, data.yoffset + 0.05, 1.3, 1.3, "preview", @@ -1124,7 +1125,7 @@ local function get_items_fs(fs, data, full_height) for i = 1, #data.items do local item = data.items[i] if not i3.compressed[item] then - new[#new + 1] = item + insert(new, item) end end @@ -1175,7 +1176,7 @@ local function get_items_fs(fs, data, full_height) local Y = round((i % ipp - X) / rows + 1, 0) Y = Y - (Y * 0.085) + 0.95 - fs[#fs + 1] = fmt("item_image_button", X, Y, size, size, name, item, "") + insert(fs, fmt("item_image_button", X, Y, size, size, name, item, "")) if compressible(item, data) then local expand = data.expand == name @@ -1340,9 +1341,9 @@ local function make_fs(player, data) local elem = fs_elements[args[1]] if elem then - t[#t + 1] = fmt(elem, select(2, ...)) + insert(t, fmt(elem, select(2, ...))) else - t[#t + 1] = concat(args) + insert(t, concat(args)) end end }) diff --git a/src/progressive.lua b/src/progressive.lua index 4be1311..385cbcc 100644 --- a/src/progressive.lua +++ b/src/progressive.lua @@ -1,5 +1,6 @@ local singleplayer = core.is_singleplayer() +local set_fs = i3.set_fs 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 = @@ -225,7 +226,7 @@ local function poll_new_items() data.itab = 1 search(data) - i3.set_fs(player) + set_fs(player) end end diff --git a/src/recipes.lua b/src/recipes.lua index caaffd6..e3408ea 100644 --- a/src/recipes.lua +++ b/src/recipes.lua @@ -1,5 +1,6 @@ local replacements = {fuel = {}} +local ItemStack = ItemStack 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") @@ -244,7 +245,7 @@ local function resolve_aliases(hash) end if newname ~= "" and i3.recipes_cache[oldname] and not hash[newname] then - i3.init_items[#i3.init_items + 1] = newname + insert(i3.init_items, newname) end end end @@ -265,7 +266,7 @@ local function init_recipes() for name in pairs(_preselect) do cache_usages(name) - i3.init_items[#i3.init_items + 1] = name + insert(i3.init_items, name) _select[name] = true end