From 9332371c71279ecdd0bb54cad3c1650c74ac41c8 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 18 Aug 2022 16:44:49 +0100 Subject: [PATCH] Various fixes for /tp --- .../huskhomes/command/TpCommand.java | 25 ++++++++++++++++--- .../huskhomes/position/Position.java | 10 +++++++- .../huskhomes/teleport/TeleportManager.java | 8 +++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java index 273fc707..ad4ba3a8 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.concurrent.CompletableFuture; public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable { @@ -36,7 +37,8 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe targetPosition = new TeleportCommandTarget(args[0]); } else { // Determine if a player argument has been passed before the destination argument - int coordinatesIndex = (args[0].matches("^[a-zA-Z0-9_.*]{1,17}$") || args.length == 4 || args.length == 6) ? 1 : 0; + int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3]))) + || (args.length == 2)) ? 1 : 0; if (coordinatesIndex == 1) { userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0])) .findFirst().orElse(plugin.getOnlinePlayers().stream().filter(user -> user.username.startsWith(args[0])) @@ -60,7 +62,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe // Parse the coordinates and set the target position final Position userPosition = onlineUser.getPosition().join(); final Optional parsedPosition = Position.parse(userPosition, - Arrays.copyOfRange(args, coordinatesIndex, args.length - 1)); + Arrays.copyOfRange(args, coordinatesIndex, args.length)); if (parsedPosition.isEmpty()) { plugin.getLocales().getLocale("error_invalid_syntax", "/tp [world] [server]") .ifPresent(onlineUser::sendMessage); @@ -74,15 +76,32 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe final OnlineUser teleportingUser = userToTeleport; if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) { assert targetPosition.targetPlayer != null; - plugin.getTeleportManager().teleportToPlayer(teleportingUser, targetPosition.targetPlayer); + CompletableFuture.runAsync(() -> plugin.getTeleportManager().teleportToPlayer(teleportingUser, targetPosition.targetPlayer)); + return; } else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) { assert targetPosition.targetPosition != null; plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept( result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result)); + return; } throw new HuskHomesException("Attempted to execute invalid teleport command operation"); } + private boolean isCoordinate(@NotNull String coordinate) { + try { + if (coordinate.startsWith("~")) { + coordinate = coordinate.substring(1); + } + if (coordinate.isBlank()) { + return true; + } + Double.parseDouble(coordinate); + return true; + } catch (NumberFormatException e) { + return false; + } + } + @Override public void onConsoleExecute(@NotNull String[] args) { //todo diff --git a/common/src/main/java/net/william278/huskhomes/position/Position.java b/common/src/main/java/net/william278/huskhomes/position/Position.java index f7beffa4..fe06c74c 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Position.java +++ b/common/src/main/java/net/william278/huskhomes/position/Position.java @@ -32,8 +32,11 @@ public class Position extends Location { * @return The position if it could be parsed, otherwise an empty optional */ public static Optional parse(@NotNull Position currentPosition, @NotNull String... values) { + System.out.println(String.join(" ", values)); + // Validate argument length if (values.length < 3 || values.length > 5) { + System.out.println("Invalid syntax"); return Optional.empty(); } @@ -59,8 +62,13 @@ public class Position extends Location { * @throws NumberFormatException If the string could not be parsed */ private static double parseCoordinate(@NotNull String value, double currentValue) throws NumberFormatException { + // Future: Consider supporting ^ ^ ^ (position-relative-to-facing) if (value.startsWith("~")) { - return currentValue + Double.parseDouble(value.substring(1)); + if (value.length() == 1) { + return currentValue; + } else { + return currentValue + Double.parseDouble(value.substring(1)); + } } else { return Double.parseDouble(value); } 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 fbf3710a..29efee2e 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -156,14 +156,14 @@ public class TeleportManager { if (currentlyOnWarmup.contains(onlineUser.uuid)) { return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING); } - return processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime)) + return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime)) .thenApply(teleport -> { if (!teleport.cancelled) { - return teleportNow(onlineUser, position); + return teleportNow(onlineUser, position).join(); } else { - return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED); + return TeleportResult.CANCELLED; } - }).join(); + }).join()); } else { return teleportNow(onlineUser, position); }