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