2015-04-18 21:23:22 +02:00
|
|
|
-- Based on 4itemnames mod by 4aiman
|
2014-05-26 11:43:25 +02:00
|
|
|
|
2020-03-25 18:46:35 +01:00
|
|
|
local item_names = {} -- [player_name] = { hud, dtime, itemname }
|
2015-04-18 21:23:22 +02:00
|
|
|
local dlimit = 3 -- HUD element will be hidden after this many seconds
|
2015-10-02 20:01:07 +02:00
|
|
|
local hudbars_mod = minetest.get_modpath("hudbars")
|
2023-10-15 21:02:35 +02:00
|
|
|
local only_names = minetest.settings:get_bool("unified_inventory_only_names", true)
|
|
|
|
local max_length = tonumber(minetest.settings:get("unified_inventory_max_item_name_length")) or 80
|
2014-05-26 05:41:40 +02:00
|
|
|
|
2015-04-18 21:23:22 +02:00
|
|
|
local function set_hud(player)
|
2017-05-07 19:54:13 +02:00
|
|
|
local player_name = player:get_player_name()
|
2020-06-05 21:30:53 +02:00
|
|
|
local off = {x=0, y=-65}
|
|
|
|
if hudbars_mod then
|
|
|
|
-- Assume no alignment (2 per line)
|
|
|
|
off.y = off.y - math.ceil(hb.hudbars_count / 2) * 25
|
|
|
|
else
|
|
|
|
off.y = off.y - 25
|
2014-05-26 05:41:40 +02:00
|
|
|
end
|
2020-06-05 21:30:53 +02:00
|
|
|
|
2017-05-07 19:54:13 +02:00
|
|
|
item_names[player_name] = {
|
|
|
|
hud = player:hud_add({
|
2024-07-18 20:45:24 +02:00
|
|
|
-- TODO: remove compatibility code when 5.8.0 is no longer used
|
|
|
|
[minetest.features.hud_def_type_field and "type" or "hud_elem_type"] = "text",
|
2017-05-07 19:54:13 +02:00
|
|
|
position = {x=0.5, y=1},
|
|
|
|
offset = off,
|
2020-06-05 21:30:53 +02:00
|
|
|
alignment = {x=0, y=-1},
|
2017-05-07 19:54:13 +02:00
|
|
|
number = 0xFFFFFF,
|
|
|
|
text = "",
|
|
|
|
}),
|
|
|
|
dtime = dlimit,
|
|
|
|
index = 1,
|
|
|
|
itemname = ""
|
|
|
|
}
|
2015-04-18 21:23:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(0, set_hud, player)
|
2014-05-26 05:41:40 +02:00
|
|
|
end)
|
|
|
|
|
2017-05-07 19:54:13 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
item_names[player:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
2014-05-26 05:41:40 +02:00
|
|
|
minetest.register_globalstep(function(dtime)
|
2015-04-18 21:23:22 +02:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
2017-05-07 19:54:13 +02:00
|
|
|
local data = item_names[player:get_player_name()]
|
|
|
|
if not data or not data.hud then
|
|
|
|
data = {} -- Update on next step
|
|
|
|
set_hud(player)
|
|
|
|
end
|
2014-05-26 05:41:40 +02:00
|
|
|
|
2017-05-07 19:54:13 +02:00
|
|
|
local index = player:get_wield_index()
|
|
|
|
local stack = player:get_wielded_item()
|
|
|
|
local itemname = stack:get_name()
|
|
|
|
|
|
|
|
if data.hud and data.dtime < dlimit then
|
|
|
|
data.dtime = data.dtime + dtime
|
|
|
|
if data.dtime > dlimit then
|
|
|
|
player:hud_change(data.hud, 'text', "")
|
2014-05-26 05:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-07 19:54:13 +02:00
|
|
|
if data.hud and (itemname ~= data.itemname or index ~= data.index) then
|
|
|
|
data.itemname = itemname
|
|
|
|
data.index = index
|
|
|
|
data.dtime = 0
|
2023-10-15 21:02:35 +02:00
|
|
|
local lang_code = minetest.get_player_information(player:get_player_name()).lang_code
|
2017-05-07 19:54:13 +02:00
|
|
|
|
|
|
|
local desc = stack.get_meta
|
|
|
|
and stack:get_meta():get_string("description")
|
|
|
|
|
|
|
|
if not desc or desc == "" then
|
|
|
|
-- Try to use default description when none is set in the meta
|
|
|
|
local def = minetest.registered_items[itemname]
|
|
|
|
desc = def and def.description or ""
|
2014-05-26 05:41:40 +02:00
|
|
|
end
|
2023-10-15 21:02:35 +02:00
|
|
|
if only_names and desc and string.find(desc, "\n") then
|
|
|
|
desc = string.match(desc, "([^\n]*)")
|
|
|
|
end
|
|
|
|
desc = minetest.get_translated_string(lang_code, desc)
|
|
|
|
desc = minetest.strip_colors(desc)
|
|
|
|
if string.len(desc) > max_length and max_length > 0 then
|
|
|
|
desc = string.sub(desc, 1, max_length) .. " [...]"
|
|
|
|
end
|
2017-05-07 19:54:13 +02:00
|
|
|
player:hud_change(data.hud, 'text', desc)
|
2014-05-26 05:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2015-04-18 21:23:22 +02:00
|
|
|
|