item_names.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- Based on 4itemnames mod by 4aiman
  2. local wield = {}
  3. local huds = {}
  4. local dtimes = {}
  5. local dlimit = 3 -- HUD element will be hidden after this many seconds
  6. local air_hud_mod = minetest.get_modpath("4air")
  7. local hud_mod = minetest.get_modpath("hud")
  8. local hudbars_mod = minetest.get_modpath("hudbars")
  9. local function set_hud(player)
  10. local player_name = player:get_player_name()
  11. local off = {x=0, y=-70}
  12. if air_hud_mod or hud_mod then
  13. off.y = off.y - 20
  14. elseif hudbars_mod then
  15. off.y = off.y + 13
  16. end
  17. huds[player_name] = player:hud_add({
  18. hud_elem_type = "text",
  19. position = {x=0.5, y=1},
  20. offset = off,
  21. alignment = {x=0, y=0},
  22. number = 0xFFFFFF ,
  23. text = "",
  24. })
  25. end
  26. minetest.register_on_joinplayer(function(player)
  27. minetest.after(0, set_hud, player)
  28. end)
  29. minetest.register_globalstep(function(dtime)
  30. for _, player in pairs(minetest.get_connected_players()) do
  31. local player_name = player:get_player_name()
  32. local wstack = player:get_wielded_item():get_name()
  33. if dtimes[player_name] and dtimes[player_name] < dlimit then
  34. dtimes[player_name] = dtimes[player_name] + dtime
  35. if dtimes[player_name] > dlimit and huds[player_name] then
  36. player:hud_change(huds[player_name], 'text', "")
  37. end
  38. end
  39. if wstack ~= wield[player_name] then
  40. wield[player_name] = wstack
  41. dtimes[player_name] = 0
  42. if huds[player_name] then
  43. local def = minetest.registered_items[wstack]
  44. local desc = def and def.description or ""
  45. player:hud_change(huds[player_name], 'text', desc)
  46. end
  47. end
  48. end
  49. end)