mtimer/system/on_joinplayer.lua

47 lines
1.6 KiB
Lua
Raw Normal View History

2019-02-16 16:45:32 +01:00
local m = mtimer
2019-02-22 20:33:42 +01:00
-- When a player joins
--
-- 1. Check if a hard reset was requested and perform it if it was
-- 2. Set default values if not set
-- 3. Set session start timestamp
-- 4. Set “empty” HUD element and write ID to meta data for later use
2019-02-16 16:45:32 +01:00
minetest.register_on_joinplayer(function(player)
local meta = player:get_meta()
local name = player:get_player_name()
local re = meta:get_int('mtimer:hard_reset_everything')
2019-02-22 15:29:08 +01:00
-- Forecefully remove all meta data set in the player object that starts
-- with `mtimer:` prefix (perform the requested reset everything).
if re > 0 then
local message = m.translator('All configuration was reset. Server time of the requested: @1', os.date('%c', re))
for fname,fvalue in pairs(meta:to_table().fields) do
if fname:sub(1,7) == 'mtimer:' then
meta:set_string(fname, '')
end
end
minetest.chat_send_player(name, '[mTimer] '..message)
end
-- Set all unset metadata to their defined default values
2019-02-16 16:45:32 +01:00
for _,def in pairs(m.meta) do
local current = meta:get_string(def.key)
if current == '' then meta:set_string(def.key, def.default) end
end
2019-02-22 15:29:08 +01:00
-- Always set session start timestamp for using it within the custom timer
meta:set_string('mtimer:session_start', os.time())
2019-02-22 15:29:08 +01:00
-- Initially set empty HUD element to store the ID for updates
2019-02-22 15:29:08 +01:00
meta:set_string('mtimer:hud_id', player:hud_add({
hud_elem_type = 'text',
text = '',
number = '0x000000',
position = {x=0,y=0},
alignment = {x=0,y=0},
size = {x=0,y=0},
2019-02-22 15:29:08 +01:00
offset = {x=0,y=0}
}))
2019-02-16 16:45:32 +01:00
end)