init.lua 8.7 KB

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