From e710fcd48395f230f3a732086de59a6a358aeb15 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sun, 23 Jun 2019 01:42:12 +0200 Subject: [PATCH] Small optimization --- init.lua | 60 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/init.lua b/init.lua index 8483219..03afd38 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,7 @@ craftguide = {} local M = minetest -local player_data = {} +local pdata = {} -- Caches local init_items = {} @@ -18,6 +18,8 @@ local colorize = M.colorize local reg_items = M.registered_items local get_result = M.get_craft_result local show_formspec = M.show_formspec +local get_players = M.get_connected_players +local get_all_recipes = M.get_all_craft_recipes local get_player_by_name = M.get_player_by_name local serialize, deserialize = M.serialize, M.deserialize @@ -41,7 +43,6 @@ local MIN_LIMIT, MAX_LIMIT = 10, 12 DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE)) local GRID_LIMIT = 5 -local POLL_FREQ = 0.25 local FMT = { box = "box[%f,%f;%f,%f;%s]", @@ -316,7 +317,7 @@ local function get_filtered_items(player) end local function cache_recipes(output) - local recipes = M.get_all_craft_recipes(output) or {} + local recipes = get_all_recipes(output) or {} local c = 0 for i = 1, #custom_crafts do @@ -590,7 +591,7 @@ local function get_recipe_fs(data, iY) end local function make_formspec(name) - local data = player_data[name] + local data = pdata[name] local iY = sfinv_only and 4 or data.iX - 5 local ipp = data.iX * iY @@ -777,7 +778,7 @@ local function search(data) end local function init_data(name) - player_data[name] = { + pdata[name] = { filter = "", pagenum = 1, iX = sfinv_only and 8 or DEFAULT_SIZE, @@ -832,7 +833,7 @@ end local function on_receive_fields(player, fields) local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] for elem_name, def in pairs(formspec_elements) do if fields[elem_name] and def.action then @@ -940,7 +941,7 @@ if sfinv_only then on_enter = function(self, player, context) if next(recipe_filters) then local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] data.items_raw = get_filtered_items(player) search(data) @@ -962,7 +963,7 @@ else local name = user:get_player_name() if next(recipe_filters) then - local data = player_data[name] + local data = pdata[name] data.items_raw = get_filtered_items(user) search(data) end @@ -1047,6 +1048,9 @@ else end if progressive_mode then + local PLAYERS = {} + local POLL_FREQ = 0.25 + local function item_in_inv(item, inv_items) local inv_items_size = #inv_items @@ -1082,7 +1086,7 @@ if progressive_mode then local function progressive_filter(recipes, player) local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] if #data.inv_items == 0 then return {} @@ -1128,13 +1132,13 @@ if progressive_mode then -- Workaround. Need an engine call to detect when the contents -- of the player inventory changed, instead. local function poll_new_items() - local players = M.get_connected_players() - for i = 1, #players do - local player = players[i] + for i = 1, #PLAYERS do + local player = PLAYERS[i] local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] + local inv_items = get_inv_items(player) - local diff = table_diff(inv_items, data.inv_items) + local diff = table_diff(inv_items, data.inv_items) if #diff > 0 then data.inv_items = table_merge(diff, data.inv_items) @@ -1149,9 +1153,11 @@ if progressive_mode then craftguide.add_recipe_filter("Default progressive filter", progressive_filter) M.register_on_joinplayer(function(player) + PLAYERS = get_players() + local meta = player:get_meta() local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] data.inv_items = deserialize(meta:get_string("inv_items")) or {} end) @@ -1159,17 +1165,19 @@ if progressive_mode then local function save_meta(player) local meta = player:get_meta() local name = player:get_player_name() - local data = player_data[name] + local data = pdata[name] meta:set_string("inv_items", serialize(data.inv_items)) end - M.register_on_leaveplayer(save_meta) + M.register_on_leaveplayer(function(player) + PLAYERS = get_players() + save_meta(player) + end) M.register_on_shutdown(function() - local players = M.get_connected_players() - for i = 1, #players do - local player = players[i] + for i = 1, #PLAYERS do + local player = PLAYERS[i] save_meta(player) end end) @@ -1177,17 +1185,17 @@ end M.register_on_leaveplayer(function(player) local name = player:get_player_name() - player_data[name] = nil + pdata[name] = nil end) M.register_chatcommand("craft", { description = S("Show recipe(s) of the pointed node"), func = function(name) local player = get_player_by_name(name) - local ppos = player:get_pos() local dir = player:get_look_dir() + local ppos = player:get_pos() + ppos.y = ppos.y + 1.625 - ppos.y = ppos.y + 1.625 local node_name for i = 1, 10 do @@ -1206,7 +1214,7 @@ M.register_chatcommand("craft", { return false, red .. S("No node pointed") end - local data = player_data[name] + local data = pdata[name] reset_data(data) local recipes = recipes_cache[node_name] @@ -1233,7 +1241,7 @@ M.register_chatcommand("craft", { end data.query_item = node_name - data.recipes = recipes + data.recipes = recipes return true, show_fs(player, name) end, @@ -1243,7 +1251,7 @@ function craftguide.show(name, item, show_usages) local func = "craftguide." .. __func() .. "(): " assert(is_str(name), func .. "player name missing") - local data = player_data[name] + local data = pdata[name] local player = get_player_by_name(name) local query_item = data.query_item