Save players’ data on discovered recipes

Specifically, data on items/groups they have held.
This commit is contained in:
Jaidyn Ann 2024-08-13 21:42:12 -05:00
parent e4d566b13a
commit 7b1c884406

View File

@ -1,5 +1,6 @@
local S = minetest.get_translator("mtg_craftguide") local S = minetest.get_translator("mtg_craftguide")
local esc = minetest.formspec_escape local esc = minetest.formspec_escape
local storage = minetest.get_mod_storage()
local player_data = {} local player_data = {}
local init_items = {} local init_items = {}
@ -408,26 +409,6 @@ local function on_receive_fields(player, fields)
end end
end end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
local info = minetest.get_player_information(name)
player_data[name] = {
filter = "",
pagenum = 1,
items = {},
held_items = {},
held_groups = {},
known_items = {},
lang_code = info.lang_code
}
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
sfinv.register_page("mtg_craftguide:craftguide", { sfinv.register_page("mtg_craftguide:craftguide", {
title = esc(S("Recipes")), title = esc(S("Recipes")),
get = function(self, player, context) get = function(self, player, context)
@ -559,4 +540,77 @@ minetest.register_on_craft(function(itemstack, player)
update_known_items(player, itemstack:get_name()) update_known_items(player, itemstack:get_name())
end) end)
-- Initialize player-data.
-- If saved data is found, load previous held-items and held-groups data.
function load_player_data(player)
local name = player:get_player_name()
local info = minetest.get_player_information(name)
player_data[name] = {
filter = "",
pagenum = 1,
items = {},
held_items = {},
held_groups = {},
known_items = {},
lang_code = info.lang_code
}
local saved_data_str = storage:get_string("player_" .. name)
local saved_data = minetest.deserialize(saved_data_str or "")
if saved_data_str and saved_data then
player_data[name].held_items = saved_data.held_items
player_data[name].held_groups = saved_data.held_groups
end
end
-- Go over held items, and make sure any recipes that can be done with
-- them is listed in visible items.
function init_player_known_items(player)
local name = player:get_player_name()
local data = player_data[name]
for item,_ in pairs(data.held_items) do
local outputs = known_crafting_outputs(data, item)
if outputs then
for _,output in pairs(outputs) do
data.known_items[output] = true
end
end
end
end
-- Save a players held-item and held-group data.
function save_player_data(player)
local name = player:get_player_name()
local data = player_data[name]
if data then
storage:set_string("player_" .. name, minetest.serialize(data))
end
end
-- Save all players held-item and held-group data.
function save_all_player_data()
for name,data in pairs(player_data) do
if data then
storage:set_string("player_" .. name, minetest.serialize(data))
end
end
end
minetest.register_on_joinplayer(function(player)
load_player_data(player)
init_player_known_items(player)
-- Init displayed items.
local data = player_data[player:get_player_name()]
data.items = visible_items(data)
end)
minetest.register_on_leaveplayer(function(player)
save_player_data(player)
player_data[player:get_player_name()] = nil -- Save memory!
end)
minetest.register_on_shutdown(save_all_player_data)