mirror of
https://github.com/pyrollo/display_modpack.git
synced 2024-08-19 20:24:06 +02:00
Code simplification and securisation
This commit is contained in:
parent
b8357a505c
commit
641dddfd51
@ -19,67 +19,37 @@
|
|||||||
|
|
||||||
local modname = minetest.get_current_modname()
|
local modname = minetest.get_current_modname()
|
||||||
|
|
||||||
-- Context management functions (surely many improvements to do)
|
|
||||||
|
|
||||||
local contexts = {}
|
local contexts = {}
|
||||||
|
|
||||||
local function get_player_name(player)
|
|
||||||
if type(player) == 'string' then return player end
|
|
||||||
if type(player) == 'userdata' and player.get_player_name then
|
|
||||||
return player:get_player_name()
|
|
||||||
end
|
|
||||||
minetest.log('warning', '['..modname..'] get_player_name could not identify player.')
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
local playername = get_player_name(player)
|
if minetest.is_player(player) then
|
||||||
if playername then contexts[playername] = nil end
|
contexts[player:get_player_name()] = nil
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function new_context(player, context)
|
local function get_context(playername)
|
||||||
local playername = get_player_name(player)
|
if not contexts[playername] then
|
||||||
if playername then
|
contexts[playername] = { playername = playername }
|
||||||
contexts[playername] = context
|
|
||||||
contexts[playername].playername = playername
|
|
||||||
return contexts[playername]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_context(player)
|
|
||||||
local playername = get_player_name(player)
|
|
||||||
if playername then
|
|
||||||
if contexts[playername] then
|
|
||||||
return contexts[playername]
|
|
||||||
else
|
|
||||||
minetest.log('warning', '['..modname..'] Context not found for player "'..playername..'"')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function update_context(player, changes)
|
|
||||||
local playername = get_player_name(player)
|
|
||||||
if playername then
|
|
||||||
if not contexts[playername] then
|
|
||||||
contexts[playername] = { playername = playername }
|
|
||||||
end
|
|
||||||
for key, value in pairs(changes) do
|
|
||||||
contexts[playername][key] = value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
return contexts[playername]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Show node formspec functions
|
-- Show node formspec functions
|
||||||
|
local function show_node_formspec(playername, pos)
|
||||||
local function show_node_formspec(player, pos)
|
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local playername = get_player_name(player)
|
|
||||||
|
|
||||||
-- Decontextualize formspec
|
-- Decontextualize formspec
|
||||||
local fs = meta:get_string('formspec')
|
local fs = meta:get_string('formspec')
|
||||||
|
|
||||||
|
if not fs then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- Change context and currrent_name references to nodemeta references
|
-- Change context and currrent_name references to nodemeta references
|
||||||
fs = fs:gsub("current_name", "nodemeta:"..pos.x..","..pos.y..","..pos.z)
|
-- Change context and currrent_name references to nodemeta references
|
||||||
fs = fs:gsub("context", "nodemeta:"..pos.x..","..pos.y..","..pos.z)
|
local nodemeta = string.format("nodemeta:%i,%i,%i", pos.x, pos.y ,pos.z)
|
||||||
|
fs = fs:gsub("current_name", nodemeta)
|
||||||
|
fs = fs:gsub("context", nodemeta)
|
||||||
|
|
||||||
-- Change all ${} to their corresponding metadata values
|
-- Change all ${} to their corresponding metadata values
|
||||||
local s, e
|
local s, e
|
||||||
@ -92,44 +62,44 @@ local function show_node_formspec(player, pos)
|
|||||||
end
|
end
|
||||||
until s == nil
|
until s == nil
|
||||||
|
|
||||||
|
local context = get_context(playername)
|
||||||
|
context.node_pos = pos
|
||||||
|
|
||||||
-- Find node on_receive_fields
|
-- Find node on_receive_fields
|
||||||
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
||||||
|
|
||||||
if ndef and ndef.on_receive_fields then
|
if ndef and ndef.on_receive_fields then
|
||||||
update_context(player, { on_receive_fields = ndef.on_receive_fields } )
|
context.on_receive_fields = ndef.on_receive_fields
|
||||||
end
|
end
|
||||||
update_context(player, { node_pos = pos } )
|
|
||||||
|
|
||||||
-- Show formspec
|
-- Show formspec
|
||||||
minetest.show_formspec(playername, modname..':context_formspec', fs)
|
minetest.show_formspec(playername, modname..':context_formspec', fs)
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
if formname == modname..':context_formspec' then
|
if formname ~= modname..':context_formspec' then
|
||||||
local context = get_context(player)
|
return
|
||||||
if context == nil then return end
|
|
||||||
|
|
||||||
if context.on_receive_fields then
|
|
||||||
context.on_receive_fields(context.pos, '', fields, player)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not minetest.is_player(player) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local context = get_context(player:get_player_name())
|
||||||
|
if context.on_receive_fields then
|
||||||
|
context.on_receive_fields(context.pos, '', fields, player)
|
||||||
|
end
|
||||||
|
return true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Specific functions
|
-- Specific functions
|
||||||
|
|
||||||
local function font_list_prepare()
|
local function show_font_formspec(playername)
|
||||||
local list = {}
|
local context = get_context(playername)
|
||||||
|
local fonts = {}
|
||||||
for name, _ in pairs(font_api.registered_fonts) do
|
for name, _ in pairs(font_api.registered_fonts) do
|
||||||
list[#list+1] = name
|
fonts[#fonts+1] = name
|
||||||
end
|
end
|
||||||
table.sort(list)
|
table.sort(fonts)
|
||||||
return list
|
|
||||||
end
|
|
||||||
|
|
||||||
local function show_fs(player)
|
|
||||||
local context = get_context(player)
|
|
||||||
if context == nil then return end
|
|
||||||
local fonts = font_list_prepare()
|
|
||||||
|
|
||||||
local fs = 'size[4,'..(#fonts + 0.8)..']'
|
local fs = 'size[4,'..(#fonts + 0.8)..']'
|
||||||
..default.gui_bg..default.gui_bg_img..default.gui_slots
|
..default.gui_bg..default.gui_bg_img..default.gui_slots
|
||||||
@ -146,26 +116,39 @@ local function show_fs(player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
if formname == modname..':font_list' then
|
if formname ~= modname..':font_list' then
|
||||||
local context = get_context(player)
|
return
|
||||||
if context == nil then return end
|
|
||||||
|
|
||||||
if fields.quit == 'true' then
|
|
||||||
for name, _ in pairs(font_api.registered_fonts) do
|
|
||||||
if fields['font_'..name] then
|
|
||||||
local meta = minetest.get_meta(context.pos)
|
|
||||||
meta:set_string("font", name)
|
|
||||||
display_api.update_entities(context.pos)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Using after to avoid the "double close" bug
|
|
||||||
minetest.after(0, show_node_formspec, player, context.pos)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not minetest.is_player(player) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local playername = player:get_player_name()
|
||||||
|
local context = get_context(playername)
|
||||||
|
|
||||||
|
if minetest.is_protected(context.pos, playername) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
if fields.quit == 'true' then
|
||||||
|
for name, _ in pairs(font_api.registered_fonts) do
|
||||||
|
if fields['font_'..name] then
|
||||||
|
local meta = minetest.get_meta(context.pos)
|
||||||
|
meta:set_string("font", name)
|
||||||
|
display_api.update_entities(context.pos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Using after to avoid the "double close" bug
|
||||||
|
minetest.after(0, show_node_formspec, playername, context.pos)
|
||||||
|
end
|
||||||
|
return true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
function font_api.show_font_list(player, pos)
|
function font_api.show_font_list_from_pos(player, pos)
|
||||||
new_context(player, { pos = pos })
|
if minetest.is_player(player) then
|
||||||
show_fs(player)
|
local context = get_context(player:get_player_name())
|
||||||
|
context.pos = pos
|
||||||
|
show_font_formspec(player:get_player_name())
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -35,49 +35,62 @@ function signs_api.set_display_text(pos, text, font)
|
|||||||
else
|
else
|
||||||
meta:set_string("infotext", "")
|
meta:set_string("infotext", "")
|
||||||
end
|
end
|
||||||
meta:set_string("font", font)
|
if font then
|
||||||
|
meta:set_string("font", font)
|
||||||
|
end
|
||||||
display_api.update_entities(pos)
|
display_api.update_entities(pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
function signs_api.set_formspec(pos)
|
function signs_api.set_formspec(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
||||||
if ndef and ndef.display_entities
|
if ndef and ndef.display_entities
|
||||||
and ndef.display_entities["signs:display_text"] then
|
and ndef.display_entities["signs:display_text"] then
|
||||||
local maxlines = ndef.display_entities["signs:display_text"].maxlines
|
local maxlines = ndef.display_entities["signs:display_text"].maxlines
|
||||||
local formspec, formheight
|
local fs, x, y
|
||||||
|
|
||||||
if maxlines == 1 then
|
if maxlines == 1 then
|
||||||
formspec =
|
fs = "field[0.5,0.7;5.5,1;display_text;"..F("Text")..
|
||||||
"field[0.5,0.7;5.5,1;display_text;"..F("Text")..
|
";${display_text}]"
|
||||||
";${display_text}]"
|
y = 1.2
|
||||||
formheight = 2
|
|
||||||
else
|
else
|
||||||
local extralabel = ""
|
local extralabel = ""
|
||||||
if maxlines then
|
if maxlines then
|
||||||
extralabel = F(" (first %s lines only)"):format(maxlines)
|
extralabel = F(" (first %s lines only)"):format(maxlines)
|
||||||
end
|
end
|
||||||
|
|
||||||
formspec =
|
fs = "textarea[0.5,0.7;5.5,2;display_text;"..F("Text")..""..
|
||||||
"textarea[0.5,0.7;5.5,2;display_text;"..F("Text")..""..
|
|
||||||
extralabel..";${display_text}]"
|
extralabel..";${display_text}]"
|
||||||
formheight = 3
|
y = 2.4
|
||||||
end
|
end
|
||||||
|
|
||||||
formspec = formspec.."button_exit[2,"..formheight..";2,1;ok;"..
|
x = 0.2
|
||||||
|
fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_font.png;font;]"
|
||||||
|
x = x + 0.4
|
||||||
|
fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_left.png;left;]"
|
||||||
|
x = x + 0.4
|
||||||
|
fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_center.png;center;]"
|
||||||
|
x = x + 0.4
|
||||||
|
fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_right.png;right;]"
|
||||||
|
y = y + 0.7
|
||||||
|
fs = fs.."button_exit[2,"..y..";2,1;ok;"..
|
||||||
F("Write").."]"
|
F("Write").."]"
|
||||||
formheight = formheight + 1
|
y = y + 0.8
|
||||||
formspec = "size[6,"..formheight.."]"..default.gui_bg..
|
fs = "size[6,"..y.."]"..default.gui_bg..
|
||||||
default.gui_bg_img..default.gui_slots..formspec
|
default.gui_bg_img..default.gui_slots..fs
|
||||||
|
|
||||||
meta:set_string("formspec", formspec)
|
meta:set_string("formspec", fs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function signs_api.on_receive_fields(pos, formname, fields, player)
|
function signs_api.on_receive_fields(pos, formname, fields, player)
|
||||||
if not minetest.is_protected(pos, player:get_player_name()) then
|
if not minetest.is_protected(pos, player:get_player_name()) then
|
||||||
if fields and (fields.ok or fields.key_enter) then
|
if fields and (fields.ok or fields.key_enter) then
|
||||||
signs_api.set_display_text(pos, fields.display_text, fields.font)
|
signs_api.set_display_text(pos, fields.display_text)
|
||||||
|
end
|
||||||
|
if fields and (fields.font) then
|
||||||
|
signs_api.set_display_text(pos, fields.display_text)
|
||||||
|
font_api.show_font_list_from_pos(player, pos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -183,7 +196,8 @@ function signs_api.register_sign(mod, name, model)
|
|||||||
on_destruct = display_api.on_destruct,
|
on_destruct = display_api.on_destruct,
|
||||||
on_rotate = signs_api.on_rotate,
|
on_rotate = signs_api.on_rotate,
|
||||||
on_receive_fields = signs_api.on_receive_fields,
|
on_receive_fields = signs_api.on_receive_fields,
|
||||||
on_punch = function(pos, node, player, pointed_thing)
|
on_punch = function(pos, node, player, pointed_thing)
|
||||||
|
signs_api.set_formspec(pos)
|
||||||
display_api.update_entities(pos)
|
display_api.update_entities(pos)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
@ -211,7 +225,3 @@ end
|
|||||||
|
|
||||||
-- Text entity for all signs
|
-- Text entity for all signs
|
||||||
display_api.register_display_entity("signs:display_text")
|
display_api.register_display_entity("signs:display_text")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user