Skip to content

Commit

Permalink
Merge pull request #342 from PureGero/feature/teleport-async
Browse files Browse the repository at this point in the history
Use Paper's teleportAsync if available
  • Loading branch information
GeorgH93 authored Jul 18, 2024
2 parents 273dc66 + 2c77339 commit 4af0e07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void doTheTP(MarriagePlayer player, Marriage marriage)
player.send(messageNoHome);
return;
}
player.getPlayerOnline().teleport(marriage.getHome().getLocation());
TpCommand.teleportAsync(player.getPlayerOnline(), marriage.getHome().getLocation());
player.send(messageTPed);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class TpCommand extends MarryCommand
{
Expand Down Expand Up @@ -197,7 +200,7 @@ public void doTheTP(final @NotNull Player player, final @NotNull Player partner)
}
else
{
player.teleport(loc);
teleportAsync(player, loc);
messageTeleport.send(player);
messageTeleportTo.send(partner);
}
Expand All @@ -213,6 +216,16 @@ public void doTheTP(final @NotNull Player player, final @NotNull Player partner)
}
}

public static CompletableFuture<Boolean> teleportAsync(Player player, Location location) {
try {
Method method = player.getClass().getMethod("teleportAsync", Location.class);
return (CompletableFuture<Boolean>) method.invoke(player, location);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// paper's teleportAsync not found, fallback to bukkit's teleport
return CompletableFuture.completedFuture(player.teleport(location));
}
}

@AllArgsConstructor
private class TpToPartner implements DelayableTeleportAction
{
Expand Down

0 comments on commit 4af0e07

Please sign in to comment.