|
| 1 | +--- |
| 2 | +slug: /dev/entity-teleport |
| 3 | +description: The entity teleportation API and how to use it. |
| 4 | +--- |
| 5 | + |
| 6 | +# Entity Teleportation |
| 7 | + |
| 8 | +Entities can be instantaneously teleported to specific positions, synchronously and asynchronously with the |
| 9 | +<Javadoc name={"org.bukkit.entity.Entity#teleport(org.bukkit.Location)"}>`teleport`</Javadoc> and |
| 10 | +<Javadoc name={"org.bukkit.entity.Entity#teleportAsync(org.bukkit.Location)"}>`teleportAsync`</Javadoc> API. |
| 11 | + |
| 12 | +```java |
| 13 | +entity.teleport(location); // teleports the entity synchronously |
| 14 | + |
| 15 | +entity.teleportAsync(location).thenAccept(success -> { // teleports the entity asynchronously |
| 16 | + // this code is ran when the teleport completes |
| 17 | + // the Future is completed on the main thread, so it is safe to use the API here |
| 18 | + |
| 19 | + if (success) { |
| 20 | + // the entity was teleported successfully! |
| 21 | + } |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +## Look at |
| 26 | + |
| 27 | +The <Javadoc name={"org.bukkit.entity.Player#lookAt(io.papermc.paper.math.Position,io.papermc.paper.entity.LookAnchor)"}>`lookAt`</Javadoc> |
| 28 | +API allows you to make a player look at a certain position or entity. |
| 29 | + |
| 30 | +```java |
| 31 | +player.lookAt( |
| 32 | + position, |
| 33 | + LookAnchor.EYES // the player's eyes will be facing the position |
| 34 | +); |
| 35 | + |
| 36 | +player.lookAt( |
| 37 | + entity, |
| 38 | + LookAnchor.EYES // the player's eyes will be facing the entity |
| 39 | + LookAnchor.FEET // the player will be facing the entity's feet |
| 40 | +); |
| 41 | +``` |
| 42 | + |
| 43 | +## Teleport flags |
| 44 | + |
| 45 | +Teleport flags offer a way to teleport entities whilst being able to customize behavior. |
| 46 | +This allows you to do things like teleport players using relative flags and being able to retain passengers. |
| 47 | + |
| 48 | +All available teleport flags can be found in the <Javadoc name={"io.papermc.paper.entity.TeleportFlag"}>`TeleportFlag`</Javadoc> class. |
| 49 | + |
| 50 | +### Relative teleportation |
| 51 | + |
| 52 | +Teleport a player relatively, preventing velocity from being reset in the X, Y and Z axes. |
| 53 | + |
| 54 | +```java |
| 55 | +player.teleport( |
| 56 | + location, |
| 57 | + TeleportFlag.Relative.X, |
| 58 | + TeleportFlag.Relative.Y, |
| 59 | + TeleportFlag.Relative.Z |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +### Retaining passengers |
| 64 | + |
| 65 | +Teleport an entity with the <Javadoc name={"io.papermc.paper.entity.TeleportFlag$EntityState#RETAIN_PASSENGERS"}>`RETAIN_PASSENGERS`</Javadoc> flag, |
| 66 | +allowing its passengers to be transferred with the entity. |
| 67 | + |
| 68 | +```java |
| 69 | +entity.teleport(location, TeleportFlag.EntityState.RETAIN_PASSENGERS); |
| 70 | +``` |
0 commit comments