mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
Reduce unnecessary future nesting
This commit is contained in:
1 parent
a91128ca3d
commit
d494797aa4
1 file changed
+10
-14
@@ -114,11 +114,10 @@ public abstract class TeleportManager {
|
|||||||
* @return future optionally supplying the player's position, if the player could be found
|
* @return future optionally supplying the player's position, if the player could be found
|
||||||
*/
|
*/
|
||||||
private CompletableFuture<Optional<Position>> getPlayerPosition(@NotNull OnlineUser requester, @NotNull String playerName) {
|
private CompletableFuture<Optional<Position>> getPlayerPosition(@NotNull OnlineUser requester, @NotNull String playerName) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
|
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
|
||||||
player.username.equalsIgnoreCase(playerName)).findFirst();
|
player.username.equalsIgnoreCase(playerName)).findFirst();
|
||||||
if (localPlayer.isPresent()) {
|
if (localPlayer.isPresent()) {
|
||||||
return localPlayer.get().getPosition().thenApply(Optional::of).join();
|
return localPlayer.get().getPosition().thenApply(Optional::of);
|
||||||
}
|
}
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
assert plugin.getNetworkMessenger() != null;
|
assert plugin.getNetworkMessenger() != null;
|
||||||
@@ -129,10 +128,9 @@ public abstract class TeleportManager {
|
|||||||
new EmptyPayload(),
|
new EmptyPayload(),
|
||||||
Message.MessageKind.MESSAGE,
|
Message.MessageKind.MESSAGE,
|
||||||
plugin.getSettings().clusterId))
|
plugin.getSettings().clusterId))
|
||||||
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload))).join();
|
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload)));
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return CompletableFuture.supplyAsync(Optional::empty);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -142,21 +140,19 @@ public abstract class TeleportManager {
|
|||||||
* @param position the target {@link Position} to teleport to
|
* @param position the target {@link Position} to teleport to
|
||||||
*/
|
*/
|
||||||
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
|
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
|
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
|
||||||
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
|
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
|
||||||
return processTimedTeleport(new TimedTeleport(onlineUser, position, teleportWarmupTime))
|
return processTimedTeleport(new TimedTeleport(onlineUser, position, teleportWarmupTime))
|
||||||
.thenApply(teleport -> {
|
.thenApply(teleport -> {
|
||||||
if (!teleport.cancelled) {
|
if (!teleport.cancelled) {
|
||||||
return teleportNow(onlineUser, position).join();
|
return teleportNow(onlineUser, position);
|
||||||
} else {
|
} else {
|
||||||
return TeleportResult.CANCELLED;
|
return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED);
|
||||||
}
|
}
|
||||||
}).join();
|
}).join();
|
||||||
} else {
|
} else {
|
||||||
return teleportNow(onlineUser, position).join();
|
return teleportNow(onlineUser, position);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,7 +185,7 @@ public abstract class TeleportManager {
|
|||||||
*/
|
*/
|
||||||
private CompletableFuture<TeleportResult> teleportNow(@NotNull OnlineUser onlineUser, @NotNull Position position) {
|
private CompletableFuture<TeleportResult> teleportNow(@NotNull OnlineUser onlineUser, @NotNull Position position) {
|
||||||
final Teleport teleport = new Teleport(onlineUser, position);
|
final Teleport teleport = new Teleport(onlineUser, position);
|
||||||
return CompletableFuture.supplyAsync(() -> onlineUser.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase()
|
return onlineUser.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase()
|
||||||
.setLastPosition(onlineUser, preTeleportPosition) // Update the player's last position
|
.setLastPosition(onlineUser, preTeleportPosition) // Update the player's last position
|
||||||
.thenApply(ignored -> plugin.getServer(onlineUser).thenApply(server -> {
|
.thenApply(ignored -> plugin.getServer(onlineUser).thenApply(server -> {
|
||||||
// Teleport player locally, or across server depending on need
|
// Teleport player locally, or across server depending on need
|
||||||
@@ -198,16 +194,16 @@ public abstract class TeleportManager {
|
|||||||
} else {
|
} else {
|
||||||
return teleportCrossServer(onlineUser, teleport).join();
|
return teleportCrossServer(onlineUser, teleport).join();
|
||||||
}
|
}
|
||||||
}).join()).join()).join());
|
}).join()).join());
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<TeleportResult> teleportCrossServer(OnlineUser onlineUser, Teleport teleport) {
|
private CompletableFuture<TeleportResult> teleportCrossServer(OnlineUser onlineUser, Teleport teleport) {
|
||||||
assert plugin.getNetworkMessenger() != null;
|
assert plugin.getNetworkMessenger() != null;
|
||||||
return CompletableFuture.supplyAsync(() -> plugin.getDatabase().setCurrentTeleport(teleport.player, teleport)
|
return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport)
|
||||||
.thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
|
.thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
|
||||||
.thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER :
|
.thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER :
|
||||||
TeleportResult.FAILED_INVALID_SERVER)
|
TeleportResult.FAILED_INVALID_SERVER)
|
||||||
.join()).join());
|
.join());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user