-
Notifications
You must be signed in to change notification settings - Fork 1
/
hud.lua
111 lines (92 loc) · 2.68 KB
/
hud.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- player => { name => id }
local hud_data = {}
local hud_position = { x = 0.1, y = 0.9 }
local function setup_hud(player)
local data = {}
data.img = player:hud_add({
[minetest.features.hud_def_type_field and "type" or "hud_elem_type"] = "image",
position = hud_position,
text = "mapsync_loaded.png",
offset = {x = 0, y = 0},
alignment = { x = -1, y = 0},
scale = {x = 2, y = 2}
})
data.text = player:hud_add({
[minetest.features.hud_def_type_field and "type" or "hud_elem_type"] = "text",
position = hud_position,
number = 0x00ff00,
text = "",
offset = {x = 0, y = -8},
alignment = { x = 1, y = 0},
scale = {x = 2, y = 2}
})
data.text2 = player:hud_add({
[minetest.features.hud_def_type_field and "type" or "hud_elem_type"] = "text",
position = hud_position,
number = 0x00ff00,
text = "",
offset = {x = 0, y = 8},
alignment = { x = 1, y = 0},
scale = {x = 2, y = 2}
})
hud_data[player:get_player_name()] = data
end
local function update_player_hud(player)
local playername = player:get_player_name()
local data = hud_data[playername]
if not data then
return
end
local ppos = player:get_pos()
local chunk_pos = mapsync.get_chunkpos(ppos)
local backend = mapsync.select_backend(chunk_pos)
local txt = "Mapsync: "
if mapsync.autosave then
txt = txt .. "autosaving"
player:hud_change(data.img, "text", "mapsync_autosave.png")
else
txt = txt .. "ready"
player:hud_change(data.img, "text", "mapsync_loaded.png")
end
-- provide some info about the current chunk and available backend
local txt2 = string.format("Chunk: %s, Backend: ", minetest.pos_to_string(chunk_pos))
if backend then
txt2 = txt2 .. string.format("'%s' [%s]", backend.name, backend.type)
else
txt2 = txt2 .. "<none>"
end
if backend then
-- backend available
player:hud_change(data.text2, "number", 0x00FF00)
else
-- no backend available
player:hud_change(data.text2, "number", 0xFF0000)
end
player:hud_change(data.text, "text", txt)
player:hud_change(data.text2, "text", txt2)
end
-- init
local function update_hud()
for _, player in ipairs(minetest.get_connected_players()) do
if minetest.check_player_privs(player, "mapsync") then
update_player_hud(player)
end
end
minetest.after(0.2, update_hud)
end
minetest.after(1, update_hud)
minetest.register_on_joinplayer(function(player)
if minetest.check_player_privs(player, "mapsync") then
setup_hud(player)
update_player_hud(player)
end
end)
-- priv updates
if minetest.register_on_priv_grant then
minetest.register_on_priv_grant(function(name, granter, priv)
local player = minetest.get_player_by_name(name)
if priv == "mapsync" and player and granter then
setup_hud(player)
end
end)
end