Correct unsafe future joining

This commit is contained in:
William278 committed 2022-09-07 22:36:06 +03:00
1 parent adac00ea47
commit 950a51c140
14 files changed
+42 -42

No files matched your search

@@ -174,10 +174,10 @@ public interface HuskHomes {
*
* @return the {@link Position} of the spawn, or an empty {@link Optional} if it has not been set
*/
default Optional<? extends Position> getSpawn() {
return getSettings().crossServer && getSettings().globalSpawn
default CompletableFuture<Optional<? extends Position>> getSpawn() {
return CompletableFuture.supplyAsync(() -> getSettings().crossServer && getSettings().globalSpawn
? getDatabase().getWarp(getSettings().globalSpawnName).join()
: getLocalCachedSpawn().flatMap(spawn -> spawn.getPosition(getPluginServer()));
: getLocalCachedSpawn().flatMap(spawn -> spawn.getPosition(getPluginServer())));
}
/**
@@ -203,7 +203,7 @@ public abstract class BaseHuskHomesAPI {
* @return A {@link CompletableFuture} that will complete with the {@link Position} of the spawn point
*/
public final CompletableFuture<Optional<? extends Position>> getSpawn() {
return CompletableFuture.supplyAsync(plugin::getSpawn);
return plugin.getSpawn();
}
/**
@@ -121,7 +121,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
for (final Home toDelete : homes) {
homeDeletionFuture.add(plugin.getSavedPositionManager().deleteHome(homeOwner, toDelete.meta.name));
}
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRun(() ->
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRunAsync(() ->
plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(homeDeletionFuture.size()))
.ifPresent(deleter::sendMessage)).join();
});
@@ -68,7 +68,7 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
for (final Warp toDelete : warps) {
homeDeletionFuture.add(plugin.getSavedPositionManager().deleteWarp(toDelete.meta.name));
}
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRun(() ->
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])).thenRunAsync(() ->
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(homeDeletionFuture.size()))
.ifPresent(deleter::sendMessage)).join();
});
@@ -44,7 +44,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable {
return;
}
}
plugin.getDatabase().getHome(userData.user(), homeIdentifier.homeName()).thenAccept(optionalHome -> {
plugin.getDatabase().getHome(userData.user(), homeIdentifier.homeName()).thenAcceptAsync(optionalHome -> {
if (optionalHome.isEmpty()) {
plugin.getLocales().getLocale("error_home_invalid_other",
homeIdentifier.ownerName(), homeIdentifier.homeName())
@@ -26,7 +26,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> {
case 0 -> plugin.getDatabase().getPublicHomes().thenAcceptAsync(publicHomes -> {
// Display the public home list if there are any public home set
if (publicHomes.size() == 0) {
plugin.getLocales().getLocale("error_no_public_homes_set").ifPresent(onlineUser::sendMessage);
@@ -47,7 +47,7 @@ public class PublicHomeListCommand extends CommandBase implements ConsoleExecuta
return;
}
plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> {
plugin.getDatabase().getPublicHomes().thenAcceptAsync(publicHomes -> {
if (publicHomes.isEmpty()) {
plugin.getLocales().getLocale("error_no_public_homes_set").ifPresent(onlineUser::sendMessage);
return;
@@ -5,6 +5,7 @@ import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.UserData;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
import net.william278.huskhomes.util.Permission;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;
@@ -70,25 +71,27 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
}
// Get a random position and teleport
plugin.getRandomTeleportEngine().getRandomPosition(onlineUser, rtpArguments)
.thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
.ifPresent(onlineUser::sendMessage);
return;
}
(isExecutorTeleporting ? plugin.getTeleportManager().timedTeleport(userToTeleport, position.get(), Settings.EconomyAction.RANDOM_TELEPORT)
: plugin.getTeleportManager().teleport(userToTeleport, position.get())).thenAccept(result -> {
if (isExecutorTeleporting &&
result.successful && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
plugin.getDatabase().updateUserData(new UserData(onlineUser,
userData.get().homeSlots(), userData.get().ignoringTeleports(),
Instant.now().plus(plugin.getSettings().rtpCooldownLength, ChronoUnit.MINUTES)));
}
plugin.getTeleportManager().finishTeleport(userToTeleport, result,
Settings.EconomyAction.RANDOM_TELEPORT);
}).join();
}).join();
plugin.getRandomTeleportEngine().getRandomPosition(onlineUser, rtpArguments).thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
.ifPresent(onlineUser::sendMessage);
return;
}
final CompletableFuture<TeleportResult> randomTeleport = isExecutorTeleporting ? plugin.getTeleportManager()
.timedTeleport(userToTeleport, position.get(), Settings.EconomyAction.RANDOM_TELEPORT)
: plugin.getTeleportManager().teleport(userToTeleport, position.get());
randomTeleport.thenAccept(result -> {
if (isExecutorTeleporting &&
result.successful && !onlineUser.hasPermission(Permission.BYPASS_RTP_COOLDOWN.node)) {
plugin.getDatabase().updateUserData(new UserData(onlineUser,
userData.get().homeSlots(), userData.get().ignoringTeleports(),
Instant.now().plus(plugin.getSettings().rtpCooldownLength, ChronoUnit.MINUTES)));
}
plugin.getTeleportManager().finishTeleport(userToTeleport, result,
Settings.EconomyAction.RANDOM_TELEPORT);
});
});
});
}
@@ -5,8 +5,6 @@ import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public class SpawnCommand extends CommandBase {
protected SpawnCommand(@NotNull HuskHomes implementor) {
@@ -21,7 +19,7 @@ public class SpawnCommand extends CommandBase {
return;
}
CompletableFuture.supplyAsync(plugin::getSpawn).thenAccept(position -> {
plugin.getSpawn().thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_spawn_not_set")
.ifPresent(onlineUser::sendMessage);
@@ -26,7 +26,7 @@ public class TpIgnoreCommand extends CommandBase {
plugin.getRequestManager().setIgnoringRequests(onlineUser, isIgnoringRequests);
// Update value on the database and send a message
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(
plugin.getDatabase().getUserData(onlineUser.uuid).thenAcceptAsync(
userData -> userData.ifPresent(data -> plugin.getDatabase().updateUserData(new UserData(onlineUser,
data.homeSlots(), isIgnoringRequests, data.rtpCooldown())).join())).thenRun(
() -> plugin.getLocales().getRawLocale("tpignore_toggle_" + (isIgnoringRequests ? "on" : "off"),
@@ -39,8 +39,8 @@ public class TpOfflineCommand extends CommandBase implements TabCompletable {
plugin.getLocales().getLocale("teleporting_offline_player", targetUser)
.ifPresent(onlineUser::sendMessage);
plugin.getTeleportManager().teleport(onlineUser, offlinePosition.get()).thenAccept(
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
}).join();
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result));
});
});
}
@@ -41,7 +41,7 @@ public class EventListener {
firstUser = false;
plugin.fetchServer(onlineUser).join();
}
}).thenRun(() -> {
}).thenRunAsync(() -> {
// Ensure the player is present on the database first
plugin.getDatabase().ensureUser(onlineUser).join();
}).thenRun(() -> {
@@ -58,9 +58,8 @@ public class EventListener {
} else {
onlineUser.teleport(bedPosition.get(), plugin.getSettings().asynchronousTeleports);
}
plugin.getDatabase().setCurrentTeleport(onlineUser, null).thenRun(() ->
plugin.getDatabase().setRespawnPosition(onlineUser, bedPosition.orElse(null))
.join()).join();
plugin.getDatabase().setCurrentTeleport(onlineUser, null).thenRunAsync(() ->
plugin.getDatabase().setRespawnPosition(onlineUser, bedPosition.orElse(null)).join());
return;
}
@@ -81,7 +80,7 @@ public class EventListener {
}).thenRun(() -> {
// Update the cached player list
plugin.getCache().updatePlayerListCache(plugin, onlineUser);
}).thenRun(() -> {
}).thenRunAsync(() -> {
// Get this user's tp-ignore state and set locally
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> {
if (userData.isPresent()) {
@@ -139,7 +139,7 @@ public class RequestManager {
// If the player couldn't be found locally, send the request cross-server
if (plugin.getSettings().crossServer) {
// Find the matching networked target
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> {
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApplyAsync(networkedTarget -> {
if (networkedTarget.isEmpty()) {
return Optional.empty();
}
@@ -197,7 +197,7 @@ public class TeleportManager {
*/
public CompletableFuture<Optional<TeleportResult>> teleportNamedPlayers(@NotNull String playerName, @NotNull String targetPlayer,
@NotNull OnlineUser requester, final boolean timed) {
return getPlayerPositionByName(requester, targetPlayer).thenApply(position -> {
return getPlayerPositionByName(requester, targetPlayer).thenApplyAsync(position -> {
if (position.isEmpty()) {
return Optional.empty();
}
@@ -375,7 +375,7 @@ public class TeleportManager {
private CompletableFuture<TeleportResult> teleportCrossServer(@NotNull OnlineUser onlineUser, @NotNull Teleport teleport) {
return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport)
.thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
.thenApplyAsync(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
.thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER :
TeleportResult.FAILED_INVALID_SERVER)
.join());