From f72c01d10bc04a4e0250e52264ce313d82e2dfd2 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 24 Aug 2022 23:24:26 +0100 Subject: [PATCH] Console /rtp command --- .../huskhomes/command/RtpCommand.java | 29 ++++++++++++++++++- .../huskhomes/command/TpCommand.java | 4 +-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java index 653d39ad..3279e13c 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -12,6 +12,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; public class RtpCommand extends CommandBase implements ConsoleExecutable { @@ -93,6 +94,32 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { @Override public void onConsoleExecute(@NotNull String[] args) { - //todo + if (args.length == 0) { + plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: rtp [player]"); + return; + } + final Optional foundUser = plugin.findPlayer(args[0]); + if (foundUser.isEmpty()) { + plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); + return; + } + + plugin.getLoggingAdapter().log(Level.INFO, "Finding a random position for " + foundUser.get().username + "..."); + plugin.getRtpEngine().getRandomPosition(foundUser.get().getPosition()).thenAccept(position -> { + if (position.isEmpty()) { + plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + foundUser.get().username + " to a random position; randomization timed out!"); + return; + } + plugin.getTeleportManager().teleport(foundUser.get(), position.get()) + .thenAccept(result -> { + if (result.successful) { + plugin.getLoggingAdapter().log(Level.INFO, "Teleported" + foundUser.get().username + " to a random position."); + } else { + plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport" + foundUser.get().username + " to a random position."); + } + plugin.getTeleportManager().finishTeleport(foundUser.get(), result); + }).join(); + }); + } } 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 be41e76d..167ea9cf 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -129,7 +129,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe @Override public void onConsoleExecute(@NotNull String[] args) { if (args.length < 2 || args.length > 6) { - plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax: /tp "); + plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: tp "); return; } final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); @@ -150,7 +150,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe 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]"); + plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: tp [world] [server]"); return; } }