1 -- Based on 4itemnames mod by 4aiman
7 local dlimit
= 3 -- HUD element will be hidden after this many seconds
9 local hudbars_mod
= minetest
.get_modpath("hudbars")
11 local function set_hud(player
)
12 if not player
:is_player() then return end
13 local player_name
= player
:get_player_name()
14 -- Fixed offset in config file
15 local fixed
= tonumber(minetest
.settings
:get("show_wielded_item_y_offset"))
17 if fixed
and fixed
~= -1 then
25 -- Tweak offset if hudbars mod was found
27 local rows
= math
.floor((#hb
.get_hudbar_identifiers()-1) / 2) + 1
28 local vmargin
= tonumber(minetest
.settings
:get("hudbars_vmargin")) or 28
29 off
.y
= -76 - vmargin
*rows
32 -- Dirty trick to avoid collision with Minetest's status text (e.g. “Volume changed to 0%”)
33 if off
.y
>= -167 and off
.y
<= -156 then
38 huds
[player_name
] = player
:hud_add({
39 hud_elem_type
= "text",
40 position
= {x
=0.5, y
=1},
42 alignment
= {x
=0, y
=0},
48 minetest
.register_on_joinplayer(function(player
)
51 local name
= player
:get_player_name()
52 wield
[name
] = player
:get_wielded_item():get_name()
53 wieldindex
[name
] = player
:get_wield_index()
56 minetest
.register_on_leaveplayer(function(player
)
57 local name
= player
:get_player_name()
59 wieldindex
[name
] = nil
62 minetest
.register_globalstep(function(dtime
)
63 for _
, player
in pairs(minetest
.get_connected_players()) do
64 local player_name
= player
:get_player_name()
65 local wstack
= player
:get_wielded_item()
66 local wname
= wstack
:get_name()
67 local windex
= player
:get_wield_index()
69 if dtimes
[player_name
] and dtimes
[player_name
] < dlimit
then
70 dtimes
[player_name
] = dtimes
[player_name
] + dtime
71 if dtimes
[player_name
] > dlimit
and huds
[player_name
] then
72 player
:hud_change(huds
[player_name
], 'text', "")
76 -- Update HUD when wielded item or wielded index changed
77 if wname
~= wield
[player_name
] or windex
~= wieldindex
[player_name
] then
78 wieldindex
[player_name
] = windex
79 wield
[player_name
] = wname
80 dtimes
[player_name
] = 0
82 if huds
[player_name
] then
84 local def
= minetest
.registered_items
[wname
]
85 local meta
= wstack
:get_meta()
87 --[[ Get description. Order of preference:
88 * description from metadata
89 * description from item definition
91 local desc
= meta
:get_string("description")
92 if (desc
== nil or desc
== "") and def
then
93 desc
= def
.description
95 if desc
== nil or desc
== "" then
98 -- Cut off item description after first newline
99 local firstnewline
= string.find(desc
, "\n")
101 desc
= string.sub(desc
, 1, firstnewline
-1)
103 player
:hud_change(huds
[player_name
], 'text', desc
)