hud.lua 1.6 KB

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