Code simplification and securisation

This commit is contained in:
Pierre-Yves Rollo 2018-11-18 15:29:59 +01:00
parent 8fa161c90b
commit a35b0ec7b9

@ -35,7 +35,9 @@ function signs_api.set_display_text(pos, text, font)
else else
meta:set_string("infotext", "") meta:set_string("infotext", "")
end end
if font then
meta:set_string("font", font) meta:set_string("font", font)
end
display_api.update_entities(pos) display_api.update_entities(pos)
end end
@ -45,39 +47,50 @@ function signs_api.set_formspec(pos)
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}]"
formheight = 2 y = 1.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
@ -184,6 +197,7 @@ function signs_api.register_sign(mod, name, model)
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")