From 3d087b1bbe40a358951675c6968f2a6b73f02867 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Tue, 7 Jul 2020 14:30:31 +0200 Subject: [PATCH] Fix tab completion and usage --- .../plotsquared/core/command/HomeCommand.java | 29 ++++++++- .../com/plotsquared/core/command/Visit.java | 31 ++++++---- .../plotsquared/core/util/TabCompletions.java | 60 +++++++++++++++++++ 3 files changed, 107 insertions(+), 13 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java index 779d97ef2..07c15147a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java @@ -34,19 +34,22 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotId; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MathMan; +import com.plotsquared.core.util.TabCompletions; import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.query.SortingStrategy; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.CompletableFuture; @CommandDeclaration(command = "home", description = "Teleport to your plot(s)", permission = "plots.home", - usage = "/plot home [||| ]", + usage = "/plot home [||| | ]", aliases = {"h"}, requiredType = RequiredType.PLAYER, category = CommandCategory.TELEPORT) @@ -166,4 +169,28 @@ public class HomeCommand extends Command { return CompletableFuture.completedFuture(true); } + @Override + public Collection tab(PlotPlayer player, String[] args, boolean space) { + final List completions = new ArrayList<>(); + switch (args.length - 1) { + case 0: + completions.addAll( + TabCompletions.completeAreas(args[0])); + if (args[0].isEmpty()) { + // if no input is given, only suggest 1 - 3 + completions.addAll( + TabCompletions.asCompletions("1", "2", "3")); + break; + } + // complete more numbers from the already given input + completions.addAll( + TabCompletions.completeNumbers(args[0], 10, 999)); + break; + case 1: + completions.addAll( + TabCompletions.completeNumbers(args[1], 10, 999)); + break; + } + return completions; + } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Visit.java b/Core/src/main/java/com/plotsquared/core/command/Visit.java index e2faaf19e..b984d7c58 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -40,12 +40,11 @@ import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.query.SortingStrategy; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; -import com.plotsquared.core.uuid.UUIDMapping; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -54,7 +53,7 @@ import java.util.concurrent.TimeoutException; @CommandDeclaration(command = "visit", permission = "plots.visit", description = "Visit someones plot", - usage = "/plot visit <||> [#]", + usage = "/plot visit || [area]|[#] [#]", aliases = {"v", "tp", "teleport", "goto", "warp"}, requiredType = RequiredType.PLAYER, category = CommandCategory.TELEPORT) @@ -153,7 +152,7 @@ public class Visit extends Command { int page = Integer.MIN_VALUE; switch (args.length) { - // /p v [...] [...] + // /p v case 3: if (!MathMan.isInteger(args[2])) { Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)"); @@ -228,24 +227,32 @@ public class Visit extends Command { } @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { - final List completions = new LinkedList<>(); + final List completions = new ArrayList<>(); switch (args.length - 1) { case 0: - this.completeNumbers(completions, args[0], 0); completions.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); - break; + break; case 1: - if (MathMan.isInteger(args[0])) { + completions.addAll( + TabCompletions.completeAreas(args[1])); + if (args[1].isEmpty()) { + // if no input is given, only suggest 1 - 3 + completions.addAll( + TabCompletions.asCompletions("1", "2", "3")); break; } - this.completeNumbers(completions, args[1], 0); - this.completeAreas(completions, args[1]); + completions.addAll( + TabCompletions.completeNumbers(args[1], 10, 999)); break; case 2: - if (MathMan.isInteger(args[1])) { + if (args[2].isEmpty()) { + // if no input is given, only suggest 1 - 3 + completions.addAll( + TabCompletions.asCompletions("1", "2", "3")); break; } - this.completeNumbers(completions, args[2], 0); + completions.addAll( + TabCompletions.completeNumbers(args[2], 10, 999)); break; } diff --git a/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java index 56a3ff8fa..077ac4563 100644 --- a/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java +++ b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java @@ -34,6 +34,7 @@ import com.plotsquared.core.command.RequiredType; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.uuid.UUIDMapping; import lombok.experimental.UtilityClass; import org.jetbrains.annotations.NotNull; @@ -136,6 +137,65 @@ public class TabCompletions { return Collections.emptyList(); } + /** + * Get a list of integer numbers matching the given input. If the input string + * is empty, nothing will be returned. The list is unmodifiable. + * + * @param input Input to filter with + * @param amountLimit Maximum amount of suggestions + * @param highestLimit Highest number to include + * @return Unmodifiable list of number completions + */ + @NotNull public List completeNumbers(@NotNull final String input, + final int amountLimit, final int highestLimit) { + if (input.isEmpty() || input.length() > highestLimit || !MathMan.isInteger(input)) { + return Collections.emptyList(); + } + int offset; + try { + offset = Integer.parseInt(input) * 10; + } catch (NumberFormatException ignored) { + return Collections.emptyList(); + } + final List commands = new ArrayList<>(); + for (int i = offset; i < highestLimit && (offset - i + amountLimit) > 0; i++) { + commands.add(String.valueOf(i)); + } + return asCompletions(commands.toArray(new String[0])); + } + + /** + * Get a list of plot areas matching the given input. + * The list is unmodifiable. + * + * @param input Input to filter with + * @return Unmodifiable list of area completions + */ + @NotNull public List completeAreas(@NotNull final String input) { + final List completions = new ArrayList<>(); + for (final PlotArea area : PlotSquared.get().getPlotAreas()) { + String areaName = area.getWorldName(); + if (area.getId() != null) { + areaName += ";" + area.getId(); + } + if (!areaName.toLowerCase().startsWith(input.toLowerCase())) { + continue; + } + completions.add(new Command(null, false, areaName, "", + RequiredType.NONE, null) {}); + } + return Collections.unmodifiableList(completions); + } + + @NotNull public List asCompletions(String... toFilter) { + final List completions = new ArrayList<>(); + for (String completion : toFilter) { + completions.add(new Command(null, false, completion, "", + RequiredType.NONE, null) {}); + } + return Collections.unmodifiableList(completions); + } + /** * @param cacheIdentifier Cache key * @param input Command input