Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer teleportAsync #129

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.popcraft.bolt.lang.Translator;
import org.popcraft.bolt.protection.BlockProtection;
import org.popcraft.bolt.util.BoltComponents;
import org.popcraft.bolt.util.PaperUtil;
import org.popcraft.bolt.util.Profiles;
import org.popcraft.bolt.util.Protections;
import org.popcraft.bolt.util.SchedulerUtil;
Expand Down Expand Up @@ -84,7 +85,7 @@ private void runPage(final CommandSender sender, final Profile playerProfile, fi
blockProtectionsFromPlayer.stream().skip(skip).limit(RESULTS_PER_PAGE).forEach(blockProtection -> {
final World world = plugin.getServer().getWorld(blockProtection.getWorld());
final ClickEvent teleport = plugin.getCallbackManager().registerPlayerOnly(player -> {
player.teleport(new Location(world, blockProtection.getX() + 0.5, blockProtection.getY(), blockProtection.getZ() + 0.5));
PaperUtil.teleportAsync(player, new Location(world, blockProtection.getX() + 0.5, blockProtection.getY(), blockProtection.getZ() + 0.5));
});
BoltComponents.sendMessage(
sender,
Expand Down
17 changes: 17 additions & 0 deletions paper/src/main/java/org/popcraft/bolt/util/PaperUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.Vector;

Expand All @@ -14,6 +16,7 @@ public class PaperUtil {
private static boolean getOfflinePlayerIfCachedExists;
private static boolean getChunkAtAsyncExists;
private static boolean getClickedPositionExists;
private static boolean teleportAsyncExists;

static {
try {
Expand All @@ -34,6 +37,12 @@ public class PaperUtil {
} catch (NoSuchMethodException e) {
getClickedPositionExists = false;
}
try {
Entity.class.getMethod("teleportAsync", Location.class);
teleportAsyncExists = true;
} catch (NoSuchMethodException e) {
teleportAsyncExists = false;
}
}

private PaperUtil() {
Expand Down Expand Up @@ -68,6 +77,14 @@ public static Vector getClickedPosition(final PlayerInteractEvent e) {
}
}

public static CompletableFuture<Boolean> teleportAsync(final Entity entity, final Location location) {
if (teleportAsyncExists) {
return entity.teleportAsync(location);
} else {
return CompletableFuture.completedFuture(entity.teleport(location));
}
}

private static boolean classExists(final String clazz) {
try {
Class.forName(clazz);
Expand Down