init.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. if not minetest.global_exists("player_labels") then player_labels = {} end
  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 player_labels.query_nametag_onoff(pname) then
  16. -- Player's nametag is currently on.
  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. pova.update_modifier(player, "nametag", {text=rename.gpn(pname)}, "id_mark")
  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 = rename.gpn(obj:get_player_name())
  53. pova.remove_modifier(obj, "nametag", "id_mark")
  54. pova.remove_modifier(obj, "properties", "id_mark")
  55. end
  56. end
  57. local nametag_hide = function(name)
  58. local obj = minetest.get_player_by_name(name)
  59. if obj and obj:is_player() then
  60. local col = {a=0, r=0, g=0, b=0}
  61. local txt = ""
  62. pova.set_modifier(obj, "nametag", {color=col, text=txt}, "id_mark")
  63. pova.set_modifier(obj, "properties", {show_on_minimap = false}, "id_mark")
  64. end
  65. end
  66. -- Persist player_labels.cast state into player's metadata. The inverted state
  67. -- is stored, so that absence of the key in player's metadata makes the nametag
  68. -- visible by default.
  69. local function persist_cast(pname)
  70. local player = minetest.get_player_by_name(pname)
  71. local plmeta = player:get_meta()
  72. if player_labels.cast[pname] then
  73. plmeta:set_string("hides_nametag", "") -- Removes the key.
  74. else
  75. plmeta:set_int("hides_nametag", 1)
  76. end
  77. end
  78. -- Public API function.
  79. -- Returns 'true' if the player's nametag is ON.
  80. -- Returns 'false' if the player's nametag is OFF.
  81. -- Returns 'nil' if the player's nametag state cannot be determined (player doesn't exist, or other error).
  82. -- This is called from the `bones` mod for instance, to decide whether to tell everyone about the bones.
  83. player_labels.query_nametag_onoff = function(name)
  84. assert(type(name) == "string")
  85. local res = nil
  86. if player_labels.cast[name] ~= nil then -- Stupid booleans don't mix with nil, so need explicit check.
  87. res = player_labels.cast[name]
  88. if refcount_get(name) > 0 then
  89. res = true
  90. end
  91. end
  92. return res
  93. end
  94. -- Set up default state for new players.
  95. player_labels.on_joinplayer = function(player)
  96. local pname = player:get_player_name()
  97. -- Start fresh.
  98. player_labels.cast[pname] = true
  99. player_labels.mark[pname] = 0
  100. -- Restore previous state.
  101. if player:get_meta():get_int("hides_nametag") ~= 0 then
  102. player_labels.cast[pname] = false
  103. end
  104. -- Player labels are shown according to restored state.
  105. if player_labels.cast[pname] then
  106. nametag_show(pname)
  107. else
  108. -- After welcome msg, before joinspec and email.
  109. minetest.after(5, function(pn)
  110. minetest.chat_send_player(pn, "# Server: Avatar name broadcast is OFF.")
  111. end, pname)
  112. nametag_hide(pname)
  113. end
  114. end
  115. -- Called when the player uses a `player labels token`.
  116. player_labels.toggle_nametag_broadcast = function(name)
  117. if player_labels.cast[name] then
  118. minetest.chat_send_player(name, "# Server: Avatar name broadcast is OFF.")
  119. player_labels.cast[name] = false
  120. player_labels.disable_nametag_broadcast(name)
  121. else
  122. minetest.chat_send_player(name, "# Server: Avatar name broadcast is ON.")
  123. player_labels.cast[name] = true
  124. player_labels.enable_nametag_broadcast(name)
  125. end
  126. persist_cast(name)
  127. end
  128. function player_labels.enable_nametag(pname)
  129. player_labels.cast[pname] = true
  130. player_labels.enable_nametag_broadcast(pname)
  131. persist_cast(pname)
  132. end
  133. function player_labels.disable_nametag(pname)
  134. player_labels.cast[pname] = false
  135. player_labels.disable_nametag_broadcast(pname)
  136. persist_cast(pname)
  137. end
  138. player_labels.enable_nametag_broadcast = function(name)
  139. if player_labels.cast[name] == true then
  140. nametag_show(name)
  141. end
  142. end
  143. player_labels.disable_nametag_broadcast = function(name)
  144. if player_labels.cast[name] == false then
  145. if refcount_get(name) <= 0 then
  146. nametag_hide(name)
  147. end
  148. end
  149. end
  150. -- A delayed action function, for hiding a players name (if needed) after some delay.
  151. -- Called from `minetest.after`.
  152. player_labels.on_tag_timeout = function(name)
  153. local count = refcount_decrement(name)
  154. if count <= 0 then
  155. refcount_set(name, 0)
  156. if not player_labels.cast[name] then
  157. nametag_hide(name)
  158. end
  159. end
  160. end
  161. --[[
  162. player_labels.on_anim_timeout = function(name)
  163. local object = minetest.get_player_by_name(name)
  164. if object and object:is_player() then
  165. local fr = object:get_animation()
  166. if fr.x == 221 and fr.y == 251 then -- If still bobbing head.
  167. object:set_animation({x=0, y=79}, 30, 0, true)
  168. end
  169. end
  170. end
  171. --]]
  172. -- This function is called whenever the player uses the ID token.
  173. player_labels.on_token_use = function(itemstack, user, pointed_thing)
  174. if not user then return end
  175. if not user:is_player() then return end
  176. local pname = user:get_player_name()
  177. if pointed_thing.type == "object" then
  178. local object = pointed_thing.ref
  179. if object:is_player() and not gdac.player_is_admin(object) then
  180. local uname = user:get_player_name()
  181. local oname = object:get_player_name()
  182. if gdac_invis.is_invisible(oname) == true then return end
  183. if cloaking.is_cloaked(oname) then return end
  184. local sex = skins.get_gender_strings(oname)
  185. local xp_amount = xp.get_xp(oname, "digxp")
  186. minetest.chat_send_player(uname, "# Server: Player's alias is <" .. rename.gpn(oname) .. ">; " .. sex.his .. " login name is <" .. oname .. ">.")
  187. minetest.chat_send_player(uname, "# Server: Player <" .. rename.gpn(oname) .. ">'s mineral XP is " .. xp_amount .. ".")
  188. minetest.chat_send_player(oname, "# Server: Player <" .. rename.gpn(uname) .. "> identified you.")
  189. refcount_increment(oname)
  190. nametag_show(oname)
  191. minetest.after(player_labels.mark_timeout, player_labels.on_tag_timeout, oname)
  192. return
  193. end
  194. end
  195. if gdac_invis.is_invisible(pname) == true then
  196. minetest.chat_send_player(pname, "# Server: You are currently invisible! Being invisible already hides your nametag.")
  197. minetest.chat_send_player(pname, "# Server: If you want to show your nametag again, stop being invisible.")
  198. easyvend.sound_error(pname)
  199. return
  200. end
  201. if cloaking.is_cloaked(pname) then
  202. minetest.chat_send_player(pname, "# Server: You are currently cloaked! Being cloaked already hides your nametag.")
  203. minetest.chat_send_player(pname, "# Server: If you want to show your nametag again, turn of your cloak.")
  204. easyvend.sound_error(pname)
  205. return
  206. end
  207. player_labels.toggle_nametag_broadcast(pname)
  208. return
  209. end
  210. -- This function gets called from another mod whenever the player sends a chat message.
  211. player_labels.on_chat_message = function(name, message)
  212. if gdac_invis.is_invisible(name) then return end
  213. local object = minetest.get_player_by_name(name)
  214. if object and object:is_player() then
  215. --object:set_animation({x=189, y=198}, 30, 0, true)
  216. --object:set_animation({x=221, y=251}, 30, 0, true)
  217. --minetest.after(player_labels.anim_timeout, player_labels.on_anim_timeout, name)
  218. refcount_increment(name)
  219. nametag_show(name)
  220. minetest.after(player_labels.chat_timeout, player_labels.on_tag_timeout, name)
  221. end
  222. end
  223. -- First-time execution only.
  224. if not player_labels.registered then
  225. minetest.register_on_joinplayer(function(...) return player_labels.on_joinplayer(...) end)
  226. minetest.register_craftitem("player_labels:show", {
  227. description = "ID Marker\n\nUse this to see someone's name, if suspicious of impersonation.\nThis can also show or hide your own name.",
  228. inventory_image = "default_copper_block.png",
  229. --range = 10, -- Disabled because this allows players to access formspecs from long range.
  230. on_use = function(...) return player_labels.on_token_use(...) end,
  231. -- Disabled because these attempts to disable right-click functionality do not appear to work.
  232. --on_rightclick = function(...) end,
  233. --on_secondary_use = function(...) end,
  234. --on_place = function(...) end,
  235. })
  236. minetest.register_craft({
  237. output = 'player_labels:show',
  238. recipe = {
  239. {"", "default:copper_ingot", ""},
  240. {"default:copper_ingot", "", "default:copper_ingot"},
  241. {"", "default:copper_ingot", ""},
  242. },
  243. })
  244. player_labels.registered = true
  245. end
  246. -- Support for reloading.
  247. if minetest.get_modpath("reload") then
  248. local c = "player_labels:core"
  249. local f = player_labels.modpath .. "/init.lua"
  250. if not reload.file_registered(c) then
  251. reload.register_file(c, f, false)
  252. end
  253. end