init.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. minetest.chat_send_player(pname, "# Server: Avatar name broadcast is OFF.")
  109. nametag_hide(pname)
  110. end
  111. end
  112. -- Called when the player uses a `player labels token`.
  113. player_labels.toggle_nametag_broadcast = function(name)
  114. if player_labels.cast[name] then
  115. minetest.chat_send_player(name, "# Server: Avatar name broadcast is OFF.")
  116. player_labels.cast[name] = false
  117. player_labels.disable_nametag_broadcast(name)
  118. else
  119. minetest.chat_send_player(name, "# Server: Avatar name broadcast is ON.")
  120. player_labels.cast[name] = true
  121. player_labels.enable_nametag_broadcast(name)
  122. end
  123. persist_cast(name)
  124. end
  125. function player_labels.enable_nametag(pname)
  126. player_labels.cast[pname] = true
  127. player_labels.enable_nametag_broadcast(pname)
  128. persist_cast(pname)
  129. end
  130. function player_labels.disable_nametag(pname)
  131. player_labels.cast[pname] = false
  132. player_labels.disable_nametag_broadcast(pname)
  133. persist_cast(pname)
  134. end
  135. player_labels.enable_nametag_broadcast = function(name)
  136. if player_labels.cast[name] == true then
  137. nametag_show(name)
  138. end
  139. end
  140. player_labels.disable_nametag_broadcast = function(name)
  141. if player_labels.cast[name] == false then
  142. if refcount_get(name) <= 0 then
  143. nametag_hide(name)
  144. end
  145. end
  146. end
  147. -- A delayed action function, for hiding a players name (if needed) after some delay.
  148. -- Called from `minetest.after`.
  149. player_labels.on_tag_timeout = function(name)
  150. local count = refcount_decrement(name)
  151. if count <= 0 then
  152. refcount_set(name, 0)
  153. if not player_labels.cast[name] then
  154. nametag_hide(name)
  155. end
  156. end
  157. end
  158. --[[
  159. player_labels.on_anim_timeout = function(name)
  160. local object = minetest.get_player_by_name(name)
  161. if object and object:is_player() then
  162. local fr = object:get_animation()
  163. if fr.x == 221 and fr.y == 251 then -- If still bobbing head.
  164. object:set_animation({x=0, y=79}, 30, 0, true)
  165. end
  166. end
  167. end
  168. --]]
  169. -- This function is called whenever the player uses the ID token.
  170. player_labels.on_token_use = function(itemstack, user, pointed_thing)
  171. if not user then return end
  172. if not user:is_player() then return end
  173. local pname = user:get_player_name()
  174. if pointed_thing.type == "object" then
  175. local object = pointed_thing.ref
  176. if object:is_player() and not gdac.player_is_admin(object) then
  177. local uname = user:get_player_name()
  178. local oname = object:get_player_name()
  179. if gdac_invis.is_invisible(oname) == true then return end
  180. if cloaking.is_cloaked(oname) then return end
  181. local sex = skins.get_gender_strings(oname)
  182. local xp_amount = xp.get_xp(oname, "digxp")
  183. minetest.chat_send_player(uname, "# Server: Player's alias is <" .. rename.gpn(oname) .. ">; " .. sex.his .. " login name is <" .. oname .. ">.")
  184. minetest.chat_send_player(uname, "# Server: Player <" .. rename.gpn(oname) .. ">'s mineral XP is " .. xp_amount .. ".")
  185. minetest.chat_send_player(oname, "# Server: Player <" .. rename.gpn(uname) .. "> identified you.")
  186. refcount_increment(oname)
  187. nametag_show(oname)
  188. minetest.after(player_labels.mark_timeout, player_labels.on_tag_timeout, oname)
  189. return
  190. end
  191. end
  192. if gdac_invis.is_invisible(pname) == true then
  193. minetest.chat_send_player(pname, "# Server: You are currently invisible! Being invisible already hides your nametag.")
  194. minetest.chat_send_player(pname, "# Server: If you want to show your nametag again, stop being invisible.")
  195. easyvend.sound_error(pname)
  196. return
  197. end
  198. if cloaking.is_cloaked(pname) then
  199. minetest.chat_send_player(pname, "# Server: You are currently cloaked! Being cloaked already hides your nametag.")
  200. minetest.chat_send_player(pname, "# Server: If you want to show your nametag again, turn of your cloak.")
  201. easyvend.sound_error(pname)
  202. return
  203. end
  204. player_labels.toggle_nametag_broadcast(pname)
  205. return
  206. end
  207. -- This function gets called from another mod whenever the player sends a chat message.
  208. player_labels.on_chat_message = function(name, message)
  209. if gdac_invis.is_invisible(name) then return end
  210. local object = minetest.get_player_by_name(name)
  211. if object and object:is_player() then
  212. --object:set_animation({x=189, y=198}, 30, 0, true)
  213. --object:set_animation({x=221, y=251}, 30, 0, true)
  214. --minetest.after(player_labels.anim_timeout, player_labels.on_anim_timeout, name)
  215. refcount_increment(name)
  216. nametag_show(name)
  217. minetest.after(player_labels.chat_timeout, player_labels.on_tag_timeout, name)
  218. end
  219. end
  220. -- First-time execution only.
  221. if not player_labels.registered then
  222. minetest.register_on_joinplayer(function(...) return player_labels.on_joinplayer(...) end)
  223. minetest.register_craftitem("player_labels:show", {
  224. description = "ID Marker\n\nUse this to see someone's name, if suspicious of impersonation.\nThis can also show or hide your own name.",
  225. inventory_image = "default_copper_block.png",
  226. --range = 10, -- Disabled because this allows players to access formspecs from long range.
  227. on_use = function(...) return player_labels.on_token_use(...) end,
  228. -- Disabled because these attempts to disable right-click functionality do not appear to work.
  229. --on_rightclick = function(...) end,
  230. --on_secondary_use = function(...) end,
  231. --on_place = function(...) end,
  232. })
  233. minetest.register_craft({
  234. output = 'player_labels:show',
  235. recipe = {
  236. {"", "default:copper_ingot", ""},
  237. {"default:copper_ingot", "", "default:copper_ingot"},
  238. {"", "default:copper_ingot", ""},
  239. },
  240. })
  241. player_labels.registered = true
  242. end
  243. -- Support for reloading.
  244. if minetest.get_modpath("reload") then
  245. local c = "player_labels:core"
  246. local f = player_labels.modpath .. "/init.lua"
  247. if not reload.file_registered(c) then
  248. reload.register_file(c, f, false)
  249. end
  250. end