diff --git a/src/main/java/me/william278/huskhomes2/commands/PublicHomeCommand.java b/src/main/java/me/william278/huskhomes2/commands/PublicHomeCommand.java index 1b1772dc..2dccc60d 100644 --- a/src/main/java/me/william278/huskhomes2/commands/PublicHomeCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/PublicHomeCommand.java @@ -131,7 +131,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompleter { @Override protected void onCommand(Player player, Command command, String label, String[] args) { - // Console is fine too + // Accept console input } @Override diff --git a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java index ed91ddc6..d03ebbe6 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java @@ -1,11 +1,13 @@ package me.william278.huskhomes2.commands; +import de.themoep.minedown.MineDown; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.util.MessageManager; import me.william278.huskhomes2.teleport.TeleportManager; import me.william278.huskhomes2.teleport.points.TeleportationPoint; import me.william278.huskhomes2.util.NameAutoCompleter; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -13,118 +15,349 @@ import org.bukkit.entity.Player; import org.bukkit.util.StringUtil; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; public class TpCommand extends CommandBase { private static final HuskHomes plugin = HuskHomes.getInstance(); + private static final HashSet inputHandlers = new HashSet<>(); + + public TpCommand() { + inputHandlers.add(new TpCommandInputHandler(false, "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "")); + inputHandlers.add(new TpCommandInputHandler(false, "", "", "")); + inputHandlers.add(new TpCommandInputHandler(false, "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(false, "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(false, "", "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "", "", "", "")); + + if (HuskHomes.getSettings().doBungee()) { + inputHandlers.add(new TpCommandInputHandler(false, "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "", "", "")); + inputHandlers.add(new TpCommandInputHandler(true, "", "", "", "", "", "", "", "")); + } + } + @Override - protected void onCommand(Player p, Command command, String label, String[] args) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - switch (args.length) { - default: - case 0: - MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); - return; - case 1: - String targetPlayer = args[0]; - TeleportManager.teleportPlayer(p, NameAutoCompleter.getAutoCompletedName(targetPlayer)); - return; - case 3: - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - TeleportationPoint teleportationPoint = new TeleportationPoint(p.getWorld().getName(), x, y, z, 0F, 0F, HuskHomes.getSettings().getServerID()); - TeleportManager.teleportPlayer(p, teleportationPoint); - } catch (NumberFormatException e) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); - } - return; - case 4: - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - String worldName = args[3]; - if (Bukkit.getWorld(worldName) == null) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); - return; - } - TeleportationPoint teleportationPoint = new TeleportationPoint(worldName, x, y, z, 0F, 0F, HuskHomes.getSettings().getServerID()); - TeleportManager.teleportPlayer(p, teleportationPoint); - } catch (NumberFormatException e) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); - } - return; - case 5: - if (HuskHomes.getSettings().doBungee()) { - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - String worldName = args[3]; - String serverName = args[4]; - TeleportationPoint teleportationPoint = new TeleportationPoint(worldName, x, y, z, 0F, 0F, serverName); - TeleportManager.teleportPlayer(p, teleportationPoint); - } catch (NumberFormatException e) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp "); - } - } else { - MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); - } - return; - case 6: - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - String worldName = args[3]; - float yaw = Float.parseFloat(args[4]); - float pitch = Float.parseFloat(args[5]); - TeleportationPoint teleportationPoint = new TeleportationPoint(worldName, x, y, z, yaw, pitch, HuskHomes.getSettings().getServerID()); - TeleportManager.teleportPlayer(p, teleportationPoint); - } catch (NumberFormatException e) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp "); - } - case 7: - if (HuskHomes.getSettings().doBungee()) { - try { - double x = Double.parseDouble(args[0]); - double y = Double.parseDouble(args[1]); - double z = Double.parseDouble(args[2]); - String worldName = args[3]; - String serverName = args[4]; - float yaw = Float.parseFloat(args[5]); - float pitch = Float.parseFloat(args[6]); - TeleportationPoint teleportationPoint = new TeleportationPoint(worldName, x, y, z, yaw, pitch, serverName); - TeleportManager.teleportPlayer(p, teleportationPoint); - } catch (NumberFormatException e) { - MessageManager.sendMessage(p, "error_invalid_syntax", "/tp "); - } - } else { - MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); - } + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + for (TpCommandInputHandler inputHandler : inputHandlers) { + switch (inputHandler.handleInput(args, sender)) { + 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; + } } - }); + } + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("error_invalid_syntax", + getClosestUsage((!(sender instanceof Player)), args))).toComponent()); + return true; + } + + @Override + protected void onCommand(Player player, Command command, String label, String[] args) { + // Accept console input } public static class Tab implements TabCompleter { @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) { - List players = new ArrayList<>(); - if (args.length == 0 || args.length == 1) { - players.addAll(HuskHomes.getPlayerList().getPlayers()); + String[] closestArgs = getClosestArgs(false, args); + final int currentArgIndex = Math.max(args.length - 1, 0); + if (currentArgIndex >= closestArgs.length) { + return Collections.emptyList(); } - players.remove(sender.getName()); - final List tabCompletions = new ArrayList<>(); - StringUtil.copyPartialMatches(args[0], players, tabCompletions); - Collections.sort(tabCompletions); - return tabCompletions; + + final ArrayList possibleTabCompletions = new ArrayList<>(); + final String argToReturn = closestArgs[currentArgIndex]; + + switch (argToReturn) { + case "", "", "" -> possibleTabCompletions.add("~"); + case "", "" -> { + for (int i = -180; i <= 180; i += 45) { + possibleTabCompletions.add(Integer.toString(i)); + } + } + case "" -> { + final ArrayList onlinePlayers = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) { + onlinePlayers.add(player.getName()); + } + StringUtil.copyPartialMatches(args[currentArgIndex], onlinePlayers, possibleTabCompletions); + } + case "" -> { + final ArrayList globalPlayers = new ArrayList<>(HuskHomes.getPlayerList().getPlayers()); + StringUtil.copyPartialMatches(args[currentArgIndex], globalPlayers, possibleTabCompletions); + } + case "" -> { + final ArrayList worlds = new ArrayList<>(); + for (World world : Bukkit.getWorlds()) { + worlds.add(world.getName()); + } + StringUtil.copyPartialMatches(args[currentArgIndex], worlds, possibleTabCompletions); + } + default -> possibleTabCompletions.add(argToReturn); + } + + Collections.sort(possibleTabCompletions); + return possibleTabCompletions; + } + } + + public static String getClosestUsage(boolean filterOnlyConsoleExecutable, String... inputArgs) { + StringJoiner joiner = new StringJoiner(" "); + for (String argument : getClosestArgs(filterOnlyConsoleExecutable, inputArgs)) { + joiner.add(argument); + } + return joiner.toString(); + } + + public static String[] getClosestArgs(boolean filterOnlyConsoleExecutable, String... inputArgs) { + String[] closestArgs = new String[]{""}; + int lengthDifference = Integer.MAX_VALUE; + for (TpCommandInputHandler handler : inputHandlers) { + if (filterOnlyConsoleExecutable) { + if (!handler.consoleExecutable) { + continue; + } + } + int argLengthDifference = inputArgs.length - handler.arguments.length; + if (argLengthDifference < lengthDifference) { + closestArgs = handler.arguments; + } + } + return closestArgs; + } + + public static class TpCommandInputHandler { + + private String target; + private Player player; + private Double x; + private Double y; + private Double z; + private Float yaw; + private Float pitch; + private String worldName; + private String serverId; + + private String tpHereTarget; + + public final String[] arguments; + public final boolean consoleExecutable; + + public TpCommandInputHandler(boolean consoleExecutable, String... arguments) { + this.consoleExecutable = consoleExecutable; + this.arguments = arguments; } + public HandlerResponse handleInput(String[] inputArguments, CommandSender inputExecutor) { + if (!(inputExecutor instanceof Player) && !consoleExecutable) { + 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 HandlerResponse.CANNOT_HANDLE_SYNTAX_LENGTH_MISMATCH; + } + + public void executeTeleport(CommandSender sender) { + // Handle teleportation to a target player + if (target != null) { + final String targetName = NameAutoCompleter.getAutoCompletedName(target); + TeleportManager.teleportPlayer(player, targetName); + if (sender instanceof Player sendingPlayer) { + if (!sendingPlayer.getUniqueId().equals(player.getUniqueId())) { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete")).toComponent()); + } + } else { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete")).toComponent()); + player.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete_console", targetName)).toComponent()); + } + return; + } + + // Handle teleportation + final TeleportationPoint teleportationPoint = new TeleportationPoint( + (worldName != null ? worldName : player.getWorld().getName()), + (x != null ? x : player.getLocation().getX()), + (y != null ? y : player.getLocation().getY()), + (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()); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + TeleportManager.teleportPlayer(player, teleportationPoint); + if (sender instanceof Player sendingPlayer) { + if (!sendingPlayer.getUniqueId().equals(player.getUniqueId())) { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete")).toComponent()); + } + } else { + sender.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete")).toComponent()); + player.spigot().sendMessage(new MineDown(MessageManager.getRawMessage("teleporting_complete_console", teleportationPoint.toString())).toComponent()); + } + }); + } + + private boolean isCrossServerInput() { + boolean isCrossServer = false; + for (String argument : arguments) { + if (argument.equalsIgnoreCase("")) { + isCrossServer = true; + break; + } + } + return isCrossServer; + } + + public HandlerResponse handleArg(String inputArgument, int argumentIndex, CommandSender inputExecutor) { + final String expectedArgument = arguments[argumentIndex]; + switch (expectedArgument) { + case "", "", "" -> { + try { + double value; + if (inputArgument.startsWith("~")) { + if (inputExecutor instanceof Player player) { + value = switch (expectedArgument) { + case "" -> player.getLocation().getX(); + case "" -> player.getLocation().getY(); + case "" -> player.getLocation().getZ(); + }; + } else { + return HandlerResponse.CANNOT_HANDLE_BY_CONSOLE; + } + if (inputArgument.length() > 1) { + inputArgument = inputArgument.substring(1); + value += Double.parseDouble(inputArgument); + } + } else { + value = Double.parseDouble(inputArgument); + } + switch (expectedArgument) { + case "" -> x = value; + case "" -> y = value; + case "" -> z = value; + } + return HandlerResponse.CAN_HANDLE; + } catch (NumberFormatException e) { + return HandlerResponse.CANNOT_HANDLE_SYNTAX_MISMATCH; + } + } + case "", "" -> { + try { + float value; + if (inputArgument.startsWith("~")) { + if (inputExecutor instanceof Player player) { + value = switch (expectedArgument) { + case "" -> player.getLocation().getYaw(); + case "" -> player.getLocation().getPitch(); + }; + } else { + return HandlerResponse.CANNOT_HANDLE_BY_CONSOLE; + } + if (inputArgument.length() > 1) { + inputArgument = inputArgument.substring(1); + value += Double.parseDouble(inputArgument); + } + } else { + value = Float.parseFloat(inputArgument); + } + switch (expectedArgument) { + case "" -> yaw = value; + case "" -> pitch = value; + } + return HandlerResponse.CAN_HANDLE; + } catch (NumberFormatException e) { + return HandlerResponse.CANNOT_HANDLE_SYNTAX_MISMATCH; + } + } + case "" -> { + if (isCrossServerInput()) { + worldName = inputArgument; + return HandlerResponse.CAN_HANDLE; + } else { + if (Bukkit.getWorld(inputArgument) != null) { + worldName = inputArgument; + return HandlerResponse.CAN_HANDLE; + } + return HandlerResponse.CANNOT_HANDLE_INVALID_WORLD; + } + } + case "" -> { + serverId = inputArgument; + return HandlerResponse.CAN_HANDLE; + } + case "" -> { + target = inputArgument; + if (inputExecutor instanceof Player playerExecutor) { + if (NameAutoCompleter.getAutoCompletedName(target).equalsIgnoreCase(playerExecutor.getName()) && tpHereTarget != null) { + TpHereCommand.executeTpHere(playerExecutor, tpHereTarget); + return HandlerResponse.CANNOT_HANDLE_HANDLED_BY_TP_HERE; + } + } + return HandlerResponse.CAN_HANDLE; + } + case "" -> { + Player teleportingPlayer = Bukkit.getPlayerExact(inputArgument); + if (teleportingPlayer != null) { + player = teleportingPlayer; + return HandlerResponse.CAN_HANDLE; + } + if (arguments.length == 2) { + if (arguments[1].equalsIgnoreCase("")) { + tpHereTarget = inputArgument; + return HandlerResponse.CAN_HANDLE; + } + } + return HandlerResponse.CANNOT_HANDLE_INVALID_PLAYER; + } + } + return HandlerResponse.CANNOT_HANDLE_INVALID_EXPECTED_ARGUMENT; + } + + public enum HandlerResponse { + CAN_HANDLE, + CANNOT_HANDLE_HANDLED_BY_TP_HERE, + CANNOT_HANDLE_INVALID_WORLD, + CANNOT_HANDLE_INVALID_PLAYER, + CANNOT_HANDLE_BY_CONSOLE, + CANNOT_HANDLE_SYNTAX_MISMATCH, + CANNOT_HANDLE_SYNTAX_LENGTH_MISMATCH, + CANNOT_HANDLE_INVALID_EXPECTED_ARGUMENT + } } -} +} \ No newline at end of file diff --git a/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java index 632ccc9b..9c279722 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpHereCommand.java @@ -19,15 +19,20 @@ public class TpHereCommand extends CommandBase { protected void onCommand(Player p, Command command, String label, String[] args) { if (args.length == 1) { String targetPlayer = args[0]; - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - try { - TeleportManager.teleportHere(p, NameAutoCompleter.getAutoCompletedName(targetPlayer)); - } catch (SQLException e) { - plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred using /tphere"); - } - }); + executeTpHere(p, targetPlayer); } else { MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); } } + + // Execute the tp here operation + public static void executeTpHere(Player player, String targetPlayer) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try { + TeleportManager.teleportHere(player, NameAutoCompleter.getAutoCompletedName(targetPlayer)); + } catch (SQLException e) { + plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred using /tphere"); + } + }); + } } diff --git a/src/main/java/me/william278/huskhomes2/commands/WarpCommand.java b/src/main/java/me/william278/huskhomes2/commands/WarpCommand.java index 680d6101..b57e827e 100644 --- a/src/main/java/me/william278/huskhomes2/commands/WarpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/WarpCommand.java @@ -62,7 +62,7 @@ public class WarpCommand extends CommandBase { @Override protected void onCommand(Player player, Command command, String label, String[] args) { - // Console is fine too + // Accept console input } // Handle a warp from console 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 c6cef276..6849631d 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java @@ -1,9 +1,12 @@ package me.william278.huskhomes2.teleport.points; +import me.william278.huskhomes2.HuskHomes; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import java.util.StringJoiner; + /** * @author William * HuskHomes' position representation object; represents an in-game location on a server @@ -20,8 +23,9 @@ public class TeleportationPoint { /** * Update the location of a TeleportationPoint + * * @param location a new Bukkit location - * @param server a new Bungee server ID + * @param server a new Bungee server ID */ public void setLocation(Location location, String server) { worldName = location.getWorld().getName(); @@ -35,8 +39,9 @@ public class TeleportationPoint { /** * HuskHomes' position representation object; represents an in-game location on a server + * * @param location The Bukkit location - * @param server The Bungee server ID which the location is on + * @param server The Bungee server ID which the location is on */ public TeleportationPoint(Location location, String server) { if (location.getWorld() == null) { @@ -47,13 +52,14 @@ public class TeleportationPoint { /** * HuskHomes' position representation object; represents an in-game location on a server + * * @param worldName The name of the location's world - * @param x The location's x-coordinate - * @param y The location's y-coordinate - * @param z The location's z-coordinate - * @param yaw The location's yaw angle - * @param pitch The location's pitch angle - * @param server The Bungee server ID which the location is on + * @param x The location's x-coordinate + * @param y The location's y-coordinate + * @param z The location's z-coordinate + * @param yaw The location's yaw angle + * @param pitch The location's pitch angle + * @param server The Bungee server ID which the location is on */ public TeleportationPoint(String worldName, double x, double y, double z, float yaw, float pitch, String server) { this.server = server; @@ -67,6 +73,7 @@ public class TeleportationPoint { /** * Get the Bukkit location from a TeleportationPoint + * * @return the Bukkit location on the server from a TeleportationPoint * @throws IllegalStateException if the location is not valid on the server */ @@ -81,6 +88,7 @@ public class TeleportationPoint { /** * Get the name of the world from a TeleportationPoint + * * @return the name of the world the position is on from a TeleportationPoint */ public String getWorldName() { @@ -89,6 +97,7 @@ public class TeleportationPoint { /** * Get the Bungee server ID from a TeleportationPoint + * * @return the TeleportationPoint's Bungee server ID */ public String getServer() { @@ -97,6 +106,7 @@ public class TeleportationPoint { /** * Get the x-coordinate of a TeleportationPoint + * * @return the x-coordinate */ public double getX() { @@ -105,6 +115,7 @@ public class TeleportationPoint { /** * Get the y-coordinate of a TeleportationPoint + * * @return the y-coordinate */ public double getY() { @@ -113,6 +124,7 @@ public class TeleportationPoint { /** * Get the z-coordinate of a TeleportationPoint + * * @return the z-coordinate */ public double getZ() { @@ -121,6 +133,7 @@ public class TeleportationPoint { /** * Get the yaw angle of a TeleportationPoint + * * @return the yaw angle */ public float getYaw() { @@ -129,9 +142,20 @@ public class TeleportationPoint { /** * Get the pitch angle of a TeleportationPoint + * * @return the pitch angle */ public float getPitch() { return pitch; } + + /** + * Returns the TeleportationPoint as a formatted string + * + * @return the TeleportationPoint + */ + @Override + public String toString() { + return x + ", " + y + ", " + z + " (" + worldName + (HuskHomes.getSettings().getServerID().equalsIgnoreCase(server) ? "/" + server : "") + ")"; + } }