-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.lua
53 lines (43 loc) · 1.43 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
playtime = {}
local current = {}
local storage = minetest.get_mod_storage()
function playtime.get_current_playtime(name)
if current[name] then
return os.time() - current[name]
else return 0
end
end
-- Function to get playtime
function playtime.get_total_playtime(name)
return storage:get_int(name) + playtime.get_current_playtime(name)
end
function playtime.remove_playtime(name)
storage:set_string(name, "")
end
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
storage:set_int(name, storage:get_int(name) + playtime.get_current_playtime(name))
current[name] = nil
end)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
current[name] = os.time()
end)
local function SecondsToClock(seconds)
local seconds = tonumber(seconds)
if seconds <= 0 then
return "00:00:00";
else
hours = string.format("%02.f", math.floor(seconds/3600));
mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
return hours..":"..mins..":"..secs
end
end
minetest.register_chatcommand("playtime", {
params = "",
description = "Use it to get your own playtime!",
func = function(name)
minetest.chat_send_player(name, "Total: "..SecondsToClock(playtime.get_total_playtime(name)).." Current: "..SecondsToClock(playtime.get_current_playtime(name)))
end,
})