mtimer/system/on_receive_fields.lua

63 lines
1.9 KiB
Lua
Raw Normal View History

2019-02-16 16:45:32 +01:00
local m = mtimer
local f = mtimer.show_formspec
minetest.register_on_player_receive_fields(function(player, formname, fields)
local meta = player:get_meta()
local name = player:get_player_name()
-- Select what formspec to show basing on main menu button
if formname == 'mtimer:main_menu' then
if fields.set_visibility then f.set_visibility(name) end
2019-02-17 01:31:10 +01:00
if fields.set_position then f.set_position(name) end
2019-02-17 03:10:41 +01:00
if fields.set_color then f.set_color(name) end
2019-02-16 16:45:32 +01:00
end
-- Set timer visibility
if formname == 'mtimer:set_visibility' then
local attr = m.meta.visible
if fields.visible then meta:set_string(attr.key, 'true') end
if fields.invisible then meta:set_string(attr.key, 'false') end
if fields.default then meta:set_string(attr.key, attr.default) end
end
2019-02-17 01:31:10 +01:00
-- Set timer position
if formname == 'mtimer:set_position' then
local attr = m.meta.position
for p,_ in pairs(fields) do
if p == 'default' then
meta:set_string(attr.key, attr.default)
elseif p:gsub('_.*', '') == 'pos' then
local new_pos = p:gsub('pos_', '')
if new_pos ~= 'xx' then meta:set_string(attr.key, new_pos) end
end
end
end
2019-02-17 03:10:41 +01:00
-- Set timer text color
if formname == 'mtimer:set_color' then
local attr = m.meta.color
local color = ''
if fields.color then
local valid = fields.color:match('^#'..('[0-9a-fA-F]'):rep(8)..'$')
local color = valid and fields.color or attr.default
meta:set_string(attr.key, color)
end
if fields.default then meta:set_string(attr.key, attr.default) end
if not fields.quit then mtimer.show_formspec.set_color(name) end
end
2019-02-16 16:45:32 +01:00
-- Back to menu from all formspecs
2019-02-16 17:33:16 +01:00
if fields.back then f.main_menu(name) end
2019-02-16 16:45:32 +01:00
-- DEBUG: Print all player meta data
print(dump(meta:to_table()))
end)