hud.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local S = protector.intllib
  2. local radius = (tonumber(minetest.setting_get("protector_radius")) or 5)
  3. local hud = {}
  4. local hud_timer = 0
  5. local hud_interval = (tonumber(minetest.setting_get("protector_hud_interval")) or 5)
  6. if hud_interval > 0 then
  7. minetest.register_globalstep(function(dtime)
  8. -- every 5 seconds
  9. hud_timer = hud_timer + dtime
  10. if hud_timer < hud_interval then
  11. return
  12. end
  13. hud_timer = 0
  14. for _, player in pairs(minetest.get_connected_players()) do
  15. local name = player:get_player_name()
  16. local pos = vector.round(player:get_pos())
  17. local hud_text = ""
  18. local protectors = minetest.find_nodes_in_area(
  19. {x = pos.x - radius , y = pos.y - radius , z = pos.z - radius},
  20. {x = pos.x + radius , y = pos.y + radius , z = pos.z + radius},
  21. {"protector:protect","protector:protect2", "protector:protect_hidden"})
  22. if #protectors > 0 then
  23. local npos = protectors[1]
  24. local meta = minetest.get_meta(npos)
  25. local nodeowner = meta:get_string("owner")
  26. hud_text = S("Owner: @1", nodeowner)
  27. end
  28. if not hud[name] then
  29. hud[name] = {}
  30. hud[name].id = player:hud_add({
  31. hud_elem_type = "text",
  32. name = "Protector Area",
  33. number = 0xFFFF22,
  34. position = {x = 0, y = 0.95},
  35. offset = {x = 8, y = -8},
  36. text = hud_text,
  37. scale = {x = 200, y = 60},
  38. alignment = {x = 1, y = -1},
  39. })
  40. return
  41. else
  42. player:hud_change(hud[name].id, "text", hud_text)
  43. end
  44. end
  45. end)
  46. minetest.register_on_leaveplayer(function(player)
  47. hud[player:get_player_name()] = nil
  48. end)
  49. end