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
+15 -20

No files matched your search

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