From 4fd927e34eadd1b44a75f7ba3ba5dea3abcc3684 Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Thu, 1 Mar 2018 21:15:04 +0100 Subject: [PATCH] Bugfix #8 Create and get UI-context on demand --- chatcommands.lua | 4 ++-- formspecs.lua | 10 ++++++++++ init.lua | 1 - unified_inventory_page.lua | 4 ++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index d22e6fd..c84b695 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -1,7 +1,7 @@ local S = skins.S local function show_selection_formspec(player) - local context = skins.ui_context[player:get_player_name()] + local context = skins.get_formspec_context(player) local name = player:get_player_name() local skin = skins.get_player_skin(player) local formspec = "size[8,8]"..skins.get_skin_info_formspec(skin) @@ -89,7 +89,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end - local context = skins.ui_context[player:get_player_name()] + local context = skins.get_formspec_context(player) local action = skins.on_skin_selection_receive_fields(player, context, fields) if action == 'set' then diff --git a/formspecs.lua b/formspecs.lua index d1174c9..328c2f3 100644 --- a/formspecs.lua +++ b/formspecs.lua @@ -1,5 +1,15 @@ local S = skins.S +function skins.get_formspec_context(player) + if player then + local playername = player:get_player_name() + skins.ui_context[playername] = skins.ui_context[playername] or {} + return skins.ui_context[playername] + else + return {} + end +end + -- Show skin info function skins.get_skin_info_formspec(skin) local texture = skin:get_texture() diff --git a/init.lua b/init.lua index 2a27eb8..86a137f 100644 --- a/init.lua +++ b/init.lua @@ -47,7 +47,6 @@ end 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) diff --git a/unified_inventory_page.lua b/unified_inventory_page.lua index ba7e917..a19528a 100644 --- a/unified_inventory_page.lua +++ b/unified_inventory_page.lua @@ -15,7 +15,7 @@ unified_inventory.register_button("skins", { }) local function get_formspec(player) - local context = skins.ui_context[player:get_player_name()] + local context = skins.get_formspec_context(player) local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]".. skins.get_skin_selection_formspec(player, context, -0.2) return formspec @@ -38,7 +38,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end - local context = skins.ui_context[player:get_player_name()] + local context = skins.get_formspec_context(player) local action = skins.on_skin_selection_receive_fields(player, context, fields) if action == 'set' then unified_inventory.set_inventory_formspec(player, "skins")