From a768171e328fa078e38543824b1a17dfe13bf782 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 29 Jan 2022 03:29:12 +0000 Subject: [PATCH] Fix various things and it now works --- pom.xml | 4 +- .../me/william278/huskhomes2/HuskHomes.java | 2 +- .../huskhomes2/commands/TpCommand.java | 91 +++++++++++-------- .../teleport/points/TeleportationPoint.java | 2 +- 4 files changed, 57 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index cd88c948..4ad0b762 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,9 @@ false -Xdoclint:none - ${project.build.directory}/javadoc-bundle-options/maven-javadoc-plugin-stale-data.txt + + ${project.build.directory}/javadoc-bundle-options/maven-javadoc-plugin-stale-data.txt + diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 85b7e541..2c3ec586 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -212,7 +212,7 @@ public final class HuskHomes extends JavaPlugin { // Fetch spawn location if set SettingHandler.fetchSpawnLocation(); - // Setup the map integration with the correct plugin + // Set up the map integration with the correct plugin if (HuskHomes.getSettings().doMapIntegration()) { String mapPlugin = HuskHomes.getSettings().getMapPlugin(); switch (mapPlugin) { diff --git a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java index 353bc4ec..e427b934 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java @@ -44,32 +44,37 @@ public class TpCommand extends CommandBase { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + TpCommandInputHandler.HandlerResponse response = null; for (TpCommandInputHandler inputHandler : inputHandlers) { - switch (inputHandler.handleInput(args, sender)) { + response = inputHandler.handleInput(args, sender); + switch (response) { case CAN_HANDLE -> { inputHandler.executeTeleport(sender); return true; } - case CANNOT_HANDLE_BY_CONSOLE -> { - sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_syntax", - getClosestUsage(true, args))).toComponent()); - return true; - } - case CANNOT_HANDLE_INVALID_WORLD -> { - sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_world")).toComponent()); - return true; - } - case CANNOT_HANDLE_INVALID_PLAYER -> { - sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_player")).toComponent()); - return true; - } case CANNOT_HANDLE_HANDLED_BY_TP_HERE -> { return true; } } } + assert response != null; + switch (response) { + case CANNOT_HANDLE_BY_CONSOLE -> { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_syntax", + "/tp " + getClosestUsage(true, sender, args))).toComponent()); + return true; + } + case CANNOT_HANDLE_INVALID_WORLD -> { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_tp_invalid_world")).toComponent()); + return true; + } + case CANNOT_HANDLE_INVALID_PLAYER -> { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_player")).toComponent()); + return true; + } + } sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_syntax", - getClosestUsage((!(sender instanceof Player)), args))).toComponent()); + "/tp " + getClosestUsage((!(sender instanceof Player)), sender, args))).toComponent()); return true; } @@ -81,7 +86,7 @@ public class TpCommand extends CommandBase { public static class Tab implements TabCompleter { @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) { - String[] closestArgs = getClosestArgs(false, args); + String[] closestArgs = getClosestArgs(false, sender, args); final int currentArgIndex = Math.max(args.length - 1, 0); if (currentArgIndex >= closestArgs.length) { return Collections.emptyList(); @@ -123,25 +128,28 @@ public class TpCommand extends CommandBase { } } - public static String getClosestUsage(boolean filterOnlyConsoleExecutable, String... inputArgs) { + public static String getClosestUsage(boolean filterOnlyConsoleExecutable, CommandSender sender, String... inputArgs) { StringJoiner joiner = new StringJoiner(" "); - for (String argument : getClosestArgs(filterOnlyConsoleExecutable, inputArgs)) { + for (String argument : getClosestArgs(filterOnlyConsoleExecutable, sender, inputArgs)) { joiner.add(argument); } return joiner.toString(); } - public static String[] getClosestArgs(boolean filterOnlyConsoleExecutable, String... inputArgs) { + public static String[] getClosestArgs(boolean filterOnlyConsoleExecutable, CommandSender sender, String... inputArgs) { String[] closestArgs = new String[]{""}; - int lengthDifference = Integer.MAX_VALUE; + int difference = Integer.MAX_VALUE; for (TpCommandInputHandler handler : inputHandlers) { if (filterOnlyConsoleExecutable) { if (!handler.consoleExecutable) { continue; } } - int argLengthDifference = inputArgs.length - handler.arguments.length; - if (argLengthDifference < lengthDifference) { + int argLengthDifference = handler.arguments.length - inputArgs.length; + if (argLengthDifference < 0) { + continue; + } + if (argLengthDifference < difference) { closestArgs = handler.arguments; } } @@ -175,26 +183,31 @@ public class TpCommand extends CommandBase { return HandlerResponse.CANNOT_HANDLE_BY_CONSOLE; } if (inputArguments.length == arguments.length) { - int argumentIndex = 0; - for (String inputArgument : inputArguments) { - final HandlerResponse argumentResponse = handleArg(inputArgument, argumentIndex, inputExecutor); - if (argumentResponse != HandlerResponse.CAN_HANDLE) { - return argumentResponse; - } - argumentIndex++; - } - if (player == null) { - if (inputExecutor instanceof Player commandDispatcher) { - player = commandDispatcher; - } else { - return HandlerResponse.CANNOT_HANDLE_BY_CONSOLE; - } - } - return HandlerResponse.CAN_HANDLE; + return getHandleable(inputArguments, inputExecutor); } return HandlerResponse.CANNOT_HANDLE_SYNTAX_LENGTH_MISMATCH; } + private HandlerResponse getHandleable(String[] inputArguments, CommandSender inputExecutor) { + int argumentIndex = 0; + for (String inputArgument : inputArguments) { + if (argumentIndex >= arguments.length) break; + final HandlerResponse argumentResponse = handleArg(inputArgument, argumentIndex, inputExecutor); + if (argumentResponse != HandlerResponse.CAN_HANDLE) { + return argumentResponse; + } + argumentIndex++; + } + if (player == null) { + if (inputExecutor instanceof Player commandDispatcher) { + player = commandDispatcher; + } else { + return HandlerResponse.CANNOT_HANDLE_BY_CONSOLE; + } + } + return HandlerResponse.CAN_HANDLE; + } + public void executeTeleport(CommandSender sender) { // Handle teleportation to a target player if (target != null) { @@ -219,7 +232,7 @@ public class TpCommand extends CommandBase { (z != null ? z : player.getLocation().getZ()), (yaw != null ? yaw : player.getLocation().getYaw()), (pitch != null ? pitch : player.getLocation().getPitch()), - serverId != null || !HuskHomes.getSettings().doBungee() ? serverId : HuskHomes.getSettings().getServerID()); + serverId != null || HuskHomes.getSettings().doBungee() ? serverId : HuskHomes.getSettings().getServerID()); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { TeleportManager.teleportPlayer(player, teleportationPoint); if (sender instanceof Player sendingPlayer) { diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java index 6849631d..360cb61a 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java @@ -156,6 +156,6 @@ public class TeleportationPoint { */ @Override public String toString() { - return x + ", " + y + ", " + z + " (" + worldName + (HuskHomes.getSettings().getServerID().equalsIgnoreCase(server) ? "/" + server : "") + ")"; + return x + ", " + y + ", " + z + " (" + worldName + (HuskHomes.getSettings().doBungee() ? "/" + server : "") + ")"; } }