]> git.lizzy.rs Git - playerlist.git/blob - init.lua
Fix crash and invalid setting default
[playerlist.git] / init.lua
1 playerlist = {
2         huds = {}
3 }
4
5 local playerlist_key = minetest.settings:get("playerlist_key") or "sneak"
6
7 function playerlist.iterator()
8         return ipairs(minetest.get_connected_players())
9 end
10
11 function playerlist.count()
12         return #minetest.get_connected_players()
13 end
14
15 controls.register_on_press(function(user, key)
16         if key == playerlist_key and user then
17                 local user_name = user:get_player_name()
18                 local huds = {user:hud_add({
19                         hud_elem_type = "image",
20                         position = {x = 0.5, y = 0},
21                         offset = {x = 0, y = 20},
22                         text = "playerlist_background.png",
23                         alignment = {x = 0, y = 1},
24                         scale = {x = 400, y = playerlist.count() * 18 + 8},
25                         number = 0xFFFFFF,
26                 })}
27                 for i, player, color, text in playerlist.iterator() do
28                         local name = player:get_player_name()
29                         local ping = math.max(1, math.ceil(4 - (minetest.get_player_information(name).avg_rtt or 0) * 50))
30                         table.insert(huds, user:hud_add({
31                                 hud_elem_type = "text",
32                                 position = {x = 0.5, y = 0},
33                                 offset = {x = 0, y = 23 + (i - 1) * 18},
34                                 text = text or name,
35                                 alignment = {x = 0, y = 1},
36                                 scale = {x = 100, y = 100},
37                                 number = color or 0xFFFFFF,
38                         }))
39                         table.insert(huds, user:hud_add({
40                                 hud_elem_type = "image",
41                                 position = {x = 0.5, y = 0},
42                                 offset = {x = -195, y = 20 + (i - 1) * 18},
43                                 text = "server_ping_" .. ping .. ".png",
44                                 alignment = {x = 1, y = 1},
45                                 scale = {x = 1.5, y = 1.5},
46                                 number = 0xFFFFFF,
47                         }))
48                 end
49                 playerlist.huds[user_name] = huds
50         end
51 end)
52
53 controls.register_on_release(function(user, key)
54         if key == playerlist_key and user then
55                 for _, id in pairs(playerlist.huds[user:get_player_name()]) do
56                         user:hud_remove(id)
57                 end
58         end
59 end)
60