Skip to content

Commit 2a2cf2d

Browse files
committed
Update passenger teleport support
Paper retains passengers automatically in 1.21.10+
1 parent 982ad97 commit 2a2cf2d

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCEntity.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@
3838

3939
public class BukkitMCEntity extends BukkitMCMetadatable implements MCEntity {
4040

41-
Entity e;
42-
private static volatile Boolean isPaperTeleportFlag = null;
43-
private static final Object IS_PAPER_TELEPORT_FLAG_LOCK = new Object();
41+
private static final boolean USE_RETAIN_PASSENGERS;
42+
43+
static {
44+
boolean useRetainPassengers = false;
45+
try {
46+
Class<?> clz = Class.forName("io.papermc.paper.entity.TeleportFlag$EntityState");
47+
if(clz.getAnnotation(Deprecated.class) == null) {
48+
useRetainPassengers = true;
49+
}
50+
} catch(ClassNotFoundException ignored) {}
51+
USE_RETAIN_PASSENGERS = useRetainPassengers;
52+
}
53+
54+
private final Entity e;
4455

4556
public BukkitMCEntity(Entity e) {
4657
super(e);
@@ -254,30 +265,13 @@ public boolean teleport(MCLocation location) {
254265

255266
@Override
256267
public boolean teleport(MCLocation location, MCTeleportCause cause) {
257-
@SuppressWarnings("LocalVariableHidesMemberVariable")
258-
Boolean isPaperTeleportFlag = BukkitMCEntity.isPaperTeleportFlag;
259-
if(isPaperTeleportFlag == null) {
260-
synchronized(IS_PAPER_TELEPORT_FLAG_LOCK) {
261-
isPaperTeleportFlag = BukkitMCEntity.isPaperTeleportFlag;
262-
if(isPaperTeleportFlag == null) {
263-
try {
264-
// 1.19.3
265-
Class.forName("io.papermc.paper.entity.TeleportFlag$EntityState");
266-
BukkitMCEntity.isPaperTeleportFlag = isPaperTeleportFlag = true;
267-
} catch(ClassNotFoundException ex) {
268-
BukkitMCEntity.isPaperTeleportFlag = isPaperTeleportFlag = false;
269-
}
270-
}
271-
}
272-
}
273-
Location l = ((BukkitMCLocation) location).asLocation();
268+
Location l = (Location) location.getHandle();
274269
TeleportCause c = TeleportCause.valueOf(cause.name());
275-
if(isPaperTeleportFlag) {
276-
// Paper requires a third parameter to maintain consistent behavior when teleporting
277-
// entities with passengers.
270+
if(USE_RETAIN_PASSENGERS) {
271+
// Paper requires a third parameter to maintain consistent behavior when teleporting entities with passengers.
272+
// TeleportFlag.EntityState added in 1.19.3 but RETAIN_PASSENGERS was made default in 1.21.10.
278273
TeleportFlag teleportFlag = TeleportFlag.EntityState.RETAIN_PASSENGERS;
279-
// Paper only method:
280-
// e.teleport(l, c, teleportFlag);
274+
// Paper only method: e.teleport(l, c, teleportFlag);
281275
return ReflectionUtils.invokeMethod(Entity.class, e, "teleport",
282276
new Class[] {
283277
org.bukkit.Location.class,

src/main/java/com/laytonsmith/core/functions/EntityManagement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ public String getName() {
470470
@Override
471471
public String docs() {
472472
return "boolean {entityUUID, locationArray} Teleports the entity to the given location and returns whether"
473-
+ " the action was successful. Note this can set both location and direction. On paper servers,"
474-
+ " this teleports passengers along with the entity.";
473+
+ " the action was successful. Note this can set both location and direction. On Paper 1.19.3+"
474+
+ " servers, this teleports passengers along with the entity instead of canceling the teleport.";
475475
}
476476

477477
@Override

src/main/java/com/laytonsmith/core/functions/PlayerManagement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@ public Integer[] numArgs() {
490490
public String docs() {
491491
return "boolean {[player], locationArray | [player], x, y, z} Sets the location of the player to the"
492492
+ " specified coordinates. If the coordinates are not valid, or the player was otherwise prevented"
493-
+ " from teleporting, false is returned, otherwise true. If player is omitted, the current player"
494-
+ " is used. Note that 1 is automatically added to the y coordinate.";
493+
+ " from teleporting, false is returned, otherwise true. On Paper 1.19.3+, passengers do not"
494+
+ " automatically prevent teleports. If player is omitted, the current player is used. Important:"
495+
+ " unlike {{function|set_entity_loc}}, 1.0 is automatically added to the y coordinate.";
495496
}
496497

497498
@Override

0 commit comments

Comments
 (0)