2017-05-25 11:05:42 +02:00
|
|
|
_xtend.i()
|
2017-05-05 22:31:24 +02:00
|
|
|
|
|
|
|
|
2017-02-09 17:03:52 +01:00
|
|
|
-- Convert a given x,y string into a table {x=given_x, y=given_y} and return
|
|
|
|
-- it to be used of the screen location parameters in the HUD definition
|
|
|
|
local _xtimer_location_value = function (o)
|
|
|
|
local x = o:gsub(',[^,]+$', '')
|
|
|
|
local y = o:gsub('^[^,]+,', '')
|
|
|
|
return {x=x, y=y}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-05-25 12:15:46 +02:00
|
|
|
local interval = _xtend.g('xtimer_update_interval')
|
|
|
|
local placeholder = _xtend.g('xtimer_placeholder')
|
|
|
|
local font_color = _xtend.g('xtimer_font_color')
|
|
|
|
|
|
|
|
local format = {
|
|
|
|
locale = _xtend.g('xtimer_locale'),
|
|
|
|
start = _xtend.g('xtimer_session_start'),
|
|
|
|
runtime = _xtend.g('xtimer_session_runtime'),
|
|
|
|
current = _xtend.g('xtimer_current_time'),
|
|
|
|
ingame = _xtend.g('xtimer_ingame_time'),
|
|
|
|
output = _xtend.g('xtimer_output_format')
|
|
|
|
}
|
|
|
|
|
|
|
|
local location = {
|
2017-02-09 17:03:52 +01:00
|
|
|
position = _xtimer_location_value(_xtend.g('xtimer_position')),
|
|
|
|
alignment = _xtimer_location_value(_xtend.g('xtimer_alignment')),
|
|
|
|
offset = _xtimer_location_value(_xtend.g('xtimer_offset'))
|
|
|
|
}
|
|
|
|
|
2017-05-25 12:15:46 +02:00
|
|
|
local playerData = {}
|
|
|
|
|
2017-02-09 17:03:52 +01:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(1, function(player)
|
2017-02-27 20:00:37 +01:00
|
|
|
local playerName = player:get_player_name()
|
2017-02-28 19:38:24 +01:00
|
|
|
local _hudID = player:hud_add({
|
2017-02-09 17:03:52 +01:00
|
|
|
hud_elem_type = 'text',
|
2017-05-25 12:15:46 +02:00
|
|
|
position = location.position,
|
|
|
|
alignment = location.alignment,
|
|
|
|
offset = location.offset,
|
|
|
|
text = placeholder,
|
|
|
|
number = '0x'..font_color
|
2017-02-09 17:03:52 +01:00
|
|
|
})
|
2017-05-25 12:15:46 +02:00
|
|
|
playerData[playerName] = {
|
2017-02-28 19:38:24 +01:00
|
|
|
startTime = os.time(),
|
|
|
|
hudID = _hudID
|
|
|
|
}
|
|
|
|
minetest.after(5, _xtimer_changeText, player)
|
2017-02-09 17:03:52 +01:00
|
|
|
end, player)
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
2017-02-28 19:38:24 +01:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local playerName = player:get_player_name()
|
2017-05-25 12:15:46 +02:00
|
|
|
playerData[playerName] = nil
|
2017-02-28 19:38:24 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
function _xtimer_changeText(player)
|
2017-03-01 18:33:29 +01:00
|
|
|
local playerName = player:get_player_name()
|
|
|
|
if playerName == '' then return end
|
|
|
|
|
2017-02-09 17:03:52 +01:00
|
|
|
-- Save the game-detected locale. Fun fact: changing the locale on runtime
|
|
|
|
-- from within a mod causes the UI to glitch out completely.
|
|
|
|
local currentLocale = os.setlocale(nil)
|
2017-05-25 12:15:46 +02:00
|
|
|
os.setlocale(format.locale)
|
2017-02-09 17:03:52 +01:00
|
|
|
|
2017-05-25 12:15:46 +02:00
|
|
|
local startTime = playerData[playerName].startTime
|
|
|
|
local hudID = playerData[playerName].hudID
|
2017-02-28 19:38:24 +01:00
|
|
|
|
2017-02-09 17:03:52 +01:00
|
|
|
local time = 24*60*minetest.get_timeofday()
|
|
|
|
local h = tostring((math.floor(time/60) % 60))
|
|
|
|
local m = tostring((math.floor(time) % 60))
|
|
|
|
|
2017-05-25 12:15:46 +02:00
|
|
|
local res = format.output:gsub('(+.)', {
|
|
|
|
['+s'] = os.date(format.start, startTime),
|
|
|
|
['+c'] = os.date(format.current),
|
|
|
|
['+r'] = os.date('!'..format.runtime:gsub('(+.)', {
|
2017-02-09 17:03:52 +01:00
|
|
|
['+h'] = '%H',
|
|
|
|
['+m'] = '%M',
|
|
|
|
['+s'] = '%S'
|
2017-02-28 19:38:24 +01:00
|
|
|
}), os.time() - startTime),
|
2017-05-25 12:15:46 +02:00
|
|
|
['+i'] = format.ingame:gsub('(+.)', {
|
2017-02-09 17:03:52 +01:00
|
|
|
['+h'] = string.rep('0', 2-#h)..h,
|
|
|
|
['+m'] = string.rep('0', 2-#m)..m
|
|
|
|
}),
|
|
|
|
['+n'] = "\n"
|
|
|
|
})
|
|
|
|
|
|
|
|
os.setlocale(currentLocale) -- Restore game-detected locale
|
|
|
|
|
2017-02-28 19:38:24 +01:00
|
|
|
player:hud_change(hudID, 'text', res)
|
2017-05-25 12:15:46 +02:00
|
|
|
minetest.after(interval, _xtimer_changeText, player)
|
2017-02-09 17:03:52 +01:00
|
|
|
end
|