init.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. player_labels = player_labels or {}
  2. player_labels.modpath = minetest.get_modpath("player_labels")
  3. -- Timeout settings.
  4. player_labels.mark_timeout = 60
  5. player_labels.chat_timeout = 10
  6. player_labels.anim_timeout = 1
  7. -- State tables.
  8. player_labels.mark = player_labels.mark or {} -- Nametag display `reference` counts.
  9. player_labels.cast = player_labels.cast or {} -- Whether the player has deliberately turned off their nametag.
  10. -- Called by the rename mod to update a player's nametag when they rename themselves.
  11. -- If the player's nametag is currently displayed, this should rewrite it.
  12. -- Otherwise, we don't need to do anything, since it will be rewritten automatically
  13. -- when it is next displayed.
  14. function player_labels.update_nametag_text(pname)
  15. if not player_labels.query_nametag_onoff(pname) then
  16. -- Player's nametag is currently off.
  17. return
  18. end
  19. -- Get player.
  20. local player = minetest.get_player_by_name(pname)
  21. if not player then
  22. return
  23. end
  24. -- Update nametag.
  25. player:set_nametag_attributes({text=gdac_invis.gpn(pname)})
  26. end
  27. -- Increment nametag refcount, return new value.
  28. local refcount_increment = function(name)
  29. local count = player_labels.mark[name]
  30. count = count + 1
  31. player_labels.mark[name] = count
  32. return count
  33. end
  34. -- Decrement nametag refcount, return new value.
  35. local refcount_decrement = function(name)
  36. local count = player_labels.mark[name]
  37. count = count - 1
  38. player_labels.mark[name] = count
  39. return count
  40. end
  41. local refcount_get = function(name)
  42. return player_labels.mark[name]
  43. end
  44. local refcount_set = function(name, count)
  45. player_labels.mark[name] = count
  46. end
  47. -- Unconditionally show/hide player's nametag.
  48. local nametag_show = function(name)
  49. local obj = minetest.get_player_by_name(name)
  50. if obj and obj:is_player() then
  51. local col = {a=255, r=0, g=255, b=255}
  52. local txt = gdac_invis.gpn(obj:get_player_name())
  53. obj:set_nametag_attributes({color=col, text=txt})
  54. end
  55. end
  56. local nametag_hide = function(name)
  57. local obj = minetest.get_player_by_name(name)
  58. if obj and obj:is_player() then
  59. local col = {a=0, r=0, g=0, b=0}
  60. local txt = gdac_invis.gpn(obj:get_player_name())
  61. obj:set_nametag_attributes({color=col, text=txt})
  62. end
  63. end
  64. -- Public API function.
  65. -- Returns 'true' if the player's nametag is ON.
  66. -- Returns 'false' if the player's nametag is OFF.
  67. -- Returns 'nil' if the player's nametag state cannot be determined (player doesn't exist, or other error).
  68. -- This is called from the `bones` mod for instance, to decide whether to tell everyone about the bones.
  69. player_labels.query_nametag_onoff = function(name)
  70. assert(type(name) == "string")
  71. local res = nil
  72. if player_labels.cast[name] ~= nil then -- Stupid booleans don't mix with nil, so need explicit check.
  73. res = player_labels.cast[name]
  74. if refcount_get(name) > 0 then
  75. res = true
  76. end
  77. end
  78. return res
  79. end
  80. -- Set up default state for new players.
  81. player_labels.on_joinplayer = function(player)
  82. local pname = player:get_player_name()
  83. -- Start fresh.
  84. player_labels.cast[pname] = true
  85. player_labels.mark[pname] = 0
  86. -- Player labels are shown by default.
  87. nametag_show(pname)
  88. end
  89. -- Called when the player uses a `player labels token`.
  90. player_labels.toggle_nametag_broadcast = function(name)
  91. if player_labels.cast[name] then
  92. minetest.chat_send_player(name, "# Server: Avatar name broadcast is OFF.")
  93. player_labels.cast[name] = false
  94. player_labels.disable_nametag_broadcast(name)
  95. else
  96. minetest.chat_send_player(name, "# Server: Avatar name broadcast is ON.")
  97. player_labels.cast[name] = true
  98. player_labels.enable_nametag_broadcast(name)
  99. end
  100. end
  101. function player_labels.enable_nametag(pname)
  102. player_labels.cast[pname] = true
  103. player_labels.enable_nametag_broadcast(pname)
  104. end
  105. function player_labels.disable_nametag(pname)
  106. player_labels.cast[pname] = false
  107. player_labels.disable_nametag_broadcast(pname)
  108. end
  109. player_labels.enable_nametag_broadcast = function(name)
  110. if player_labels.cast[name] == true then
  111. nametag_show(name)
  112. end
  113. end
  114. player_labels.disable_nametag_broadcast = function(name)
  115. if player_labels.cast[name] == false then
  116. if refcount_get(name) <= 0 then
  117. nametag_hide(name)
  118. end
  119. end
  120. end
  121. -- A delayed action function, for hiding a players name (if needed) after some delay.
  122. -- Called from `minetest.after`.
  123. player_labels.on_tag_timeout = function(name)
  124. local count = refcount_decrement(name)
  125. if count <= 0 then
  126. refcount_set(name, 0)
  127. if not player_labels.cast[name] then
  128. nametag_hide(name)
  129. end
  130. end
  131. end
  132. --[[
  133. player_labels.on_anim_timeout = function(name)
  134. local object = minetest.get_player_by_name(name)
  135. if object and object:is_player() then
  136. local fr = object:get_animation()
  137. if fr.x == 221 and fr.y == 251 then -- If still bobbing head.
  138. object:set_animation({x=0, y=79}, 30, 0, true)
  139. end
  140. end
  141. end
  142. --]]
  143. -- This function is called whenever the player uses the ID token.
  144. player_labels.on_token_use = function(itemstack, user, pointed_thing)
  145. if not user then return end
  146. if not user:is_player() then return end
  147. if pointed_thing.type == "object" then
  148. local object = pointed_thing.ref
  149. if object:is_player() then
  150. local uname = user:get_player_name()
  151. local oname = object:get_player_name()
  152. if gdac_invis.is_invisible(oname) == true then return end
  153. local sex = skins.get_gender_strings(oname)
  154. minetest.chat_send_player(uname, "# Server: Player's alias is <" .. rename.gpn(oname) .. ">; " .. sex.his .. " login name is <" .. oname .. ">.")
  155. minetest.chat_send_player(oname, "# Server: Player <" .. rename.gpn(uname) .. "> identified you.")
  156. refcount_increment(oname)
  157. nametag_show(oname)
  158. minetest.after(player_labels.mark_timeout, player_labels.on_tag_timeout, oname)
  159. return
  160. end
  161. end
  162. if gdac_invis.is_invisible(user:get_player_name()) == true then
  163. minetest.chat_send_player(user:get_player_name(), "# Server: Not while invisible!")
  164. easyvend.sound_error(user:get_player_name())
  165. return
  166. end
  167. player_labels.toggle_nametag_broadcast(user:get_player_name())
  168. return
  169. end
  170. -- This function gets called from another mod whenever the player sends a chat message.
  171. player_labels.on_chat_message = function(name, message)
  172. if gdac_invis.is_invisible(name) then return end
  173. local object = minetest.get_player_by_name(name)
  174. if object and object:is_player() then
  175. --object:set_animation({x=189, y=198}, 30, 0, true)
  176. --object:set_animation({x=221, y=251}, 30, 0, true)
  177. --minetest.after(player_labels.anim_timeout, player_labels.on_anim_timeout, name)
  178. refcount_increment(name)
  179. nametag_show(name)
  180. minetest.after(player_labels.chat_timeout, player_labels.on_tag_timeout, name)
  181. end
  182. end
  183. -- First-time execution only.
  184. if not player_labels.registered then
  185. minetest.register_on_joinplayer(function(...) return player_labels.on_joinplayer(...) end)
  186. minetest.register_craftitem("player_labels:show", {
  187. description = "ID Marker\n\nUse this to see someone's name, if suspicious of impersonation.\nThis can also show or hide your own name.",
  188. inventory_image = "default_copper_block.png",
  189. --range = 10, -- Disabled because this allows players to access formspecs from long range.
  190. on_use = function(...) return player_labels.on_token_use(...) end,
  191. -- Disabled because these attempts to disable right-click functionality do not appear to work.
  192. --on_rightclick = function(...) end,
  193. --on_secondary_use = function(...) end,
  194. --on_place = function(...) end,
  195. })
  196. minetest.register_craft({
  197. output = 'player_labels:show',
  198. recipe = {
  199. {"", "default:copper_ingot", ""},
  200. {"default:copper_ingot", "", "default:copper_ingot"},
  201. {"", "default:copper_ingot", ""},
  202. },
  203. })
  204. player_labels.registered = true
  205. end
  206. -- Support for reloading.
  207. if minetest.get_modpath("reload") then
  208. local c = "player_labels:core"
  209. local f = player_labels.modpath .. "/init.lua"
  210. if not reload.file_registered(c) then
  211. reload.register_file(c, f, false)
  212. end
  213. end