mark_token.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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;true]"
  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. local pname = user:get_player_name()
  24. if pointed.type == "object" then
  25. local object = pointed.ref
  26. if object and object:is_player() then
  27. local success = command_tokens.mark.execute(
  28. pname, object:get_player_name())
  29. if success then
  30. itemstack:take_item()
  31. return itemstack
  32. end
  33. else
  34. minetest.chat_send_player(pname, "# Server: Target is not a player!")
  35. end
  36. else
  37. minetest.show_formspec(pname, "command_tokens:mark", formspec)
  38. end
  39. end
  40. end
  41. local function is_valid_target(name, target)
  42. if minetest.get_player_by_name(target) then
  43. if gdac_invis.is_invisible(target) == true then return end
  44. if name == target then
  45. return true -- Player can always mark self.
  46. end
  47. if not minetest.check_player_privs(target, {server=true}) then
  48. return true
  49. end
  50. end
  51. end
  52. command_tokens.mark.execute = function(player, target)
  53. player = rename.grn(player)
  54. target = rename.grn(target)
  55. local dname = rename.gpn(target)
  56. if is_valid_target(player, target) then
  57. local pref = minetest.get_player_by_name(player)
  58. local p1 = pref:get_pos()
  59. local p2 = minetest.get_player_by_name(target):get_pos()
  60. if rc.same_realm(p1, p2) then
  61. -- Ensure player has a mark token in their inventory.
  62. local pinv = pref:get_inventory()
  63. local stack = ItemStack("command_tokens:mark_player")
  64. if pinv:contains_item("main", stack) then
  65. -- Mark player if they wern't marked, unmark them if they were.
  66. if not command_tokens.mark.players[target] then
  67. command_tokens.mark.players[target] = true
  68. minetest.chat_send_all("# Server: Player <" .. dname .. "> has been marked!")
  69. else
  70. command_tokens.mark.players[target] = nil
  71. minetest.chat_send_all("# Server: Player <" .. dname .. "> was unmarked.")
  72. end
  73. -- Caller must consume token.
  74. return true
  75. else
  76. minetest.chat_send_player(player, "# Server: Error. Invalid usage.")
  77. end
  78. else
  79. minetest.chat_send_player(player, "# Server: Target is in another dimension!")
  80. end
  81. else
  82. minetest.chat_send_player(player, "# Server: Player <" .. dname .. "> cannot be marked.")
  83. end
  84. end
  85. command_tokens.mark_on_receive_fields = function(player, formname, fields)
  86. if formname == "command_tokens:mark" then
  87. if fields.key_enter_field == "PLAYERNAME" or fields.OK then
  88. local pname = player:get_player_name()
  89. -- Attempt to mark the target, get success/failure.
  90. local success = command_tokens.mark.execute(
  91. pname, fields.PLAYERNAME or "")
  92. if success then
  93. local inv = player:get_inventory()
  94. if inv then
  95. inv:remove_item("main", "command_tokens:mark_player")
  96. end
  97. end
  98. end
  99. end
  100. end
  101. -- Register once only.
  102. if not command_tokens.mark.registered then
  103. minetest.register_on_player_receive_fields(function(...)
  104. return command_tokens.mark_on_receive_fields(...)
  105. end)
  106. command_tokens.mark.registered = true
  107. end