Item_names: Refactorize & use meta description
This commit is contained in:
parent
4010953888
commit
86c73a4752
@ -1,8 +1,6 @@
|
|||||||
-- Based on 4itemnames mod by 4aiman
|
-- Based on 4itemnames mod by 4aiman
|
||||||
|
|
||||||
local wield = {}
|
local item_names = {} -- [player_name] = { hud, dtime, itemname }
|
||||||
local huds = {}
|
|
||||||
local dtimes = {}
|
|
||||||
local dlimit = 3 -- HUD element will be hidden after this many seconds
|
local dlimit = 3 -- HUD element will be hidden after this many seconds
|
||||||
local air_hud_mod = minetest.get_modpath("4air")
|
local air_hud_mod = minetest.get_modpath("4air")
|
||||||
local hud_mod = minetest.get_modpath("hud")
|
local hud_mod = minetest.get_modpath("hud")
|
||||||
@ -16,40 +14,62 @@ local function set_hud(player)
|
|||||||
elseif hudbars_mod then
|
elseif hudbars_mod then
|
||||||
off.y = off.y + 13
|
off.y = off.y + 13
|
||||||
end
|
end
|
||||||
huds[player_name] = player:hud_add({
|
item_names[player_name] = {
|
||||||
hud_elem_type = "text",
|
hud = player:hud_add({
|
||||||
position = {x=0.5, y=1},
|
hud_elem_type = "text",
|
||||||
offset = off,
|
position = {x=0.5, y=1},
|
||||||
alignment = {x=0, y=0},
|
offset = off,
|
||||||
number = 0xFFFFFF ,
|
alignment = {x=0, y=0},
|
||||||
text = "",
|
number = 0xFFFFFF,
|
||||||
})
|
text = "",
|
||||||
|
}),
|
||||||
|
dtime = dlimit,
|
||||||
|
index = 1,
|
||||||
|
itemname = ""
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
minetest.after(0, set_hud, player)
|
minetest.after(0, set_hud, player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
minetest.register_on_leaveplayer(function(player)
|
||||||
|
item_names[player:get_player_name()] = nil
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
for _, player in pairs(minetest.get_connected_players()) do
|
for _, player in pairs(minetest.get_connected_players()) do
|
||||||
local player_name = player:get_player_name()
|
local data = item_names[player:get_player_name()]
|
||||||
local wstack = player:get_wielded_item():get_name()
|
if not data or not data.hud then
|
||||||
|
data = {} -- Update on next step
|
||||||
|
set_hud(player)
|
||||||
|
end
|
||||||
|
|
||||||
if dtimes[player_name] and dtimes[player_name] < dlimit then
|
local index = player:get_wield_index()
|
||||||
dtimes[player_name] = dtimes[player_name] + dtime
|
local stack = player:get_wielded_item()
|
||||||
if dtimes[player_name] > dlimit and huds[player_name] then
|
local itemname = stack:get_name()
|
||||||
player:hud_change(huds[player_name], 'text', "")
|
|
||||||
|
if data.hud and data.dtime < dlimit then
|
||||||
|
data.dtime = data.dtime + dtime
|
||||||
|
if data.dtime > dlimit then
|
||||||
|
player:hud_change(data.hud, 'text', "")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if wstack ~= wield[player_name] then
|
if data.hud and (itemname ~= data.itemname or index ~= data.index) then
|
||||||
wield[player_name] = wstack
|
data.itemname = itemname
|
||||||
dtimes[player_name] = 0
|
data.index = index
|
||||||
if huds[player_name] then
|
data.dtime = 0
|
||||||
local def = minetest.registered_items[wstack]
|
|
||||||
local desc = def and def.description or ""
|
local desc = stack.get_meta
|
||||||
player:hud_change(huds[player_name], 'text', desc)
|
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 ""
|
||||||
end
|
end
|
||||||
|
player:hud_change(data.hud, 'text', desc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user