Version 0.41.3
[MineClone/MineClone2/MineClone2-Fixes.git] / mods / HUD / show_wielded_item / init.lua
blobaddd92e0ec03821ca770fbd9bc4a24a0d662c53a
1 -- Based on 4itemnames mod by 4aiman
3 local wield = {}
4 local wieldindex = {}
5 local huds = {}
6 local dtimes = {}
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"))
16 local off
17 if fixed and fixed ~= -1 then
18 -- Manual offset
19 off = {x=0, y=-fixed}
20 else
21 -- Default offset
22 off = {x=0, y=-101}
24 if hudbars_mod 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
30 end
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
34 off.y = -181
35 end
36 end
38 huds[player_name] = player:hud_add({
39 hud_elem_type = "text",
40 position = {x=0.5, y=1},
41 offset = off,
42 alignment = {x=0, y=0},
43 number = 0xFFFFFF ,
44 text = "",
46 end
48 minetest.register_on_joinplayer(function(player)
49 set_hud(player)
51 local name = player:get_player_name()
52 wield[name] = player:get_wielded_item():get_name()
53 wieldindex[name] = player:get_wield_index()
54 end)
56 minetest.register_on_leaveplayer(function(player)
57 local name = player:get_player_name()
58 wield[name] = nil
59 wieldindex[name] = nil
60 end)
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', "")
73 end
74 end
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
90 * itemstring ]]
91 local desc = meta:get_string("description")
92 if (desc == nil or desc == "") and def then
93 desc = def.description
94 end
95 if desc == nil or desc == "" then
96 desc = wname
97 end
98 -- Cut off item description after first newline
99 local firstnewline = string.find(desc, "\n")
100 if firstnewline then
101 desc = string.sub(desc, 1, firstnewline-1)
103 player:hud_change(huds[player_name], 'text', desc)
107 end)