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