forked from minetest-mods/areas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hud.lua
100 lines (85 loc) · 2.64 KB
/
hud.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- This is inspired by the landrush mod by Bremaweb
local S = areas.S
areas.hud = {}
local vround = vector.round
local tconcat, tinsert = table.concat, table.insert
local sub8 = utf8.sub
local creative_mode = minetest.settings:get_bool("creative_mode")
local function trim_area_name(name)
return sub8(name, 1, areas.config.max_area_name_length)
end
local function createAreaString(area, id)
local parts = {"🛡️ ", trim_area_name(area.name), " [", id, "] (", area.owner, ")"}
if area.prev_owner then
tinsert(parts, 3, " " .. S("(by @1)", area.prev_owner))
end
if area.open then
tinsert(parts, " [" .. S("Open") .. "]")
end
if not creative_mode then
if areas.config.pvp_by_default then
-- Compare with false as nil = default
if area.canPvP == nil or area.canPvP == false then
tinsert(parts, " [" .. S("PvP disabled") .. "]")
end
elseif area.canPvP then
tinsert(parts, " [" .. S("PvP enabled") .. "]")
end
end
return tconcat(parts)
end
local function updateHud(player, name, pos)
local areaStrings, getAreasAtPos = {}, areas:getAreasAtPos(pos)
if next(getAreasAtPos) then
-- tinsert(areaStrings, S("Areas:"))
for id, area in pairs(getAreasAtPos) do
tinsert(areaStrings, createAreaString(area, id))
if #areaStrings == 10 then break end
end
end
for _, area in pairs(areas:getExternalHudEntries(pos)) do
local str = ""
if area.name then str = area.name .. " " end
if area.id then str = str .. "[" .. area.id .. "] " end
if area.owner then str = str .. "(" .. area.owner .. ")" end
tinsert(areaStrings, str)
end
if areas.invite_code then
tinsert(areaStrings, areas.invite_code)
end
local areaString = tconcat(areaStrings, "\n")
local hud = areas.hud[name]
if not hud then
hud = {
areasId = player:hud_add({
hud_elem_type = "text",
name = "Areas",
number = 0xFFFFFF,
position = {x = 0, y = 1},
offset = {x = 8, y = -8},
scale = {x = 200, y = 60},
alignment = {x = 1, y = -1},
text = areaString
}),
oldAreas = areaString
}
areas.hud[name] = hud
elseif hud.oldAreas ~= areaString then
player:hud_change(hud.areasId, "text", areaString)
hud.oldAreas = areaString
end
end
minetest.register_playerstep(function(_, playernames)
for _, name in ipairs(playernames) do
local player = minetest.get_player_by_name(name)
if player and player:is_player() then
local pos = vround(player:get_pos())
if minetest.is_valid_pos(pos) then
updateHud(player, name, pos)
end
end
end
end, true) -- Force this callback to run every step to display actual information
minetest.register_on_leaveplayer(function(player)
areas.hud[player:get_player_name()] = nil
end)