hud.lua 1.3 KB

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