Add TeleportFlags (#8855)

Abstracts relative teleport flags and instead makes a generic TeleportFlag option. This has the benefit of being able to easily add new flags in the future.
This adds a new flag, which allows you to keep inventories open when teleporting players (vanilla behavior).

These are breaking changes to the teleport api, however, it's still marked as experimental so I find this a fair change.
This commit is contained in:
Owen1212055 2023-03-04 17:07:23 -05:00
parent d922f2e369
commit 2dd20a5904
3 changed files with 122 additions and 120 deletions

View File

@ -119,7 +119,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ */ + */
+ @Deprecated + @Deprecated
+ @Override + @Override
+ public @NotNull java.util.Set<io.papermc.paper.entity.@NotNull RelativeTeleportFlag> getRelativeTeleportationFlags() { + public @NotNull java.util.Set<io.papermc.paper.entity.TeleportFlag.@NotNull Relative> getRelativeTeleportationFlags() {
+ return super.getRelativeTeleportationFlags(); + return super.getRelativeTeleportationFlags();
+ } + }
+ // Paper end + // Paper end

View File

@ -35,24 +35,41 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ */ + */
+ EYES; + EYES;
+} +}
diff --git a/src/main/java/io/papermc/paper/entity/RelativeTeleportFlag.java b/src/main/java/io/papermc/paper/entity/RelativeTeleportFlag.java diff --git a/src/main/java/io/papermc/paper/entity/TeleportFlag.java b/src/main/java/io/papermc/paper/entity/TeleportFlag.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/RelativeTeleportFlag.java +++ b/src/main/java/io/papermc/paper/entity/TeleportFlag.java
@@ -0,0 +0,0 @@ @@ -0,0 +0,0 @@
+package io.papermc.paper.entity; +package io.papermc.paper.entity;
+ +
+import org.bukkit.Location; +import org.bukkit.Location;
+import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerTeleportEvent;
+import org.jetbrains.annotations.ApiStatus;
+ +
+/** +/**
+ * Represents coordinates in a teleportation that should be handled relatively. + * Represents a flag that can be set on teleportation that may
+ * slightly modify the behavior.
+ * + *
+ * @see org.bukkit.entity.Player#teleport(Location, PlayerTeleportEvent.TeleportCause, boolean, boolean, RelativeTeleportFlag...) + * @see EntityState
+ * @see Relative
+ */ + */
+@org.jetbrains.annotations.ApiStatus.Experimental +@ApiStatus.Experimental
+public enum RelativeTeleportFlag { +public sealed interface TeleportFlag permits TeleportFlag.EntityState, TeleportFlag.Relative {
+
+ /**
+ * Note: These flags only work on {@link org.bukkit.entity.Player} entities.
+ * <p>
+ * Represents coordinates in a teleportation that should be handled relatively.
+ * <p>
+ * Coordinates of the location that the client should handle as relative teleportation
+ * Relative teleportation flags are only used client side, and cause the player to not lose velocity in that
+ * specific coordinate. The location of the teleportation will not change.
+ *
+ * @see org.bukkit.entity.Player#teleport(Location, PlayerTeleportEvent.TeleportCause, TeleportFlag...)
+ */
+ @ApiStatus.Experimental
+ enum Relative implements TeleportFlag {
+ /** + /**
+ * Represents the player's X coordinate + * Represents the player's X coordinate
+ */ + */
@ -73,6 +90,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * Represents the player's pitch + * Represents the player's pitch
+ */ + */
+ PITCH; + PITCH;
+ }
+
+ /**
+ * Represents flags that effect the entity's state on
+ * teleportation.
+ */
+ @ApiStatus.Experimental
+ enum EntityState implements TeleportFlag {
+ /**
+ * If all passengers should not be required to be removed prior to teleportation.
+ * <p>
+ * Note:
+ * Teleporting to a different world with this flag present while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ */
+ RETAIN_PASSENGERS,
+ /**
+ * If the entity should not be dismounted if they are riding another entity.
+ * <p>
+ * Note:
+ * Teleporting to a different world with this flag present while this entity is riding another entity will
+ * cause this teleportation to return false and not occur.
+ */
+ RETAIN_VEHICLE,
+ /**
+ * Indicates that a player should not have their current open inventory closed when teleporting.
+ * <p>
+ * Note:
+ * This option will be ignored when teleported to a different world.
+ */
+ RETAIN_OPEN_INVENTORY;
+ }
+ +
+} +}
diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java
@ -90,69 +139,26 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start - Teleport API + // Paper start - Teleport API
+ /** + /**
+ * Teleports this entity to the given location. + * Teleports this entity to the given location.
+ * <p>
+ * Note: Teleporting to a different world with ignorePassengers to true while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ * + *
+ * @param location New location to teleport this entity to + * @param location New location to teleport this entity to
+ * @param ignorePassengers If all passengers should not be required to be removed prior to teleportation + * @param teleportFlags Flags to be used in this teleportation
+ * @return <code>true</code> if the teleport was successful + * @return <code>true</code> if the teleport was successful
+ */ + */
+ @org.jetbrains.annotations.ApiStatus.Experimental + @org.jetbrains.annotations.ApiStatus.Experimental
+ default boolean teleport(@NotNull Location location, boolean ignorePassengers) { + default boolean teleport(@NotNull Location location, @NotNull io.papermc.paper.entity.TeleportFlag @NotNull... teleportFlags) {
+ return this.teleport(location, TeleportCause.PLUGIN, ignorePassengers); + return this.teleport(location, TeleportCause.PLUGIN, teleportFlags);
+ } + }
+ +
+ /** + /**
+ * Teleports this entity to the given location. + * Teleports this entity to the given location.
+ * <p>
+ * Note: Teleporting to a different world with ignorePassengers to true while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ * + *
+ * @param location New location to teleport this entity to + * @param location New location to teleport this entity to
+ * @param cause The cause of this teleportation + * @param cause The cause of this teleportation
+ * @param ignorePassengers If all passengers should not be required to be removed prior to teleportation + * @param teleportFlags Flags to be used in this teleportation
+ * @return <code>true</code> if the teleport was successful + * @return <code>true</code> if the teleport was successful
+ */ + */
+ @org.jetbrains.annotations.ApiStatus.Experimental + @org.jetbrains.annotations.ApiStatus.Experimental
+ default boolean teleport(@NotNull Location location, @NotNull TeleportCause cause, boolean ignorePassengers) { + boolean teleport(@NotNull Location location, @NotNull TeleportCause cause, @NotNull io.papermc.paper.entity.TeleportFlag @NotNull... teleportFlags);
+ return this.teleport(location, cause, ignorePassengers, true);
+ }
+
+ /**
+ * Teleports this entity to the given location.
+ * <p>
+ * Note: Teleporting to a different world with ignorePassengers to true while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ * Note: Teleporting to a different world with dismount to false while this entity is riding another entity will
+ * cause this teleportation to return false and not occur.
+ *
+ * @param location New location to teleport this entity to
+ * @param ignorePassengers If all passengers should not be required to be removed prior to teleportation
+ * @param dismount If the entity should be dismounted if they are riding another entity
+ * @return <code>true</code> if the teleport was successful
+ */
+ @org.jetbrains.annotations.ApiStatus.Experimental
+ default boolean teleport(@NotNull Location location, boolean ignorePassengers, boolean dismount) {
+ return this.teleport(location, TeleportCause.PLUGIN, ignorePassengers, dismount);
+ }
+
+ /**
+ * Teleports this entity to the given location.
+ * <p>
+ * Note: Teleporting to a different world with ignorePassengers to true while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ * Note: Teleporting to a different world with dismount to false while this entity is riding another entity will
+ * cause this teleportation to return false and not occur.
+ *
+ * @param location New location to teleport this entity to
+ * @param cause The cause of this teleportation
+ * @param ignorePassengers If all passengers should not be required to be removed prior to teleportation
+ * @param dismount If the entity should be dismounted if they are riding another entity
+ * @return <code>true</code> if the teleport was successful
+ */
+ @org.jetbrains.annotations.ApiStatus.Experimental
+ boolean teleport(@NotNull Location location, @NotNull TeleportCause cause, boolean ignorePassengers, boolean dismount);
+ // Paper end - Teleport API + // Paper end - Teleport API
+ +
/** /**
@ -177,28 +183,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ void setRotation(float yaw, float pitch); + void setRotation(float yaw, float pitch);
+ +
+ /** + /**
+ * Teleports this entity to the given location.
+ * <p>
+ * Note: Teleporting to a different world with ignorePassengers to true while the entity has entities riding it
+ * will cause this teleportation to return false and not occur.
+ * Note: Teleporting to a different world with dismount to false while this entity is riding another entity will
+ * cause this teleportation to return false and not occur.
+ *
+ * <p>
+ * Relative teleportation flags are only used client side, and cause the player to not lose velocity in that
+ * specific coordinate. The location of the teleportation will not change.
+ *
+ * @param location New location to teleport this entity to
+ * @param cause The cause of this teleportation
+ * @param ignorePassengers If all passengers should not be required to be removed prior to teleportation
+ * @param dismount If the entity should be dismounted if they are riding another entity
+ * @param teleportFlags Coordinates of the location that the client should handle as relative teleportation
+ * @return <code>true</code> if the teleport was successful
+ */
+ @org.jetbrains.annotations.ApiStatus.Experimental
+ boolean teleport(@NotNull Location location, @NotNull org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause, boolean ignorePassengers, boolean dismount, @NotNull io.papermc.paper.entity.RelativeTeleportFlag @NotNull... teleportFlags);
+
+ /**
+ * Causes the player to look towards the given position. + * Causes the player to look towards the given position.
+ * + *
+ * @param x x coordinate + * @param x x coordinate
@ -212,12 +196,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ /** + /**
+ * Causes the player to look towards the given location. + * Causes the player to look towards the given location.
+ * + *
+ * @param location Location to look at + * @param position Position to look at in the player's current world
+ * @param playerAnchor What part of player should face the location + * @param playerAnchor What part of player should face the location
+ */ + */
+ @org.jetbrains.annotations.ApiStatus.Experimental + @org.jetbrains.annotations.ApiStatus.Experimental
+ default void lookAt(@NotNull Location location, @NotNull io.papermc.paper.entity.LookAnchor playerAnchor) { + default void lookAt(@NotNull io.papermc.paper.math.Position position, @NotNull io.papermc.paper.entity.LookAnchor playerAnchor) {
+ this.lookAt(location.getX(), location.getY(), location.getZ(), playerAnchor); + this.lookAt(position.x(), position.y(), position.z(), playerAnchor);
+ } + }
+ +
+ /** + /**
@ -244,7 +228,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start - Teleport API + // Paper start - Teleport API
+ private boolean dismounted = true; + private boolean dismounted = true;
+ private final java.util.Set<io.papermc.paper.entity.RelativeTeleportFlag> teleportFlagSet; + private final java.util.Set<io.papermc.paper.entity.TeleportFlag.Relative> teleportFlagSet;
+ // Paper end + // Paper end
+ +
public PlayerTeleportEvent(@NotNull final Player player, @NotNull final Location from, @Nullable final Location to) { public PlayerTeleportEvent(@NotNull final Player player, @NotNull final Location from, @Nullable final Location to) {
@ -259,7 +243,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start - Teleport API + // Paper start - Teleport API
+ @org.jetbrains.annotations.ApiStatus.Experimental + @org.jetbrains.annotations.ApiStatus.Experimental
+ public PlayerTeleportEvent(@NotNull final Player player, @NotNull final Location from, @Nullable final Location to, @NotNull final TeleportCause cause, boolean dismounted, @NotNull java.util.Set<io.papermc.paper.entity.@NotNull RelativeTeleportFlag> teleportFlagSet) { + public PlayerTeleportEvent(@NotNull final Player player, @NotNull final Location from, @Nullable final Location to, @NotNull final TeleportCause cause, boolean dismounted, @NotNull java.util.Set<io.papermc.paper.entity.TeleportFlag.@NotNull Relative> teleportFlagSet) {
+ super(player, from, to); + super(player, from, to);
+ +
+ this.dismounted = dismounted; + this.dismounted = dismounted;
@ -294,7 +278,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ */ + */
+ @org.jetbrains.annotations.ApiStatus.Experimental + @org.jetbrains.annotations.ApiStatus.Experimental
+ @NotNull + @NotNull
+ public java.util.Set<io.papermc.paper.entity.@NotNull RelativeTeleportFlag> getRelativeTeleportationFlags() { + public java.util.Set<io.papermc.paper.entity.TeleportFlag.@NotNull Relative> getRelativeTeleportationFlags() {
+ return this.teleportFlagSet; + return this.teleportFlagSet;
+ } + }
+ // Paper end + // Paper end

View File

@ -16,7 +16,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
- PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause); - PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause);
+ // Paper start - Teleport API + // Paper start - Teleport API
+ Set<io.papermc.paper.entity.RelativeTeleportFlag> relativeFlags = java.util.EnumSet.noneOf(io.papermc.paper.entity.RelativeTeleportFlag.class); + Set<io.papermc.paper.entity.TeleportFlag.Relative> relativeFlags = java.util.EnumSet.noneOf(io.papermc.paper.entity.TeleportFlag.Relative.class);
+ for (net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument relativeArgument : set) { + for (net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument relativeArgument : set) {
+ relativeFlags.add(org.bukkit.craftbukkit.entity.CraftPlayer.toApiRelativeFlag(relativeArgument)); + relativeFlags.add(org.bukkit.craftbukkit.entity.CraftPlayer.toApiRelativeFlag(relativeArgument));
+ } + }
@ -39,17 +39,20 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@Override @Override
public boolean teleport(Location location, TeleportCause cause) { public boolean teleport(Location location, TeleportCause cause) {
+ // Paper start - Teleport passenger API + // Paper start - Teleport passenger API
+ return teleport(location, cause, false); + return teleport(location, cause, new io.papermc.paper.entity.TeleportFlag[0]);
+ } + }
+ +
+ @Override + @Override
+ public boolean teleport(Location location, TeleportCause cause, boolean ignorePassengers, boolean dismount) { + public boolean teleport(Location location, TeleportCause cause, io.papermc.paper.entity.TeleportFlag... flags) {
+ // Paper end + // Paper end
Preconditions.checkArgument(location != null, "location cannot be null"); Preconditions.checkArgument(location != null, "location cannot be null");
location.checkFinite(); location.checkFinite();
+ // Paper start - Teleport passenger API + // Paper start - Teleport passenger API
+ Set<io.papermc.paper.entity.TeleportFlag> flagSet = Set.of(flags);
+ boolean dismount = !flagSet.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_VEHICLE);
+ boolean ignorePassengers = flagSet.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_PASSENGERS);
+ // Don't allow teleporting between worlds while keeping passengers + // Don't allow teleporting between worlds while keeping passengers
+ if (ignorePassengers && this.entity.isVehicle() && location.getWorld() != this.getWorld()) { + if (flagSet.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_PASSENGERS) && this.entity.isVehicle() && location.getWorld() != this.getWorld()) {
+ return false; + return false;
+ } + }
+ +
@ -93,12 +96,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@Override @Override
public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) { public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) {
+ // Paper start - Teleport API + // Paper start - Teleport API
+ return this.teleport(location, cause, false); + return this.teleport(location, cause, new io.papermc.paper.entity.TeleportFlag[0]);
+ }
+
+ @Override
+ public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause, boolean ignorePassengers, boolean dismount) {
+ return this.teleport(location, cause, ignorePassengers, dismount, new io.papermc.paper.entity.RelativeTeleportFlag[0]);
+ } + }
+ +
+ @Override + @Override
@ -125,7 +123,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }; + };
+ } + }
+ +
+ public static net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument toNmsRelativeFlag(io.papermc.paper.entity.RelativeTeleportFlag apiFlag) { + public static net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument toNmsRelativeFlag(io.papermc.paper.entity.TeleportFlag.Relative apiFlag) {
+ return switch (apiFlag) { + return switch (apiFlag) {
+ case X -> net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.X; + case X -> net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.X;
+ case Y -> net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.Y; + case Y -> net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.Y;
@ -135,22 +133,35 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }; + };
+ } + }
+ +
+ public static io.papermc.paper.entity.RelativeTeleportFlag toApiRelativeFlag(net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument nmsFlag) { + public static io.papermc.paper.entity.TeleportFlag.Relative toApiRelativeFlag(net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument nmsFlag) {
+ return switch (nmsFlag) { + return switch (nmsFlag) {
+ case X -> io.papermc.paper.entity.RelativeTeleportFlag.X; + case X -> io.papermc.paper.entity.TeleportFlag.Relative.X;
+ case Y -> io.papermc.paper.entity.RelativeTeleportFlag.Y; + case Y -> io.papermc.paper.entity.TeleportFlag.Relative.Y;
+ case Z -> io.papermc.paper.entity.RelativeTeleportFlag.Z; + case Z -> io.papermc.paper.entity.TeleportFlag.Relative.Z;
+ case X_ROT -> io.papermc.paper.entity.RelativeTeleportFlag.PITCH; + case X_ROT -> io.papermc.paper.entity.TeleportFlag.Relative.PITCH;
+ case Y_ROT -> io.papermc.paper.entity.RelativeTeleportFlag.YAW; + case Y_ROT -> io.papermc.paper.entity.TeleportFlag.Relative.YAW;
+ }; + };
+ } + }
+ +
+ @Override + @Override
+ public boolean teleport(Location location, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause, boolean ignorePassengers, boolean dismount, io.papermc.paper.entity.RelativeTeleportFlag... flags) { + public boolean teleport(Location location, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause, io.papermc.paper.entity.TeleportFlag... flags) {
+ var relativeArguments = java.util.EnumSet.noneOf(net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.class); + java.util.Set<net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument> relativeArguments;
+ for (io.papermc.paper.entity.RelativeTeleportFlag flag : flags) { + java.util.Set<io.papermc.paper.entity.TeleportFlag> allFlags;
+ relativeArguments.add(toNmsRelativeFlag(flag)); + if (flags.length == 0) {
+ relativeArguments = Set.of();
+ allFlags = Set.of();
+ } else {
+ relativeArguments = java.util.EnumSet.noneOf(net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket.RelativeArgument.class);
+ allFlags = new HashSet<>();
+ for (io.papermc.paper.entity.TeleportFlag flag : flags) {
+ if (flag instanceof io.papermc.paper.entity.TeleportFlag.Relative relativeTeleportFlag) {
+ relativeArguments.add(toNmsRelativeFlag(relativeTeleportFlag));
+ } + }
+ allFlags.add(flag);
+ }
+ }
+ boolean dismount = !allFlags.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_VEHICLE);
+ boolean ignorePassengers = allFlags.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_PASSENGERS);
+ // Paper end - Teleport API + // Paper end - Teleport API
Preconditions.checkArgument(location != null, "location"); Preconditions.checkArgument(location != null, "location");
Preconditions.checkArgument(location.getWorld() != null, "location.world"); Preconditions.checkArgument(location.getWorld() != null, "location.world");
@ -187,6 +198,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
// SPIGOT-5509: Wakeup, similar to riding // SPIGOT-5509: Wakeup, similar to riding
if (this.isSleeping()) { if (this.isSleeping()) {
@@ -0,0 +0,0 @@ public class CraftPlayer extends CraftHumanEntity implements Player { @@ -0,0 +0,0 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
ServerLevel toWorld = ((CraftWorld) to.getWorld()).getHandle();
// Close any foreign inventory
- if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
+ if (this.getHandle().containerMenu != this.getHandle().inventoryMenu && !allFlags.contains(io.papermc.paper.entity.TeleportFlag.EntityState.RETAIN_OPEN_INVENTORY)) { // Paper
this.getHandle().closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.TELEPORT); // Paper
}
// Check if the fromWorld and toWorld are the same. // Check if the fromWorld and toWorld are the same.
if (fromWorld == toWorld) { if (fromWorld == toWorld) {