Async teleport is now default >v<

This commit is contained in:
HaHaWTH
2024-05-30 22:15:21 +08:00
parent ae76066db6
commit 42e907580c
7 changed files with 23 additions and 56 deletions
@@ -3,24 +3,24 @@ package fr.xephi.authme.util;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.concurrent.CompletableFuture;
/**
* This class is a utility class for handling async teleportation of players in game.
*/
public class TeleportUtils {
private static Method teleportAsyncMethod;
private static MethodHandle teleportAsyncMethodHandle;
static {
try {//Detect Paper class
Class<?> paperClass = Class.forName("com.destroystokyo.paper.PaperConfig");
teleportAsyncMethod = Player.class.getMethod("teleportAsync", Location.class);
teleportAsyncMethod.setAccessible(true);
// if detected,use teleportAsync()
} catch (ClassNotFoundException | NoSuchMethodException e) {
teleportAsyncMethod = null;
//if not, set method to null
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
teleportAsyncMethodHandle = lookup.findVirtual(Player.class, "teleportAsync", MethodType.methodType(CompletableFuture.class, Location.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
teleportAsyncMethodHandle = null;
// if not, set method handle to null
}
}
@@ -31,10 +31,10 @@ public class TeleportUtils {
* @param location Where should the player be teleported
*/
public static void teleport(Player player, Location location) {
if (teleportAsyncMethod != null) {
if (teleportAsyncMethodHandle != null) {
try {
teleportAsyncMethod.invoke(player, location);
} catch (IllegalAccessException | InvocationTargetException e) {
teleportAsyncMethodHandle.invoke(player, location);
} catch (Throwable throwable) {
player.teleport(location);
}
} else {