mirror of
https://github.com/minetest-mods/craftguide.git
synced 2024-11-23 03:13:44 +01:00
Small optimization
This commit is contained in:
parent
a2e4f20791
commit
e710fcd483
60
init.lua
60
init.lua
@ -1,7 +1,7 @@
|
|||||||
craftguide = {}
|
craftguide = {}
|
||||||
|
|
||||||
local M = minetest
|
local M = minetest
|
||||||
local player_data = {}
|
local pdata = {}
|
||||||
|
|
||||||
-- Caches
|
-- Caches
|
||||||
local init_items = {}
|
local init_items = {}
|
||||||
@ -18,6 +18,8 @@ local colorize = M.colorize
|
|||||||
local reg_items = M.registered_items
|
local reg_items = M.registered_items
|
||||||
local get_result = M.get_craft_result
|
local get_result = M.get_craft_result
|
||||||
local show_formspec = M.show_formspec
|
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 get_player_by_name = M.get_player_by_name
|
||||||
local serialize, deserialize = M.serialize, M.deserialize
|
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))
|
DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE))
|
||||||
|
|
||||||
local GRID_LIMIT = 5
|
local GRID_LIMIT = 5
|
||||||
local POLL_FREQ = 0.25
|
|
||||||
|
|
||||||
local FMT = {
|
local FMT = {
|
||||||
box = "box[%f,%f;%f,%f;%s]",
|
box = "box[%f,%f;%f,%f;%s]",
|
||||||
@ -316,7 +317,7 @@ local function get_filtered_items(player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function cache_recipes(output)
|
local function cache_recipes(output)
|
||||||
local recipes = M.get_all_craft_recipes(output) or {}
|
local recipes = get_all_recipes(output) or {}
|
||||||
local c = 0
|
local c = 0
|
||||||
|
|
||||||
for i = 1, #custom_crafts do
|
for i = 1, #custom_crafts do
|
||||||
@ -590,7 +591,7 @@ local function get_recipe_fs(data, iY)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function make_formspec(name)
|
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 iY = sfinv_only and 4 or data.iX - 5
|
||||||
local ipp = data.iX * iY
|
local ipp = data.iX * iY
|
||||||
|
|
||||||
@ -777,7 +778,7 @@ local function search(data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function init_data(name)
|
local function init_data(name)
|
||||||
player_data[name] = {
|
pdata[name] = {
|
||||||
filter = "",
|
filter = "",
|
||||||
pagenum = 1,
|
pagenum = 1,
|
||||||
iX = sfinv_only and 8 or DEFAULT_SIZE,
|
iX = sfinv_only and 8 or DEFAULT_SIZE,
|
||||||
@ -832,7 +833,7 @@ end
|
|||||||
|
|
||||||
local function on_receive_fields(player, fields)
|
local function on_receive_fields(player, fields)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
for elem_name, def in pairs(formspec_elements) do
|
for elem_name, def in pairs(formspec_elements) do
|
||||||
if fields[elem_name] and def.action then
|
if fields[elem_name] and def.action then
|
||||||
@ -940,7 +941,7 @@ if sfinv_only then
|
|||||||
on_enter = function(self, player, context)
|
on_enter = function(self, player, context)
|
||||||
if next(recipe_filters) then
|
if next(recipe_filters) then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
data.items_raw = get_filtered_items(player)
|
data.items_raw = get_filtered_items(player)
|
||||||
search(data)
|
search(data)
|
||||||
@ -962,7 +963,7 @@ else
|
|||||||
local name = user:get_player_name()
|
local name = user:get_player_name()
|
||||||
|
|
||||||
if next(recipe_filters) then
|
if next(recipe_filters) then
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
data.items_raw = get_filtered_items(user)
|
data.items_raw = get_filtered_items(user)
|
||||||
search(data)
|
search(data)
|
||||||
end
|
end
|
||||||
@ -1047,6 +1048,9 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
if progressive_mode then
|
if progressive_mode then
|
||||||
|
local PLAYERS = {}
|
||||||
|
local POLL_FREQ = 0.25
|
||||||
|
|
||||||
local function item_in_inv(item, inv_items)
|
local function item_in_inv(item, inv_items)
|
||||||
local inv_items_size = #inv_items
|
local inv_items_size = #inv_items
|
||||||
|
|
||||||
@ -1082,7 +1086,7 @@ if progressive_mode then
|
|||||||
|
|
||||||
local function progressive_filter(recipes, player)
|
local function progressive_filter(recipes, player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
if #data.inv_items == 0 then
|
if #data.inv_items == 0 then
|
||||||
return {}
|
return {}
|
||||||
@ -1128,13 +1132,13 @@ if progressive_mode then
|
|||||||
-- Workaround. Need an engine call to detect when the contents
|
-- Workaround. Need an engine call to detect when the contents
|
||||||
-- of the player inventory changed, instead.
|
-- of the player inventory changed, instead.
|
||||||
local function poll_new_items()
|
local function poll_new_items()
|
||||||
local players = M.get_connected_players()
|
for i = 1, #PLAYERS do
|
||||||
for i = 1, #players do
|
local player = PLAYERS[i]
|
||||||
local player = players[i]
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
local inv_items = get_inv_items(player)
|
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
|
if #diff > 0 then
|
||||||
data.inv_items = table_merge(diff, data.inv_items)
|
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)
|
craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
|
||||||
|
|
||||||
M.register_on_joinplayer(function(player)
|
M.register_on_joinplayer(function(player)
|
||||||
|
PLAYERS = get_players()
|
||||||
|
|
||||||
local meta = player:get_meta()
|
local meta = player:get_meta()
|
||||||
local name = player:get_player_name()
|
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 {}
|
data.inv_items = deserialize(meta:get_string("inv_items")) or {}
|
||||||
end)
|
end)
|
||||||
@ -1159,17 +1165,19 @@ if progressive_mode then
|
|||||||
local function save_meta(player)
|
local function save_meta(player)
|
||||||
local meta = player:get_meta()
|
local meta = player:get_meta()
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
meta:set_string("inv_items", serialize(data.inv_items))
|
meta:set_string("inv_items", serialize(data.inv_items))
|
||||||
end
|
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()
|
M.register_on_shutdown(function()
|
||||||
local players = M.get_connected_players()
|
for i = 1, #PLAYERS do
|
||||||
for i = 1, #players do
|
local player = PLAYERS[i]
|
||||||
local player = players[i]
|
|
||||||
save_meta(player)
|
save_meta(player)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@ -1177,17 +1185,17 @@ end
|
|||||||
|
|
||||||
M.register_on_leaveplayer(function(player)
|
M.register_on_leaveplayer(function(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
player_data[name] = nil
|
pdata[name] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
M.register_chatcommand("craft", {
|
M.register_chatcommand("craft", {
|
||||||
description = S("Show recipe(s) of the pointed node"),
|
description = S("Show recipe(s) of the pointed node"),
|
||||||
func = function(name)
|
func = function(name)
|
||||||
local player = get_player_by_name(name)
|
local player = get_player_by_name(name)
|
||||||
local ppos = player:get_pos()
|
|
||||||
local dir = player:get_look_dir()
|
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
|
local node_name
|
||||||
|
|
||||||
for i = 1, 10 do
|
for i = 1, 10 do
|
||||||
@ -1206,7 +1214,7 @@ M.register_chatcommand("craft", {
|
|||||||
return false, red .. S("No node pointed")
|
return false, red .. S("No node pointed")
|
||||||
end
|
end
|
||||||
|
|
||||||
local data = player_data[name]
|
local data = pdata[name]
|
||||||
reset_data(data)
|
reset_data(data)
|
||||||
|
|
||||||
local recipes = recipes_cache[node_name]
|
local recipes = recipes_cache[node_name]
|
||||||
@ -1233,7 +1241,7 @@ M.register_chatcommand("craft", {
|
|||||||
end
|
end
|
||||||
|
|
||||||
data.query_item = node_name
|
data.query_item = node_name
|
||||||
data.recipes = recipes
|
data.recipes = recipes
|
||||||
|
|
||||||
return true, show_fs(player, name)
|
return true, show_fs(player, name)
|
||||||
end,
|
end,
|
||||||
@ -1243,7 +1251,7 @@ function craftguide.show(name, item, show_usages)
|
|||||||
local func = "craftguide." .. __func() .. "(): "
|
local func = "craftguide." .. __func() .. "(): "
|
||||||
assert(is_str(name), func .. "player name missing")
|
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 player = get_player_by_name(name)
|
||||||
local query_item = data.query_item
|
local query_item = data.query_item
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user