mark_token.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. command_tokens = command_tokens or {}
  2. command_tokens.mark = command_tokens.mark or {}
  3. command_tokens.mark.players = command_tokens.mark.players or {}
  4. local formspec = "size[4.1,2.0]" ..
  5. default.gui_bg ..
  6. default.gui_bg_img ..
  7. default.gui_slots ..
  8. "label[0,0;Type name of person to mark:]" ..
  9. "field[0.30,0.75;4,1;PLAYERNAME;;]" ..
  10. "button_exit[0,1.30;2,1;OK;Confirm]" ..
  11. "button_exit[2,1.30;2,1;CANCEL;Cancel]" ..
  12. "field_close_on_enter[PLAYERNAME;false]"
  13. command_tokens.mark.player_marked = function(player)
  14. if command_tokens.mark.players[player] then
  15. return true
  16. else
  17. return false
  18. end
  19. end
  20. -- Called when the player uses a marker token.
  21. command_tokens.mark.mark_player = function(itemstack, user, pointed)
  22. if user and user:is_player() then
  23. if pointed.type == "object" then
  24. local object = pointed.ref
  25. if object and object:is_player() then
  26. command_tokens.mark.execute(user:get_player_name(), object:get_player_name())
  27. else
  28. minetest.chat_send_player(user:get_player_name(), "# Server: Target is not a player!")
  29. end
  30. else
  31. local name = user:get_player_name()
  32. minetest.show_formspec(name, "command_tokens:mark", formspec)
  33. end
  34. end
  35. end
  36. local function is_valid_target(name, target)
  37. if minetest.get_player_by_name(target) then
  38. if gdac_invis.is_invisible(target) == true then return end
  39. if name == target then
  40. return true -- Player can always mark self.
  41. end
  42. if not minetest.check_player_privs(target, {server=true}) then
  43. return true
  44. end
  45. end
  46. end
  47. command_tokens.mark.execute = function(player, target)
  48. player = rename.grn(player)
  49. target = rename.grn(target)
  50. local dname = rename.gpn(target)
  51. if is_valid_target(player, target) then
  52. local p1 = minetest.get_player_by_name(player):get_pos()
  53. local p2 = minetest.get_player_by_name(target):get_pos()
  54. if rc.same_realm(p1, p2) then
  55. -- Mark player if they wern't marked, unmark them if they were.
  56. if not command_tokens.mark.players[target] then
  57. command_tokens.mark.players[target] = true
  58. minetest.chat_send_all("# Server: Player <" .. dname .. "> has been marked!")
  59. else
  60. command_tokens.mark.players[target] = nil
  61. minetest.chat_send_all("# Server: Player <" .. dname .. "> was unmarked.")
  62. end
  63. -- Consume token.
  64. minetest.after(0, function() -- Necessary because this code will not operate during the on_use callback.
  65. local ref = minetest.get_player_by_name(player)
  66. if ref and ref:is_player() then
  67. local inv = ref:get_inventory()
  68. inv:remove_item("main", "command_tokens:mark_player")
  69. end
  70. end)
  71. else
  72. minetest.chat_send_player(player, "# Server: Target is in another dimension!")
  73. end
  74. else
  75. minetest.chat_send_player(player, "# Server: Player <" .. dname .. "> cannot be marked.")
  76. end
  77. end
  78. command_tokens.mark_on_receive_fields = function(player, formname, fields)
  79. if formname == "command_tokens:mark" then
  80. if fields.OK then
  81. command_tokens.mark.execute(player:get_player_name(), fields.PLAYERNAME)
  82. end
  83. end
  84. end
  85. -- Register once only.
  86. if not command_tokens.mark.registered then
  87. minetest.register_on_player_receive_fields(function(...)
  88. return command_tokens.mark_on_receive_fields(...)
  89. end)
  90. command_tokens.mark.registered = true
  91. end