From 973ae90905f054f2b5beef902a4b77fe580ce148 Mon Sep 17 00:00:00 2001 From: William278 Date: Sat, 3 Sep 2022 15:08:55 +0300 Subject: [PATCH] De-nest futures on teleport and timedTeleport, fix crash, tweak SpawnCommand logic --- .../huskhomes/player/BukkitPlayer.java | 38 +++++++++---------- .../huskhomes/command/SpawnCommand.java | 24 ++++++------ .../huskhomes/listener/EventListener.java | 7 ++-- .../huskhomes/teleport/TeleportManager.java | 22 +++++------ 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java index 4516bcad..ef5b4b00 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -118,28 +118,26 @@ public class BukkitPlayer extends OnlineUser { @Override public CompletableFuture teleport(@NotNull Location location, boolean asynchronous) { - return CompletableFuture.supplyAsync(() -> { - final Optional bukkitLocation = BukkitAdapter.adaptLocation(location); - if (bukkitLocation.isEmpty()) { - return TeleportResult.FAILED_INVALID_WORLD; + final Optional 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 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 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 diff --git a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java index 2d96bc2c..df1980d4 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java @@ -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 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()); + }); } } diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index 88d9c554..a8606615 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -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 diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java index 09bc4acf..ffb88da9 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -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); }