playerlist/init.lua

64 lines
1.9 KiB
Lua
Raw Permalink Normal View History

2021-03-01 08:31:25 +00:00
playerlist = {
2021-03-01 08:19:38 +00:00
huds = {}
}
2020-10-06 08:21:03 +00:00
2021-10-12 12:08:16 +00:00
local playerlist_key = minetest.settings:get("playerlist_key") or "sneak"
2021-03-26 09:39:24 +00:00
2021-03-01 08:19:38 +00: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 09:39:24 +00:00
if key == playerlist_key and user then
2021-03-01 08:19:38 +00: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-10-12 12:08:16 +00:00
local ping = math.max(1, math.ceil(4 - (minetest.get_player_information(name).avg_rtt or 0) * 50))
2022-02-06 19:59:31 +00:00
local color = color or ctf_colors.get_color(ctf.player(name)).hex
2021-03-01 08:19:38 +00:00
table.insert(huds, user:hud_add({
2020-10-06 08:21:03 +00:00
hud_elem_type = "text",
position = {x = 0.5, y = 0},
2021-03-01 08:19:38 +00:00
offset = {x = 0, y = 23 + (i - 1) * 18},
text = text or name,
alignment = {x = 0, y = 1},
2020-10-06 08:21:03 +00:00
scale = {x = 100, y = 100},
2021-03-01 08:19:38 +00:00
number = color or 0xFFFFFF,
}))
table.insert(huds, user:hud_add({
2020-10-06 08:21:03 +00:00
hud_elem_type = "image",
position = {x = 0.5, y = 0},
2021-03-01 08:19:38 +00:00
offset = {x = -195, y = 20 + (i - 1) * 18},
2020-10-06 08:21:03 +00:00
text = "server_ping_" .. ping .. ".png",
2021-03-01 08:19:38 +00:00
alignment = {x = 1, y = 1},
2020-10-06 08:21:03 +00:00
scale = {x = 1.5, y = 1.5},
number = 0xFFFFFF,
2021-03-01 08:19:38 +00:00
}))
2020-10-06 08:21:03 +00:00
end
2021-03-01 08:19:38 +00:00
playerlist.huds[user_name] = huds
2020-10-06 08:21:03 +00:00
end
end)
2021-03-01 08:19:38 +00:00
controls.register_on_release(function(user, key)
2021-03-26 09:39:24 +00:00
if key == playerlist_key and user then
2021-03-01 08:19:38 +00:00
for _, id in pairs(playerlist.huds[user:get_player_name()]) do
user:hud_remove(id)
2020-10-06 08:21:03 +00:00
end
end
end)
2021-03-26 09:39:24 +00:00
2022-03-04 21:29:41 +00:00
if minetest.get_modpath("random_messages") then
random_messages.add_message({"", "Use your " .. playerlist_key .. " key to show a list of all online players."})
end