De-nest futures on teleport and timedTeleport, fix crash, tweak SpawnCommand logic

This commit is contained in:
William278 committed 2022-09-03 15:08:55 +03:00
1 parent 4832d69131
commit 973ae90905
4 files changed
+43 -48

No files matched your search

@@ -118,28 +118,26 @@ public class BukkitPlayer extends OnlineUser {
@Override
public CompletableFuture<TeleportResult> teleport(@NotNull Location location, boolean asynchronous) {
return CompletableFuture.supplyAsync(() -> {
final Optional<org.bukkit.Location> bukkitLocation = BukkitAdapter.adaptLocation(location);
if (bukkitLocation.isEmpty()) {
return TeleportResult.FAILED_INVALID_WORLD;
final Optional<org.bukkit.Location> bukkitLocation = BukkitAdapter.adaptLocation(location);
if (bukkitLocation.isEmpty()) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_INVALID_WORLD);
}
assert bukkitLocation.get().getWorld() != null;
if (!bukkitLocation.get().getWorld().getWorldBorder().isInside(bukkitLocation.get())) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_ILLEGAL_COORDINATES);
}
final CompletableFuture<TeleportResult> resultCompletableFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(BukkitHuskHomes.getInstance(), () -> {
if (asynchronous) {
PaperLib.teleportAsync(player, bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN)
.thenAccept(result -> resultCompletableFuture.complete(
result ? TeleportResult.COMPLETED_LOCALLY : TeleportResult.FAILED_INVALID_WORLD));
} else {
player.teleport(bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN);
resultCompletableFuture.complete(TeleportResult.COMPLETED_LOCALLY);
}
assert bukkitLocation.get().getWorld() != null;
if (!bukkitLocation.get().getWorld().getWorldBorder().isInside(bukkitLocation.get())) {
return TeleportResult.FAILED_ILLEGAL_COORDINATES;
}
final CompletableFuture<TeleportResult> resultCompletableFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(BukkitHuskHomes.getInstance(), () -> {
if (asynchronous) {
PaperLib.teleportAsync(player, bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN)
.thenAccept(result -> resultCompletableFuture.complete(
result ? TeleportResult.COMPLETED_LOCALLY : TeleportResult.FAILED_INVALID_WORLD));
} else {
player.teleport(bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN);
resultCompletableFuture.complete(TeleportResult.COMPLETED_LOCALLY);
}
});
return resultCompletableFuture.join();
});
return resultCompletableFuture;
}
@Override
@@ -2,11 +2,9 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class SpawnCommand extends CommandBase {
@@ -23,16 +21,16 @@ public class SpawnCommand extends CommandBase {
return;
}
CompletableFuture.runAsync(() -> {
final Optional<? extends Position> position = (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn
? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join()
: plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer())));
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_spawn_not_set").ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().timedTeleport(onlineUser, position.get()).join();
});
CompletableFuture.supplyAsync(() -> plugin.getSettings().crossServer && plugin.getSettings().globalSpawn
? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join()
: plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer())))
.thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_spawn_not_set")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().timedTeleport(onlineUser, position.get());
});
}
}
@@ -56,7 +56,7 @@ public class EventListener {
.ifPresent(position -> onlineUser.teleport(position, plugin.getSettings().asynchronousTeleports));
onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid");
} else {
onlineUser.teleport(bedPosition.get(), plugin.getSettings().asynchronousTeleports).join();
onlineUser.teleport(bedPosition.get(), plugin.getSettings().asynchronousTeleports);
}
plugin.getDatabase().setCurrentTeleport(onlineUser, null).thenRun(() ->
plugin.getDatabase().setRespawnPosition(onlineUser, bedPosition.orElse(null))
@@ -75,9 +75,8 @@ public class EventListener {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_COMPLETE)
.ifPresent(onlineUser::playSound);
}
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))
.join();
})).join();
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null));
}));
}
}).thenRun(() -> {
// Update the cached player list
@@ -261,19 +261,19 @@ public class TeleportManager {
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime))
.thenApply(teleport -> {
if (!teleport.cancelled) {
for (final Settings.EconomyAction action : economyActions) {
if (!plugin.validateEconomyCheck(onlineUser, action)) {
return TeleportResult.CANCELLED;
}
}
return teleport(onlineUser, position).join();
} else {
final TimedTeleport timedTeleport = new TimedTeleport(onlineUser, position, teleportWarmupTime);
return processTeleportWarmup(timedTeleport).thenApplyAsync(teleport -> {
if (!teleport.cancelled) {
for (final Settings.EconomyAction action : economyActions) {
if (!plugin.validateEconomyCheck(onlineUser, action)) {
return TeleportResult.CANCELLED;
}
}).join());
}
return teleport(onlineUser, position).join();
} else {
return TeleportResult.CANCELLED;
}
});
} else {
return teleport(onlineUser, position);
}