Separate player data

This commit is contained in:
Paul Ouellette 2019-01-08 14:28:53 -05:00
parent 7f6dd430ce
commit e0db5834b3

@ -4,7 +4,9 @@ craftguide = {
} }
local mt = minetest local mt = minetest
local datas = {searches = {}} local player_data = {}
local init_items = {}
local searches = {}
local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode") local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = mt.settings:get_bool("craftguide_sfinv_only") local sfinv_only = mt.settings:get_bool("craftguide_sfinv_only")
@ -119,13 +121,13 @@ local function get_fueltime(item)
return get_result({method = "fuel", width = 1, items = {item}}).time return get_result({method = "fuel", width = 1, items = {item}}).time
end end
local function reset_datas(data) local function reset_data(data)
data.show_usage = nil data.show_usage = nil
data.filter = "" data.filter = ""
data.input = nil data.input = nil
data.pagenum = 1 data.pagenum = 1
data.rnum = 1 data.rnum = 1
data.items = progressive_mode and data.init_filter_items or datas.init_items data.items = progressive_mode and data.init_filter_items or init_items
end end
local function in_table(T) local function in_table(T)
@ -379,12 +381,12 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
end end
local function get_formspec(player_name) local function get_formspec(player_name)
local data = datas[player_name] local data = player_data[player_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
if not data.items then if not data.items then
data.items = datas.init_items data.items = init_items
end end
data.pagemax = max(1, ceil(#data.items / ipp)) data.pagemax = max(1, ceil(#data.items / ipp))
@ -511,12 +513,12 @@ end
local function get_filter_items(data, player) local function get_filter_items(data, player)
local filter = data.filter local filter = data.filter
if datas.searches[filter] then if searches[filter] then
data.items = datas.searches[filter] data.items = searches[filter]
return return
end end
local items_list = progressive_mode and data.init_filter_items or datas.init_items local items_list = progressive_mode and data.init_filter_items or init_items
local inv = player:get_inventory() local inv = player:get_inventory()
local filtered_list, counter = {}, 0 local filtered_list, counter = {}, 0
@ -544,26 +546,25 @@ local function get_filter_items(data, player)
end end
elseif filter ~= "" then elseif filter ~= "" then
-- Cache the results only if searched 2 times -- Cache the results only if searched 2 times
if datas.searches[filter] == nil then if searches[filter] == nil then
datas.searches[filter] = false searches[filter] = false
else else
datas.searches[filter] = filtered_list searches[filter] = filtered_list
end end
end end
data.items = filtered_list data.items = filtered_list
end end
local function init_datas(user, name) local function init_data(user, name)
datas[name] = {filter = "", pagenum = 1, iX = sfinv_only and 8 or DEFAULT_SIZE} player_data[name] = {filter = "", pagenum = 1, iX = sfinv_only and 8 or DEFAULT_SIZE}
if progressive_mode then if progressive_mode then
get_filter_items(datas[name], user) get_filter_items(player_data[name], user)
end end
end end
local function get_init_items() local function get_init_items()
local items_list, c = {}, 0 local c = 0
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
local is_fuel = get_fueltime(name) > 0 local is_fuel = get_fueltime(name) > 0
if not (def.groups.not_in_craft_guide == 1 or if not (def.groups.not_in_craft_guide == 1 or
@ -571,12 +572,10 @@ local function get_init_items()
(get_recipe(name).items or is_fuel) and (get_recipe(name).items or is_fuel) and
def.description and def.description ~= "" then def.description and def.description ~= "" then
c = c + 1 c = c + 1
items_list[c] = name init_items[c] = name
end end
end end
sort(init_items)
sort(items_list)
datas.init_items = items_list
end end
mt.register_on_mods_loaded(get_init_items) mt.register_on_mods_loaded(get_init_items)
@ -629,10 +628,10 @@ local function get_fields(player, ...)
end end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = player_data[player_name]
if fields.clear then if fields.clear then
reset_datas(data) reset_data(data)
show_fs(player, player_name) show_fs(player, player_name)
elseif fields.alternate then elseif fields.alternate then
@ -744,10 +743,10 @@ if sfinv_only then
on_enter = function(self, player, context) on_enter = function(self, player, context)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = player_data[player_name]
if progressive_mode or not data then if progressive_mode or not data then
init_datas(player, player_name) init_data(player, player_name)
end end
end, end,
@ -760,10 +759,10 @@ else
local function on_use(itemstack, user) local function on_use(itemstack, user)
local player_name = user:get_player_name() local player_name = user:get_player_name()
local data = datas[player_name] local data = player_data[player_name]
if progressive_mode or not data then if progressive_mode or not data then
init_datas(user, player_name) init_data(user, player_name)
get_formspec(player_name) get_formspec(player_name)
else else
show_formspec(player_name, "craftguide", data.formspec) show_formspec(player_name, "craftguide", data.formspec)
@ -867,12 +866,12 @@ if not progressive_mode then
if not node_name then if not node_name then
return false, colorize("[craftguide] ", "red") .. return false, colorize("[craftguide] ", "red") ..
S("No node pointed") S("No node pointed")
elseif not datas[name] then elseif not player_data[name] then
init_datas(player, name) init_data(player, name)
end end
local data = datas[name] local data = player_data[name]
reset_datas(data) reset_data(data)
local is_fuel = get_fueltime(node_name) > 0 local is_fuel = get_fueltime(node_name) > 0
local recipes = get_recipes(node_name) local recipes = get_recipes(node_name)