From 7b1c8844068d556305cac852ccf50178d5620c24 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:42:12 -0500 Subject: [PATCH] =?UTF-8?q?Save=20players=E2=80=99=20data=20on=20discovere?= =?UTF-8?q?d=20recipes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Specifically, data on items/groups they have held. --- init.lua | 94 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 20 deletions(-) diff --git a/init.lua b/init.lua index 00aa93b..f4396ee 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,6 @@ local S = minetest.get_translator("mtg_craftguide") local esc = minetest.formspec_escape +local storage = minetest.get_mod_storage() local player_data = {} local init_items = {} @@ -408,26 +409,6 @@ local function on_receive_fields(player, fields) 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", { title = esc(S("Recipes")), get = function(self, player, context) @@ -559,4 +540,77 @@ minetest.register_on_craft(function(itemstack, player) update_known_items(player, itemstack:get_name()) 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 player’s 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)