playerlist/init.lua

64 lines
1.7 KiB
Lua
Raw Normal View History

2021-03-01 09:31:25 +01:00
playerlist = {
2021-03-01 09:19:38 +01:00
huds = {}
}
2020-10-06 10:21:03 +02:00
local playerlist_key = minetest.settings:get("playerlist_key")
if playerlist_key == "" then
playerlist_key = "sneak"
end
2021-03-26 10:39:24 +01:00
2021-03-01 09:19:38 +01:00
function playerlist.iterator()
return ipairs(minetest.get_connected_players())
end
function playerlist.count()
return #minetest.get_connected_players()
end
controls.register_on_press(function(user, key)
2021-03-26 10:39:24 +01:00
if key == playerlist_key and user then
2021-03-01 09:19:38 +01:00
local user_name = user:get_player_name()
local huds = {user:hud_add({
hud_elem_type = "image",
position = {x = 0.5, y = 0},
offset = {x = 0, y = 20},
text = "playerlist_background.png",
alignment = {x = 0, y = 1},
scale = {x = 400, y = playerlist.count() * 18 + 8},
number = 0xFFFFFF,
})}
for i, player, color, text in playerlist.iterator() do
local name = player:get_player_name()
2021-03-26 10:39:24 +01:00
local ping = math.max(1, math.ceil(4 - minetest.get_player_information(name).avg_rtt * 50))
2021-03-01 09:19:38 +01:00
table.insert(huds, user:hud_add({
2020-10-06 10:21:03 +02:00
hud_elem_type = "text",
position = {x = 0.5, y = 0},
2021-03-01 09:19:38 +01:00
offset = {x = 0, y = 23 + (i - 1) * 18},
text = text or name,
alignment = {x = 0, y = 1},
2020-10-06 10:21:03 +02:00
scale = {x = 100, y = 100},
2021-03-01 09:19:38 +01:00
number = color or 0xFFFFFF,
}))
table.insert(huds, user:hud_add({
2020-10-06 10:21:03 +02:00
hud_elem_type = "image",
position = {x = 0.5, y = 0},
2021-03-01 09:19:38 +01:00
offset = {x = -195, y = 20 + (i - 1) * 18},
2020-10-06 10:21:03 +02:00
text = "server_ping_" .. ping .. ".png",
2021-03-01 09:19:38 +01:00
alignment = {x = 1, y = 1},
2020-10-06 10:21:03 +02:00
scale = {x = 1.5, y = 1.5},
number = 0xFFFFFF,
2021-03-01 09:19:38 +01:00
}))
2020-10-06 10:21:03 +02:00
end
2021-03-01 09:19:38 +01:00
playerlist.huds[user_name] = huds
2020-10-06 10:21:03 +02:00
end
end)
2021-03-01 09:19:38 +01:00
controls.register_on_release(function(user, key)
2021-03-26 10:39:24 +01:00
if key == playerlist_key and user then
2021-03-01 09:19:38 +01:00
for _, id in pairs(playerlist.huds[user:get_player_name()]) do
user:hud_remove(id)
2020-10-06 10:21:03 +02:00
end
end
end)
2021-03-26 10:39:24 +01:00