2017-03-08 20:09:46 +01:00
|
|
|
-- Based on 4itemnames mod by 4aiman
|
|
|
|
|
|
|
|
local wield = {}
|
|
|
|
local huds = {}
|
|
|
|
local dtimes = {}
|
|
|
|
local dlimit = 3 -- HUD element will be hidden after this many seconds
|
|
|
|
|
|
|
|
local function set_hud(player)
|
2017-03-08 20:18:42 +01:00
|
|
|
if not player:is_player() then return end
|
2017-03-08 20:09:46 +01:00
|
|
|
local player_name = player:get_player_name()
|
2017-03-08 20:16:41 +01:00
|
|
|
local off = {x=0, y=-136}
|
2017-03-08 20:09:46 +01:00
|
|
|
huds[player_name] = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
position = {x=0.5, y=1},
|
|
|
|
offset = off,
|
|
|
|
alignment = {x=0, y=0},
|
|
|
|
number = 0xFFFFFF ,
|
|
|
|
text = "",
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2017-03-08 20:18:42 +01:00
|
|
|
set_hud(player)
|
2017-03-08 20:09:46 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
local player_name = player:get_player_name()
|
2017-06-06 03:22:07 +02:00
|
|
|
local wstack = player:get_wielded_item()
|
|
|
|
local wname = wstack:get_name()
|
2017-03-08 20:09:46 +01:00
|
|
|
|
|
|
|
if dtimes[player_name] and dtimes[player_name] < dlimit then
|
|
|
|
dtimes[player_name] = dtimes[player_name] + dtime
|
|
|
|
if dtimes[player_name] > dlimit and huds[player_name] then
|
|
|
|
player:hud_change(huds[player_name], 'text', "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 03:22:07 +02:00
|
|
|
if wname ~= wield[player_name] then
|
|
|
|
wield[player_name] = wname
|
2017-03-08 20:09:46 +01:00
|
|
|
dtimes[player_name] = 0
|
|
|
|
if huds[player_name] then
|
2017-06-06 03:22:07 +02:00
|
|
|
local def = minetest.registered_items[wname]
|
|
|
|
local meta = wstack:get_meta()
|
|
|
|
|
|
|
|
--[[ Get description. Order of preference:
|
|
|
|
* description from metadata
|
|
|
|
* description from item definition
|
|
|
|
* itemstring ]]
|
|
|
|
local desc = meta:get_string("description")
|
|
|
|
if (desc == nil or desc == "") and def then
|
|
|
|
desc = def.description
|
|
|
|
end
|
|
|
|
if desc == nil or desc == "" then
|
|
|
|
desc = wname
|
2017-06-05 22:47:50 +02:00
|
|
|
end
|
2017-03-08 20:16:41 +01:00
|
|
|
-- Cut off item description after first newline
|
|
|
|
local firstnewline = string.find(desc, "\n")
|
|
|
|
if firstnewline then
|
|
|
|
desc = string.sub(desc, 1, firstnewline-1)
|
|
|
|
end
|
2017-03-08 20:09:46 +01:00
|
|
|
player:hud_change(huds[player_name], 'text', desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|