teleport.lua 6.0 KB

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