Various fixes for /tp

This commit is contained in:
William committed 2022-08-18 16:44:49 +01:00
1 parent cf6d9a6079
commit 9332371c71
3 files changed
+35 -8

No files matched your search

@@ -12,6 +12,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable { 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]); targetPosition = new TeleportCommandTarget(args[0]);
} else { } else {
// Determine if a player argument has been passed before the destination argument // 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) { if (coordinatesIndex == 1) {
userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0])) userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0]))
.findFirst().orElse(plugin.getOnlinePlayers().stream().filter(user -> user.username.startsWith(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 // Parse the coordinates and set the target position
final Position userPosition = onlineUser.getPosition().join(); final Position userPosition = onlineUser.getPosition().join();
final Optional<Position> parsedPosition = Position.parse(userPosition, final Optional<Position> parsedPosition = Position.parse(userPosition,
Arrays.copyOfRange(args, coordinatesIndex, args.length - 1)); Arrays.copyOfRange(args, coordinatesIndex, args.length));
if (parsedPosition.isEmpty()) { if (parsedPosition.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax", "/tp <target> <x> <y> <z> [world] [server]") plugin.getLocales().getLocale("error_invalid_syntax", "/tp <target> <x> <y> <z> [world] [server]")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -74,15 +76,32 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
final OnlineUser teleportingUser = userToTeleport; final OnlineUser teleportingUser = userToTeleport;
if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) { if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) {
assert targetPosition.targetPlayer != null; 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) { } else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) {
assert targetPosition.targetPosition != null; assert targetPosition.targetPosition != null;
plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept( plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept(
result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result)); result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result));
return;
} }
throw new HuskHomesException("Attempted to execute invalid teleport command operation"); 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 @Override
public void onConsoleExecute(@NotNull String[] args) { public void onConsoleExecute(@NotNull String[] args) {
//todo //todo
@@ -32,8 +32,11 @@ public class Position extends Location {
* @return The position if it could be parsed, otherwise an empty optional * @return The position if it could be parsed, otherwise an empty optional
*/ */
public static Optional<Position> parse(@NotNull Position currentPosition, @NotNull String... values) { public static Optional<Position> parse(@NotNull Position currentPosition, @NotNull String... values) {
System.out.println(String.join(" ", values));
// Validate argument length // Validate argument length
if (values.length < 3 || values.length > 5) { if (values.length < 3 || values.length > 5) {
System.out.println("Invalid syntax");
return Optional.empty(); return Optional.empty();
} }
@@ -59,8 +62,13 @@ public class Position extends Location {
* @throws NumberFormatException If the string could not be parsed * @throws NumberFormatException If the string could not be parsed
*/ */
private static double parseCoordinate(@NotNull String value, double currentValue) throws NumberFormatException { private static double parseCoordinate(@NotNull String value, double currentValue) throws NumberFormatException {
// Future: Consider supporting ^ ^ ^ (position-relative-to-facing)
if (value.startsWith("~")) { 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 { } else {
return Double.parseDouble(value); return Double.parseDouble(value);
} }
@@ -156,14 +156,14 @@ public class TeleportManager {
if (currentlyOnWarmup.contains(onlineUser.uuid)) { if (currentlyOnWarmup.contains(onlineUser.uuid)) {
return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING); 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 -> { .thenApply(teleport -> {
if (!teleport.cancelled) { if (!teleport.cancelled) {
return teleportNow(onlineUser, position); return teleportNow(onlineUser, position).join();
} else { } else {
return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED); return TeleportResult.CANCELLED;
} }
}).join(); }).join());
} else { } else {
return teleportNow(onlineUser, position); return teleportNow(onlineUser, position);
} }