From 9e58fc3a322f61661ced1ab6be0c90bf6e8e2a7b Mon Sep 17 00:00:00 2001 From: William Date: Wed, 24 Aug 2022 23:17:55 +0100 Subject: [PATCH] Console /tp command --- .../huskhomes/command/TpCommand.java | 50 ++++++++++++++++++- .../huskhomes/position/Position.java | 2 - .../william278/huskhomes/position/Server.java | 1 - 3 files changed, 49 insertions(+), 4 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 d5481ffb..be41e76d 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -4,11 +4,14 @@ import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomesException; import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.position.World; import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.logging.Level; import java.util.stream.Collectors; public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable { @@ -125,7 +128,52 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe @Override public void onConsoleExecute(@NotNull String[] args) { - //todo + if (args.length < 2 || args.length > 6) { + plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax: /tp "); + return; + } + final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); + if (playerToTeleport == null) { + plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); + return; + } + final TeleportCommandTarget teleportCommandTarget; + if (args.length == 2) { + teleportCommandTarget = new TeleportCommandTarget(args[1]); + } else { + try { + teleportCommandTarget = new TeleportCommandTarget(new Position( + Double.parseDouble(args[1]), + Double.parseDouble(args[2]), + Double.parseDouble(args[3]), + 0f, 0f, + args.length >= 5 ? new World(args[4], UUID.randomUUID()) : plugin.getWorlds().get(0), + args.length == 6 ? new Server(args[5]) : plugin.getServer(playerToTeleport))); + } catch (NumberFormatException e) { + plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax: /tp [world] [server]"); + return; + } + } + + // Execute the console teleport + if (teleportCommandTarget.targetType == TeleportCommandTarget.TargetType.PLAYER) { + assert teleportCommandTarget.targetPlayer != null; + plugin.getLoggingAdapter().log(Level.INFO, "Teleporting " + playerToTeleport.username + + " to " + teleportCommandTarget.targetPlayer); + plugin.getTeleportManager().teleportToPlayerByName(playerToTeleport, teleportCommandTarget.targetPlayer); + } else { + assert teleportCommandTarget.targetPosition != null; + plugin.getTeleportManager().timedTeleport(playerToTeleport, teleportCommandTarget.targetPosition) + .thenAccept(teleportResult -> { + if (teleportResult.successful) { + plugin.getLoggingAdapter().log(Level.INFO, "Successfully teleported " + playerToTeleport.username); + } else { + plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + playerToTeleport.username + + " (" + teleportResult.name() + ")"); + } + plugin.getTeleportManager().finishTeleport(playerToTeleport, teleportResult); + }); + } } @Override 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 6ad08f5f..ad14916f 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Position.java +++ b/common/src/main/java/net/william278/huskhomes/position/Position.java @@ -37,8 +37,6 @@ 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"); diff --git a/common/src/main/java/net/william278/huskhomes/position/Server.java b/common/src/main/java/net/william278/huskhomes/position/Server.java index 7c4e14d6..fe2d141b 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Server.java +++ b/common/src/main/java/net/william278/huskhomes/position/Server.java @@ -1,6 +1,5 @@ package net.william278.huskhomes.position; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /**