init.lua.old 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. player_labels = player_labels or {}
  2. player_labels.modpath = minetest.get_modpath("player_labels")
  3. player_labels.mark_timeout = 10
  4. player_labels.chat_timeout = 6
  5. player_labels.anim_timeout = 3
  6. player_labels.players = player_labels.players or {}
  7. player_labels.marks = player_labels.marks or {}
  8. player_labels.active_tags = player_labels.active_tags or {}
  9. -- Support for reloading.
  10. if minetest.get_modpath("reload") then
  11. local c = "player_labels:core"
  12. local f = player_labels.modpath .. "/init.lua"
  13. if not reload.file_registered(c) then
  14. reload.register_file(c, f, false)
  15. end
  16. end
  17. local nametag_show = function(obj)
  18. if obj and obj:is_player() then
  19. local col = {a=255, r=0, g=255, b=255}
  20. local txt = obj:get_player_name()
  21. obj:set_nametag_attributes({color=col, text=txt})
  22. end
  23. end
  24. local nametag_hide = function(obj)
  25. if obj and obj:is_player() then
  26. local col = {a=0, r=0, g=0, b=0}
  27. local txt = "!"
  28. obj:set_nametag_attributes({color=col, text=txt})
  29. end
  30. end
  31. player_labels.on_joinplayer = function(player)
  32. -- Player names are shown by default, when they first connect during this server session.
  33. if player_labels.active_tags[player:get_player_name()] == nil then
  34. nametag_show(player)
  35. player_labels.active_tags[player:get_player_name()] = true
  36. end
  37. end
  38. player_labels.calc_nametag_display = function(name)
  39. -- Ensure an entry for this name exists; this makes the rest of this function easier.
  40. if not player_labels.players[name] then
  41. player_labels.players[name] = 0
  42. end
  43. local object = minetest.get_player_by_name(name)
  44. if object and object:is_player() then
  45. local count = player_labels.players[name]
  46. if player_labels.marks[name] then
  47. count = 1
  48. end
  49. if count <= 0 then
  50. local col = {a=0, r=0, g=0, b=0}
  51. local txt = "!"
  52. object:set_nametag_attributes({color=col, text=txt})
  53. else -- Count is >= 1.
  54. local col = {a=255, r=0, g=255, b=255}
  55. local txt = name
  56. object:set_nametag_attributes({color=col, text=txt})
  57. end
  58. end
  59. end
  60. -- This function operates on the principle of reference-counting.
  61. player_labels.update_nametag_refcount = function(name, incdec)
  62. -- Ensure an entry for this name exists; this makes the rest of this function easier.
  63. if not player_labels.players[name] then
  64. player_labels.players[name] = 0
  65. end
  66. if incdec then
  67. -- Increment tag count.
  68. local count = player_labels.players[name]
  69. count = count + 1
  70. player_labels.players[name] = count
  71. else
  72. -- Decrement tag count.
  73. local count = player_labels.players[name]
  74. count = count - 1
  75. player_labels.players[name] = count
  76. end
  77. player_labels.calc_nametag_display(name)
  78. end
  79. player_labels.toggle_nametag_broadcast = function(name)
  80. if not player_labels.marks[name] then
  81. player_labels.marks[name] = true
  82. minetest.chat_send_player(name, "# Server: Avatar ID broadcast is ON.")
  83. else
  84. player_labels.marks[name] = nil
  85. minetest.chat_send_player(name, "# Server: Avatar ID broadcast is OFF.")
  86. end
  87. player_labels.calc_nametag_display(name)
  88. end
  89. player_labels.on_tag_timeout = function(name)
  90. local object = minetest.get_player_by_name(name)
  91. if object and object:is_player() and player_labels.players[name] then
  92. player_labels.update_nametag(name, false)
  93. end
  94. end
  95. player_labels.on_anim_timeout = function(name)
  96. local object = minetest.get_player_by_name(name)
  97. if object and object:is_player() then
  98. object:set_animation({x=0, y=79}, 30, 0, true)
  99. end
  100. end
  101. player_labels.on_token_use = function(itemstack, user, pointed_thing)
  102. if not user then return end
  103. if not user:is_player() then return end
  104. if pointed_thing.type == "object" then
  105. local object = pointed_thing.ref
  106. if object:is_player() then
  107. minetest.chat_send_player(user:get_player_name(), "# Server: Player's name is <" .. object:get_player_name() .. ">.")
  108. minetest.chat_send_player(object:get_player_name(), "# Server: Player <" .. user:get_player_name() .. "> identified you.")
  109. player_labels.update_nametag_refcount(object:get_player_name(), true)
  110. minetest.after(player_labels.mark_timeout, player_labels.on_tag_timeout, object:get_player_name())
  111. return itemstack
  112. end
  113. end
  114. player_labels.toggle_nametag_broadcast(user:get_player_name())
  115. return itemstack
  116. end
  117. player_labels.on_chat_message = function(name, message)
  118. local object = minetest.get_player_by_name(name)
  119. if object and object:is_player() then
  120. object:set_animation({x=189, y=198}, 30, 0, true)
  121. minetest.after(player_labels.anim_timeout, player_labels.on_anim_timeout, name)
  122. player_labels.update_nametag_refcount(name, true)
  123. minetest.after(player_labels.chat_timeout, player_labels.on_tag_timeout, name)
  124. end
  125. end
  126. -- First-time execution only.
  127. if not player_labels.registered then
  128. minetest.register_on_joinplayer(function(...) return player_labels.on_joinplayer(...) end)
  129. minetest.register_craftitem("player_labels:show", {
  130. description = "ID Marker [Use this to see someone's name, if suspicious of impersonation.]",
  131. inventory_image = "default_copper_block.png",
  132. --range = 10, -- Disabled because this allows players to access formspecs from long range.
  133. on_use = function(...) return player_labels.on_token_use(...) end,
  134. -- Disabled because these attempts to disable right-click functionality do not appear to work.
  135. --on_rightclick = function(...) end,
  136. --on_secondary_use = function(...) end,
  137. --on_place = function(...) end,
  138. })
  139. minetest.register_craft({
  140. output = 'player_labels:show',
  141. recipe = {
  142. {"", "default:copper_ingot", "" },
  143. {"default:copper_ingot", "", "default:copper_ingot"},
  144. {"", "default:copper_ingot", "" },
  145. },
  146. })
  147. player_labels.registered = true
  148. end