teleport.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. -- Localize for performance.
  2. local vector_round = vector.round
  3. function serveressentials.do_teleport(name, param)
  4. name = name:trim()
  5. param = param:trim()
  6. -- Returns (pos, true) if found, otherwise (pos, false)
  7. local function find_free_position_near(pos)
  8. pos = vector_round(pos)
  9. local tries = {
  10. {x=1,y=0,z=0},
  11. {x=-1,y=0,z=0},
  12. {x=0,y=0,z=1},
  13. {x=0,y=0,z=-1},
  14. }
  15. for _, d in ipairs(tries) do
  16. local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
  17. local n = core.get_node_or_nil(p)
  18. if n and n.name then
  19. local def = core.registered_nodes[n.name]
  20. if def and not def.walkable then
  21. if rc.is_valid_realm_pos(p) then
  22. return p, true
  23. end
  24. end
  25. end
  26. end
  27. return pos, false
  28. end
  29. local teleportee = nil
  30. local p = {}
  31. p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  32. p.x = tonumber(p.x)
  33. p.y = tonumber(p.y)
  34. p.z = tonumber(p.z)
  35. if p.x and p.y and p.z then
  36. p = vector_round(p)
  37. local lm = 31000
  38. if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then
  39. return false, "Cannot teleport out of map bounds."
  40. end
  41. if not rc.is_valid_realm_pos(p) then
  42. return false, "Cannot teleport outside of any realm."
  43. end
  44. teleportee = core.get_player_by_name(name)
  45. if teleportee then
  46. local o = vector_round(teleportee:get_pos())
  47. rc.notify_realm_update(teleportee:get_player_name(), p)
  48. jail.discharge_pref(teleportee)
  49. teleportee:set_pos(p)
  50. return true, "Teleporting from " .. rc.pos_to_namestr(o) .. " to " .. core.pos_to_string(p) .. ", which is @ " .. rc.pos_to_namestr(p) .. "."
  51. end
  52. end
  53. local teleportee = nil
  54. local p = {}
  55. local realm = nil
  56. --minetest.chat_send_player(name, param)
  57. realm, p.x, p.y, p.z = string.match(param, "^([^ ]+) *: *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  58. p.x = tonumber(p.x)
  59. p.y = tonumber(p.y)
  60. p.z = tonumber(p.z)
  61. --minetest.chat_send_player(name, "Got: " .. realm .. ":" .. p.x .. "," .. p.y .. "," .. p.z)
  62. if realm and p.x and p.y and p.z then
  63. p = rc.realmpos_to_pos(realm, p)
  64. if not p then
  65. return false, "Cannot interpret realm coordinates."
  66. end
  67. local lm = 31000
  68. if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then
  69. return false, "Cannot teleport out of map bounds."
  70. end
  71. if not rc.is_valid_realm_pos(p) then
  72. return false, "Cannot teleport outside of any realm."
  73. end
  74. teleportee = core.get_player_by_name(name)
  75. if teleportee then
  76. local o = vector_round(teleportee:get_pos())
  77. rc.notify_realm_update(teleportee:get_player_name(), p)
  78. jail.discharge_pref(teleportee)
  79. teleportee:set_pos(p)
  80. return true, "Teleporting from " .. rc.pos_to_namestr(o) .. " to " .. rc.pos_to_namestr(p) .. "."
  81. end
  82. end
  83. local teleportee = nil
  84. local p = nil
  85. local target_name = nil
  86. target_name = param:match("^([^ ]+)$")
  87. teleportee = core.get_player_by_name(name)
  88. if target_name then
  89. local target = core.get_player_by_name(target_name)
  90. if target then
  91. p = vector_round(target:get_pos())
  92. end
  93. end
  94. if teleportee and p then
  95. p = find_free_position_near(p)
  96. if not rc.is_valid_realm_pos(p) then
  97. return false, "Cannot teleport outside of any realm."
  98. end
  99. local o = vector_round(teleportee:get_pos())
  100. rc.notify_realm_update(teleportee:get_player_name(), p)
  101. jail.discharge_pref(teleportee)
  102. teleportee:set_pos(p)
  103. return true, "Teleporting from " .. rc.pos_to_namestr(o) .. " to <" .. rename.gpn(target_name) .. "> at " .. rc.pos_to_namestr(p) .. "."
  104. end
  105. if not core.check_player_privs(name, {bring=true}) then
  106. return false, "You don't have permission to teleport other players (missing bring privilege)."
  107. end
  108. local teleportee = nil
  109. local p = {}
  110. local teleportee_name = nil
  111. teleportee_name, p.x, p.y, p.z = param:match("^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  112. p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
  113. if teleportee_name then
  114. teleportee = core.get_player_by_name(teleportee_name)
  115. end
  116. if teleportee and p.x and p.y and p.z then
  117. p = vector_round(p)
  118. if not rc.is_valid_realm_pos(p) then
  119. return false, "Cannot teleport players outside realm boundaries."
  120. end
  121. local o = vector_round(teleportee:get_pos())
  122. rc.notify_realm_update(teleportee:get_player_name(), p)
  123. jail.discharge_pref(teleportee)
  124. teleportee:set_pos(p)
  125. return true, "Teleporting <" .. rename.gpn(teleportee_name) .. "> from " .. rc.pos_to_namestr(o) .. " to " .. core.pos_to_string(p) .. ", which is @ " .. rc.pos_to_namestr(p) .. "."
  126. end
  127. local teleportee = nil
  128. local p = {}
  129. local teleportee_name = nil
  130. local realm = nil
  131. teleportee_name, realm, p.x, p.y, p.z = param:match("^([^ ]+) +([^ ]+) *: *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  132. p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
  133. if teleportee_name then
  134. teleportee = core.get_player_by_name(teleportee_name)
  135. end
  136. if teleportee and realm and p.x and p.y and p.z then
  137. p = rc.realmpos_to_pos(realm, p)
  138. if not p then
  139. return false, "Cannot interpret realm coordinates."
  140. end
  141. if not rc.is_valid_realm_pos(p) then
  142. return false, "Cannot teleport players outside realm boundaries."
  143. end
  144. local o = vector_round(teleportee:get_pos())
  145. rc.notify_realm_update(teleportee:get_player_name(), p)
  146. jail.discharge_pref(teleportee)
  147. teleportee:set_pos(p)
  148. return true, "Teleporting <" .. rename.gpn(teleportee_name) .. "> from " .. rc.pos_to_namestr(o) .. " to " .. rc.pos_to_namestr(p) .. "."
  149. end
  150. local teleportee = nil
  151. local p = nil
  152. local teleportee_name = nil
  153. local target_name = nil
  154. teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
  155. if teleportee_name then
  156. teleportee = core.get_player_by_name(teleportee_name)
  157. end
  158. if target_name then
  159. local target = core.get_player_by_name(target_name)
  160. if target then
  161. p = target:get_pos()
  162. end
  163. end
  164. if teleportee and p then
  165. p = vector_round(p)
  166. if not rc.is_valid_realm_pos(p) then
  167. return false, "Cannot teleport players outside realm boundaries."
  168. end
  169. p = find_free_position_near(p)
  170. local o = vector_round(teleportee:get_pos())
  171. rc.notify_realm_update(teleportee:get_player_name(), p)
  172. jail.discharge_pref(teleportee)
  173. teleportee:set_pos(p)
  174. return true, "Teleporting <" .. rename.gpn(teleportee_name) .. "> from " .. rc.pos_to_namestr(o) .. " to <" .. rename.gpn(target_name) .. "> at " .. rc.pos_to_namestr(p) .. "."
  175. end
  176. return false, 'Invalid parameters or player not found (see /help teleport).'
  177. end