From 49620617524b626207fbf025474be6db4404e470 Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Wed, 14 Feb 2018 02:19:15 +0100 Subject: [PATCH] moved the chat- and UI context to ram --- chatcommands.lua | 19 ++++++------------- formspecs.lua | 8 +++----- init.lua | 6 ++++++ sfinv_page.lua | 3 +-- unified_inventory_page.lua | 22 +++------------------- 5 files changed, 19 insertions(+), 39 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index 22fe044..841145d 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -1,10 +1,9 @@ -local function show_selection_formspec(player, context) - skins.rebuild_formspec_context(player, context) +local function show_selection_formspec(player) + local context = skins.ui_context[player:get_player_name()] local name = player:get_player_name() local skin = skins.get_player_skin(player) local formspec = "size[8,8]"..skins.get_skin_info_formspec(skin) - formspec = formspec..skins.get_skin_selection_formspec(context, 3.5) - player:set_attribute('skinsdb_context', minetest.serialize(context)) + formspec = formspec..skins.get_skin_selection_formspec(player, context, 3.5) minetest.show_formspec(name, 'skinsdb_show_ui', formspec) end @@ -82,8 +81,7 @@ minetest.register_chatcommand("skinsdb", { local formspec = "size[8,3]"..skins.get_skin_info_formspec(skin) minetest.show_formspec(name, 'skinsdb_show_skin', formspec) elseif command == "ui" then - local context = minetest.deserialize(player:get_attribute('skinsdb_context')) or {} - show_selection_formspec(player, context) + show_selection_formspec(player) end end, }) @@ -94,17 +92,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end - local context = minetest.deserialize(player:get_attribute('skinsdb_context')) - if not context then - return - end + local context = skins.ui_context[player:get_player_name()] local action = skins.on_skin_selection_receive_fields(player, context, fields) if action == 'set' then - player:set_attribute('skinsdb_context',"") minetest.close_formspec(player:get_player_name(), formname) elseif action == 'page' then - player:set_attribute('skinsdb_context', minetest.serialize(context)) - show_selection_formspec(player, context) + show_selection_formspec(player) end end) diff --git a/formspecs.lua b/formspecs.lua index 0659724..800cb44 100644 --- a/formspecs.lua +++ b/formspecs.lua @@ -42,7 +42,8 @@ function skins.get_skin_info_formspec(skin) return formspec end -function skins.get_skin_selection_formspec(context, y_delta) +function skins.get_skin_selection_formspec(player, context, y_delta) + skins.rebuild_formspec_context(player, context) local page = context.skins_page or 1 local formspec = "" for i = (page-1)*16+1, page*16 do @@ -93,10 +94,7 @@ function skins.on_skin_selection_receive_fields(player, context, fields) for field, _ in pairs(fields) do local current = string.split(field, "$", 2) if current[1] == "skins_set" then - local selected_skin = context.skins_list[tonumber(current[2])] - setmetatable(selected_skin, skins.skin_class) - - skins.set_player_skin(player, selected_skin) + skins.set_player_skin(player, context.skins_list[tonumber(current[2])]) return 'set' elseif current[1] == "skins_page" then context.skins_page = tonumber(current[2]) diff --git a/init.lua b/init.lua index 32a2205..2a27eb8 100644 --- a/init.lua +++ b/init.lua @@ -44,6 +44,12 @@ if minetest.global_exists("armor") then end -- Update skin on join +skins.ui_context = {} minetest.register_on_joinplayer(function(player) skins.update_player_skin(player) + skins.ui_context[player:get_player_name()] = {} +end) + +minetest.register_on_leaveplayer(function(player) + skins.ui_context[player:get_player_name()] = nil end) diff --git a/sfinv_page.lua b/sfinv_page.lua index e90dd38..f8d627b 100644 --- a/sfinv_page.lua +++ b/sfinv_page.lua @@ -4,7 +4,7 @@ local S = skins.S local function get_formspec(player, context) local skin = skins.get_player_skin(player) local formspec = skins.get_skin_info_formspec(skin) - formspec = formspec..skins.get_skin_selection_formspec(context, 4) + formspec = formspec..skins.get_skin_selection_formspec(player, context, 4) return formspec end @@ -12,7 +12,6 @@ sfinv.register_page("skins:overview", { title = "Skins", get = function(self, player, context) -- collect skins data - context = skins.rebuild_formspec_context(player, context) return sfinv.make_formspec(player, context, get_formspec(player, context)) end, on_player_receive_fields = function(self, player, context, fields) diff --git a/unified_inventory_page.lua b/unified_inventory_page.lua index 91f4e57..ba7e917 100644 --- a/unified_inventory_page.lua +++ b/unified_inventory_page.lua @@ -15,12 +15,9 @@ unified_inventory.register_button("skins", { }) local function get_formspec(player) - -- unified inventory is stateless, but skins pager needs some context usage to be more flexible - local context = minetest.deserialize(player:get_attribute('skinsdb_unified_inventory_context')) or {} - context = skins.rebuild_formspec_context(player, context) + local context = skins.ui_context[player:get_player_name()] local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]".. - skins.get_skin_selection_formspec(context, -0.2) - player:set_attribute('skinsdb_unified_inventory_context', minetest.serialize(context)) + skins.get_skin_selection_formspec(player, context, -0.2) return formspec end @@ -33,7 +30,6 @@ unified_inventory.register_page("skins_page", { -- click button handlers minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.skins then - player:set_attribute('skinsdb_unified_inventory_context',"") --reset context unified_inventory.set_inventory_formspec(player, "craft") return end @@ -42,23 +38,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end - local context -- read context only if skins related action - for field, _ in pairs(fields) do - if field:sub(1,5) == "skins" then - context = minetest.deserialize(player:get_attribute('skinsdb_unified_inventory_context')) or {} - break - end - end - if not context then - return - end - + local context = skins.ui_context[player:get_player_name()] local action = skins.on_skin_selection_receive_fields(player, context, fields) if action == 'set' then - player:set_attribute('skinsdb_unified_inventory_context',"") --reset context unified_inventory.set_inventory_formspec(player, "skins") elseif action == 'page' then - player:set_attribute('skinsdb_unified_inventory_context', minetest.serialize(context)) unified_inventory.set_inventory_formspec(player, "skins_page") end end)