hud.lua 1.3 KB

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