moved the chat- and UI context to ram

This commit is contained in:
Alexander Weber 2018-02-14 02:19:15 +01:00 committed by sofar
parent 4704e17dd2
commit 4962061752
5 changed files with 19 additions and 39 deletions

View File

@ -1,10 +1,9 @@
local function show_selection_formspec(player, context) local function show_selection_formspec(player)
skins.rebuild_formspec_context(player, context) local context = skins.ui_context[player:get_player_name()]
local name = player:get_player_name() local name = player:get_player_name()
local skin = skins.get_player_skin(player) local skin = skins.get_player_skin(player)
local formspec = "size[8,8]"..skins.get_skin_info_formspec(skin) local formspec = "size[8,8]"..skins.get_skin_info_formspec(skin)
formspec = formspec..skins.get_skin_selection_formspec(context, 3.5) formspec = formspec..skins.get_skin_selection_formspec(player, context, 3.5)
player:set_attribute('skinsdb_context', minetest.serialize(context))
minetest.show_formspec(name, 'skinsdb_show_ui', formspec) minetest.show_formspec(name, 'skinsdb_show_ui', formspec)
end end
@ -82,8 +81,7 @@ minetest.register_chatcommand("skinsdb", {
local formspec = "size[8,3]"..skins.get_skin_info_formspec(skin) local formspec = "size[8,3]"..skins.get_skin_info_formspec(skin)
minetest.show_formspec(name, 'skinsdb_show_skin', formspec) minetest.show_formspec(name, 'skinsdb_show_skin', formspec)
elseif command == "ui" then elseif command == "ui" then
local context = minetest.deserialize(player:get_attribute('skinsdb_context')) or {} show_selection_formspec(player)
show_selection_formspec(player, context)
end end
end, end,
}) })
@ -94,17 +92,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return return
end end
local context = minetest.deserialize(player:get_attribute('skinsdb_context')) local context = skins.ui_context[player:get_player_name()]
if not context then
return
end
local action = skins.on_skin_selection_receive_fields(player, context, fields) local action = skins.on_skin_selection_receive_fields(player, context, fields)
if action == 'set' then if action == 'set' then
player:set_attribute('skinsdb_context',"")
minetest.close_formspec(player:get_player_name(), formname) minetest.close_formspec(player:get_player_name(), formname)
elseif action == 'page' then elseif action == 'page' then
player:set_attribute('skinsdb_context', minetest.serialize(context)) show_selection_formspec(player)
show_selection_formspec(player, context)
end end
end) end)

View File

@ -42,7 +42,8 @@ function skins.get_skin_info_formspec(skin)
return formspec return formspec
end 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 page = context.skins_page or 1
local formspec = "" local formspec = ""
for i = (page-1)*16+1, page*16 do 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 for field, _ in pairs(fields) do
local current = string.split(field, "$", 2) local current = string.split(field, "$", 2)
if current[1] == "skins_set" then if current[1] == "skins_set" then
local selected_skin = context.skins_list[tonumber(current[2])] skins.set_player_skin(player, context.skins_list[tonumber(current[2])])
setmetatable(selected_skin, skins.skin_class)
skins.set_player_skin(player, selected_skin)
return 'set' return 'set'
elseif current[1] == "skins_page" then elseif current[1] == "skins_page" then
context.skins_page = tonumber(current[2]) context.skins_page = tonumber(current[2])

View File

@ -44,6 +44,12 @@ if minetest.global_exists("armor") then
end end
-- Update skin on join -- Update skin on join
skins.ui_context = {}
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
skins.update_player_skin(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) end)

View File

@ -4,7 +4,7 @@ local S = skins.S
local function get_formspec(player, context) local function get_formspec(player, context)
local skin = skins.get_player_skin(player) local skin = skins.get_player_skin(player)
local formspec = skins.get_skin_info_formspec(skin) 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 return formspec
end end
@ -12,7 +12,6 @@ sfinv.register_page("skins:overview", {
title = "Skins", title = "Skins",
get = function(self, player, context) get = function(self, player, context)
-- collect skins data -- collect skins data
context = skins.rebuild_formspec_context(player, context)
return sfinv.make_formspec(player, context, get_formspec(player, context)) return sfinv.make_formspec(player, context, get_formspec(player, context))
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)

View File

@ -15,12 +15,9 @@ unified_inventory.register_button("skins", {
}) })
local function get_formspec(player) local function get_formspec(player)
-- unified inventory is stateless, but skins pager needs some context usage to be more flexible local context = skins.ui_context[player:get_player_name()]
local context = minetest.deserialize(player:get_attribute('skinsdb_unified_inventory_context')) or {}
context = skins.rebuild_formspec_context(player, context)
local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]".. local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]"..
skins.get_skin_selection_formspec(context, -0.2) skins.get_skin_selection_formspec(player, context, -0.2)
player:set_attribute('skinsdb_unified_inventory_context', minetest.serialize(context))
return formspec return formspec
end end
@ -33,7 +30,6 @@ unified_inventory.register_page("skins_page", {
-- click button handlers -- click button handlers
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.skins then if fields.skins then
player:set_attribute('skinsdb_unified_inventory_context',"") --reset context
unified_inventory.set_inventory_formspec(player, "craft") unified_inventory.set_inventory_formspec(player, "craft")
return return
end end
@ -42,23 +38,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return return
end end
local context -- read context only if skins related action local context = skins.ui_context[player:get_player_name()]
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 action = skins.on_skin_selection_receive_fields(player, context, fields) local action = skins.on_skin_selection_receive_fields(player, context, fields)
if action == 'set' then if action == 'set' then
player:set_attribute('skinsdb_unified_inventory_context',"") --reset context
unified_inventory.set_inventory_formspec(player, "skins") unified_inventory.set_inventory_formspec(player, "skins")
elseif action == 'page' then elseif action == 'page' then
player:set_attribute('skinsdb_unified_inventory_context', minetest.serialize(context))
unified_inventory.set_inventory_formspec(player, "skins_page") unified_inventory.set_inventory_formspec(player, "skins_page")
end end
end) end)