jail_token.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. command_tokens = command_tokens or {}
  2. command_tokens.jail = command_tokens.jail or {}
  3. minetest.register_privilege("ignore_jail_token", {description="Player cannot be sent to jail.", give_to_singleplayer=false})
  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 trespasser:]" ..
  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. -- Called when the player uses a jail token.
  14. command_tokens.jail.jail_player = function(itemstack, user, pointed)
  15. if user and user:is_player() then
  16. if pointed.type == "object" then
  17. local object = pointed.ref
  18. if object and object:is_player() then
  19. command_tokens.jail.execute(user:get_player_name(), object:get_player_name())
  20. else
  21. minetest.chat_send_player(user:get_player_name(), "# Server: Target is not a player!")
  22. end
  23. else
  24. local name = user:get_player_name()
  25. minetest.show_formspec(name, "command_tokens:jail", formspec)
  26. end
  27. end
  28. end
  29. local function is_valid_target(name, target)
  30. if minetest.get_player_by_name(target) then
  31. if not minetest.check_player_privs(target, {ignore_jail_token=true}) then
  32. return true
  33. end
  34. end
  35. return false
  36. end
  37. command_tokens.jail.execute = function(player, target)
  38. player = rename.grn(player)
  39. target = rename.grn(target)
  40. if not minetest.get_player_by_name(target) then
  41. minetest.chat_send_player(player, "# Server: Law enforcement couldn't find player <" .. rename.gpn(target) .. ">.")
  42. return
  43. end
  44. local p1 = minetest.get_player_by_name(player):get_pos()
  45. local p2 = minetest.get_player_by_name(target):get_pos()
  46. if not rc.same_realm(p1, p2) then
  47. minetest.chat_send_player(player, "# Server: Target is in another dimension!")
  48. return
  49. end
  50. local self_arrest = false
  51. if player == target then
  52. --minetest.chat_send_player(player, "# Server: Law-enforcement does not arrest self-trespassers.")
  53. --return
  54. self_arrest = true
  55. end
  56. -- Allow self-arrest.
  57. if player ~= target then
  58. if not is_valid_target(player, target) then
  59. minetest.chat_send_player(player, "# Server: Player <" .. rename.gpn(target) .. "> is immune to accusations of trespassing.")
  60. return
  61. end
  62. end
  63. local ent = minetest.get_player_by_name(target)
  64. -- 'ent' should always be valid.
  65. local pos = ent:getpos()
  66. local owner = nil
  67. if minetest.get_modpath("protector") then
  68. owner = protector.get_node_owner(pos)
  69. end
  70. if not city_block:in_city(pos) then
  71. minetest.chat_send_player(player, "# Server: Trespasser is not within city boundaries.")
  72. return
  73. end
  74. if not owner or owner ~= player or owner == "" then
  75. minetest.chat_send_player(player, "# Server: Player <" .. rename.gpn(target) .. "> is not in territory owned by you.")
  76. return
  77. end
  78. -- Get player out of cart.
  79. local kicktype = default.detach_player_if_attached(ent)
  80. if kicktype == "cart" then
  81. minetest.chat_send_all("# Server: Someone threw <" .. rename.gpn(target) .. "> out of a minecart.")
  82. elseif kicktype == "boat" then
  83. minetest.chat_send_all("# Server: Boater <" .. rename.gpn(target) .. "> was tossed overboard.")
  84. elseif kicktype == "sled" then
  85. minetest.chat_send_all("# Server: Someone kicked <" .. rename.gpn(target) .. "> off a sled.")
  86. elseif kicktype == "bed" then
  87. local formspec = "size[8,15;true]" ..
  88. "bgcolor[#080808BB; true]" ..
  89. "button_exit[2,12;4,0.75;leave;Ok]" ..
  90. "label[2.7,11;You were kicked out of bed!]"
  91. minetest.show_formspec(target, "command_tokens:bedkicked", formspec)
  92. minetest.chat_send_all("# Server: <" .. rename.gpn(target) .. "> was rudely kicked out of bed.")
  93. end
  94. local jaildelay = 0
  95. if kicktype ~= "" then
  96. jaildelay = 1
  97. end
  98. -- Not sure why the delay is necessary, but it may have to do with lag.
  99. minetest.after(jaildelay, function()
  100. if not default.player_attached[target] then
  101. local bcb = function()
  102. if self_arrest then
  103. minetest.chat_send_all("# Server: Player <" .. rename.gpn(target) .. "> committed self-arrest.")
  104. else
  105. minetest.chat_send_all("# Server: Player <" .. rename.gpn(target) .. "> sent to jail for trespassing on <" .. rename.gpn(player) .. ">'s land.")
  106. end
  107. end
  108. jail.go_to_jail(minetest.get_player_by_name(target), bcb)
  109. else
  110. minetest.chat_send_player(player, "# Server: Player <" .. rename.gpn(target) .. "> could not be detached!")
  111. end
  112. end)
  113. -- Consume token.
  114. -- Necessary because this code will not operate during the on_use callback.
  115. minetest.after(0, function()
  116. local ref = minetest.get_player_by_name(player)
  117. if ref and ref:is_player() then
  118. local inv = ref:get_inventory()
  119. inv:remove_item("main", "command_tokens:jail_player")
  120. end
  121. end)
  122. end
  123. command_tokens.jail_on_receive_fields = function(player, formname, fields)
  124. if formname == "command_tokens:jail" then
  125. if fields.OK then
  126. command_tokens.jail.execute(player:get_player_name(), fields.PLAYERNAME)
  127. end
  128. end
  129. end
  130. -- Register once only.
  131. if not command_tokens.jail.registered then
  132. minetest.register_on_player_receive_fields(function(...)
  133. return command_tokens.jail_on_receive_fields(...)
  134. end)
  135. command_tokens.jail.registered = true
  136. end