Minor optimizations

This commit is contained in:
Jean-Patrick Guerrero 2021-11-15 23:50:48 +01:00
parent c5dec1bc77
commit 8f49914c9f
8 changed files with 90 additions and 58 deletions

View File

@ -73,6 +73,7 @@ i3.files.callbacks()
local storage = core.get_mod_storage() 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 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 {} i3.data = dslz(storage:get_string "data") or {}
@ -97,7 +98,7 @@ end
if rawget(_G, "armor") then if rawget(_G, "armor") then
i3.modules.armor = true i3.modules.armor = true
armor:register_on_update(i3.set_fs) armor:register_on_update(set_fs)
end end
if rawget(_G, "skins") then if rawget(_G, "skins") then
@ -108,20 +109,20 @@ if rawget(_G, "awards") then
i3.modules.awards = true i3.modules.awards = true
core.register_on_craft(function(_, player) core.register_on_craft(function(_, player)
i3.set_fs(player) set_fs(player)
end) end)
core.register_on_dignode(function(_, _, player) core.register_on_dignode(function(_, _, player)
i3.set_fs(player) set_fs(player)
end) end)
core.register_on_placenode(function(_, _, player) core.register_on_placenode(function(_, _, player)
i3.set_fs(player) set_fs(player)
end) end)
core.register_on_chat_message(function(name) core.register_on_chat_message(function(name)
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)
i3.set_fs(player) set_fs(player)
end) end)
end end
@ -164,7 +165,7 @@ local function init_data(player, info)
local inv = player:get_inventory() local inv = player:get_inventory()
inv:set_size("main", i3.INV_SIZE) inv:set_size("main", i3.INV_SIZE)
core.after(0, i3.set_fs, player) core.after(0, set_fs, player)
end end
local function init_waypoints(player) local function init_waypoints(player)

View File

@ -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 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") 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)) inv:set_list("main", get_content(content))
end end
i3.set_fs(player) set_fs(player)
end, end,
on_take = function() on_take = function()
@ -92,7 +94,7 @@ local function init_bags(player)
local content = get_content_inv(name) local content = get_content_inv(name)
content:set_list("main", {}) content:set_list("main", {})
i3.set_fs(player) set_fs(player)
end, end,
}, name) }, name)
@ -141,7 +143,7 @@ local function init_bags(player)
bag:set_stack("main", 1, bagstack) bag:set_stack("main", 1, bagstack)
data.bag_item = safe_format(bagstack) data.bag_item = safe_format(bagstack)
i3.set_fs(player) set_fs(player)
end end
local bag_content = create_inventory(fmt("i3_bag_content_%s", name), { local bag_content = create_inventory(fmt("i3_bag_content_%s", name), {

View File

@ -1,5 +1,7 @@
local _, get_inventory_fs = i3.files.gui() local _, get_inventory_fs = i3.files.gui()
local set_fs = i3.set_fs
local ItemStack = ItemStack
local S, clr = i3.get("S", "clr") local S, clr = i3.get("S", "clr")
local min, random = i3.get("min", "random") local min, random = i3.get("min", "random")
local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases") 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 data.scrbar_inv = data.scrbar_inv + 1000
end end
return i3.set_fs(player) return set_fs(player)
end, end,
}) })
@ -308,7 +310,7 @@ local function rcp_fields(player, data, fields)
elseif fields.key_enter_field == "filter" or fields.search then elseif fields.key_enter_field == "filter" or fields.search then
if fields.filter == "" then if fields.filter == "" then
reset_data(data) reset_data(data)
return i3.set_fs(player) return set_fs(player)
end end
local str = lower(fields.filter) 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) return true, tab.fields(player, data, fields)
end end
return true, i3.set_fs(player) return true, set_fs(player)
end) end)
core.register_on_player_hpchange(function(player, hpchange) 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 local hp_max = player:get_properties().hp_max
data.hp = min(hp_max, player:get_hp() + hpchange) data.hp = min(hp_max, player:get_hp() + hpchange)
i3.set_fs(player) set_fs(player)
end) end)
core.register_on_dieplayer(function(player) core.register_on_dieplayer(function(player)
@ -449,12 +451,12 @@ core.register_on_dieplayer(function(player)
local data = i3.data[name] local data = i3.data[name]
if not data then return end if not data then return end
i3.set_fs(player) set_fs(player)
end) end)
core.register_on_chatcommand(function(name) core.register_on_chatcommand(function(name)
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)
core.after(0, i3.set_fs, player) core.after(0, set_fs, player)
end) end)
core.register_on_priv_grant(function(name, _, priv) core.register_on_priv_grant(function(name, _, priv)
@ -464,7 +466,7 @@ core.register_on_priv_grant(function(name, _, priv)
data.favs = {} data.favs = {}
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)
core.after(0, i3.set_fs, player) core.after(0, set_fs, player)
end end
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 == "main" and info.to_list == "craft") or
(info.from_list == "craft" and info.to_list == "main") or (info.from_list == "craft" and info.to_list == "main") or
(info.from_list == "craftresult" and info.to_list == "main")) then (info.from_list == "craftresult" and info.to_list == "main")) then
i3.set_fs(player) set_fs(player)
end end
end) end)

View File

@ -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 = local fmt, find, match, gmatch, sub, split, lower =
string.format, string.find, string.match, string.gmatch, string.sub, string.split, string.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 function round(num, decimal)
local mul = 10 ^ decimal local mul = 10 ^ decimal
return math.floor(num * mul + 0.5) / mul return floor(num * mul + 0.5) / mul
end end
local function search(data) 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 for name, def in pairs(core.registered_items) do
if show_item(def) and item_has_groups(def.groups, groups) then if show_item(def) and item_has_groups(def.groups, groups) then
if get_all then if get_all then
names[#names + 1] = name insert(names, name)
else else
return name return name
end end
@ -304,7 +308,7 @@ local function sort_by_category(data)
end end
if to_add then if to_add then
new[#new + 1] = item insert(new, item)
end end
end end
@ -369,7 +373,7 @@ local function craft_stack(player, data, craft_rcp)
for _, item in ipairs(item_groups) do for _, item in ipairs(item_groups) do
for _name, _count in pairs(data.export_counts[rcp_usg].inv) do for _name, _count in pairs(data.export_counts[rcp_usg].inv) do
if item == _name and remaining > 0 then if item == _name and remaining > 0 then
local c = math.min(remaining, _count) local c = min(remaining, _count)
items[item] = c items[item] = c
remaining = remaining - c remaining = remaining - c
end end
@ -385,11 +389,11 @@ local function craft_stack(player, data, craft_rcp)
end end
local count = stackcount * scrbar_val local count = stackcount * scrbar_val
local iter = math.ceil(count / stackmax) local iter = ceil(count / stackmax)
local leftover = count local leftover = count
for _ = 1, iter do for _ = 1, iter do
local c = math.min(stackmax, leftover) local c = min(stackmax, leftover)
local stack = ItemStack(fmt("%s %s", stackname, c)) local stack = ItemStack(fmt("%s %s", stackname, c))
get_stack(player, stack) get_stack(player, stack)
leftover = leftover - stackmax leftover = leftover - stackmax
@ -423,7 +427,7 @@ local function get_sorting_idx(name)
end end
local function sorter(inv, reverse, mode) local function sorter(inv, reverse, mode)
table.sort(inv, function(a, b) sort(inv, function(a, b)
if mode == 1 then if mode == 1 then
a, b = a:get_name(), b:get_name() a, b = a:get_name(), b:get_name()
else else
@ -449,9 +453,9 @@ local function pre_sorting(list, start_i)
if not empty then if not empty then
if next(meta.fields) or wear then if next(meta.fields) or wear then
special[#special + 1] = stack insert(special, stack)
else else
new_inv[#new_inv + 1] = stack insert(new_inv, stack)
end end
end end
end end
@ -474,7 +478,7 @@ local function compress_items(list, start_i)
if not empty then if not empty then
if next(meta.fields) or wear or count >= stackmax then if next(meta.fields) or wear or count >= stackmax then
special[#special + 1] = stack insert(special, stack)
else else
hash[name] = hash[name] or 0 hash[name] = hash[name] or 0
hash[name] = hash[name] + count hash[name] = hash[name] + count
@ -484,11 +488,11 @@ local function compress_items(list, start_i)
for name, count in pairs(hash) do for name, count in pairs(hash) do
local stackmax = ItemStack(name):get_stack_max() local stackmax = ItemStack(name):get_stack_max()
local iter = math.ceil(count / stackmax) local iter = ceil(count / stackmax)
local leftover = count local leftover = count
for _ = 1, iter do 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 leftover = leftover - stackmax
end end
end end
@ -555,6 +559,24 @@ local function add_hud_waypoint(player, name, pos, color)
} }
end 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 _ = { local _ = {
@ -640,6 +662,7 @@ local _ = {
insert = table.insert, insert = table.insert,
remove = table.remove, remove = table.remove,
indexof = table.indexof, indexof = table.indexof,
unpack = newunpack,
is_table = is_table, is_table = is_table,
table_merge = table_merge, table_merge = table_merge,
table_replace = table_replace, table_replace = table_replace,
@ -668,5 +691,5 @@ function i3.get(...)
t[i] = _[var] t[i] = _[var]
end end
return unpack(t) return newunpack(t)
end end

View File

@ -1,3 +1,4 @@
local set_fs = i3.set_fs
local fmt, play_sound, create_inventory = i3.get("fmt", "play_sound", "create_inventory") local fmt, play_sound, create_inventory = i3.get("fmt", "play_sound", "create_inventory")
local trash = create_inventory("i3_trash", { local trash = create_inventory("i3_trash", {
@ -12,7 +13,7 @@ local trash = create_inventory("i3_trash", {
play_sound(name, "i3_trash", 1.0) play_sound(name, "i3_trash", 1.0)
if not core.is_creative_enabled(name) then if not core.is_creative_enabled(name) then
i3.set_fs(player) set_fs(player)
end end
end, end,
}) })

View File

@ -3,13 +3,14 @@ local damage_enabled = core.settings:get_bool "enable_damage"
local model_aliases = i3.files.model_alias() local model_aliases = i3.files.model_alias()
local PNG, styles, fs_elements, colors = i3.files.styles() local PNG, styles, fs_elements, colors = i3.files.styles()
local ItemStack = ItemStack
local S, ES, translate = i3.get("S", "ES", "translate") local S, ES, translate = i3.get("S", "ES", "translate")
local clr, ESC, check_privs = i3.get("clr", "ESC", "check_privs") 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 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 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 reg_items, reg_tools, reg_entities = i3.get("reg_items", "reg_tools", "reg_entities")
local maxn, sort, concat, copy, insert, remove = local maxn, sort, concat, copy, insert, remove, unpack =
i3.get("maxn", "sort", "concat", "copy", "insert", "remove") 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 true_str, is_fav, is_num = i3.get("true_str", "is_fav", "is_num")
local groups_to_items, compression_active, compressible = 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) icon = fmt("%s^\\[colorize:#000:180", icon)
end end
fs[#fs + 1] = fmt("image", 0, y + 0.01, icon_size, icon_size, icon) insert(fs, fmt("image", 0, y + 0.01, icon_size, icon_size, icon))
fs[#fs + 1] = "style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]" insert(fs, "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("box", icon_size + 0.1, y, box_len, icon_size, ""))
if progress then if progress then
local current, target = progress.current, progress.target local current, target = progress.current, progress.target
local curr_bar = (current * box_len) / 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") insert(fs, 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]" insert(fs, "style_type[box;colors=#9dc34c80,#9dc34c,#9dc34c,#9dc34c80]")
fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, "") insert(fs, fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, ""))
fs[#fs + 1] = "style_type[label;font_size=14]" insert(fs, "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("label", icon_size + 0.5, y + 0.97, fmt("%u / %u", current, target)))
y = y - 0.14 y = y - 0.14
end end
@ -213,11 +214,11 @@ local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlo
title = _title or title title = _title or title
desc = _desc or desc desc = _desc or desc
fs[#fs + 1] = "style_type[label;font=bold;font_size=17]" insert(fs, "style_type[label;font=bold;font_size=17]")
fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.4, title) insert(fs, fmt("label", icon_size + 0.2, y + 0.4, title))
fs[#fs + 1] = "style_type[label;font=normal;font_size=15]" insert(fs, "style_type[label;font=normal;font_size=15]")
fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.75, clr("#bbbbbb", desc)) insert(fs, 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_size=16]")
end end
end end
@ -385,7 +386,7 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
id = i id = i
end end
sks[#sks + 1] = skin.name insert(sks, skin.name)
end end
sks = concat(sks, ","):gsub(";", "") sks = concat(sks, ","):gsub(";", "")
@ -527,7 +528,7 @@ local function get_inventory_fs(player, data, fs)
local t = {} local t = {}
for _, v in ipairs(props.textures) do for _, v in ipairs(props.textures) do
t[#t + 1] = ESC(v):gsub(",", "!") insert(t, (ESC(v):gsub(",", "!")))
end end
local textures = concat(t, ","):gsub("!", ",") local textures = concat(t, ","):gsub("!", ",")
@ -864,7 +865,7 @@ local function get_grid_fs(fs, data, rcp, is_recipe)
if not added then if not added then
label = fmt("%s%s\nR", label ~= "" and "\n" or "", label) 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 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) _name = fmt("%s^[verticalframe:%u:0", v.name, v.animation.frames_h or v.animation.aspect_h)
end end
t[#t + 1] = _name or v.name or v insert(t, _name or v.name or v)
end end
while #t < 6 do while #t < 6 do
t[#t + 1] = t[#t] insert(t, t[#t])
end end
fs("model", data.inv_width + 6.6, data.yoffset + 0.05, 1.3, 1.3, "preview", 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 for i = 1, #data.items do
local item = data.items[i] local item = data.items[i]
if not i3.compressed[item] then if not i3.compressed[item] then
new[#new + 1] = item insert(new, item)
end end
end end
@ -1175,7 +1176,7 @@ local function get_items_fs(fs, data, full_height)
local Y = round((i % ipp - X) / rows + 1, 0) local Y = round((i % ipp - X) / rows + 1, 0)
Y = Y - (Y * 0.085) + 0.95 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 if compressible(item, data) then
local expand = data.expand == name local expand = data.expand == name
@ -1340,9 +1341,9 @@ local function make_fs(player, data)
local elem = fs_elements[args[1]] local elem = fs_elements[args[1]]
if elem then if elem then
t[#t + 1] = fmt(elem, select(2, ...)) insert(t, fmt(elem, select(2, ...)))
else else
t[#t + 1] = concat(args) insert(t, concat(args))
end end
end end
}) })

View File

@ -1,5 +1,6 @@
local singleplayer = core.is_singleplayer() local singleplayer = core.is_singleplayer()
local set_fs = i3.set_fs
local fmt, search, table_merge, array_diff = local fmt, search, table_merge, array_diff =
i3.get("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 = local is_group, extract_groups, item_has_groups, apply_recipe_filters =
@ -225,7 +226,7 @@ local function poll_new_items()
data.itab = 1 data.itab = 1
search(data) search(data)
i3.set_fs(player) set_fs(player)
end end
end end

View File

@ -1,5 +1,6 @@
local replacements = {fuel = {}} local replacements = {fuel = {}}
local ItemStack = ItemStack
local fmt, reg_items, reg_aliases = i3.get("fmt", "reg_items", "reg_aliases") 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 maxn, copy, insert, sort, match = i3.get("maxn", "copy", "insert", "sort", "match")
@ -244,7 +245,7 @@ local function resolve_aliases(hash)
end end
if newname ~= "" and i3.recipes_cache[oldname] and not hash[newname] then 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 end
end end
@ -265,7 +266,7 @@ local function init_recipes()
for name in pairs(_preselect) do for name in pairs(_preselect) do
cache_usages(name) cache_usages(name)
i3.init_items[#i3.init_items + 1] = name insert(i3.init_items, name)
_select[name] = true _select[name] = true
end end