Fix tab completion and usage

This commit is contained in:
Hannes Greule 2020-07-07 14:30:31 +02:00 committed by Alexander Söderberg
parent c058614fcc
commit 3d087b1bbe
3 changed files with 107 additions and 13 deletions

View File

@ -34,19 +34,22 @@ import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotId; import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.MathMan; 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.PlotQuery;
import com.plotsquared.core.util.query.SortingStrategy; import com.plotsquared.core.util.query.SortingStrategy;
import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@CommandDeclaration(command = "home", @CommandDeclaration(command = "home",
description = "Teleport to your plot(s)", description = "Teleport to your plot(s)",
permission = "plots.home", permission = "plots.home",
usage = "/plot home [<page>|<alias>|<area;x;y>|<area> <x;y>]", usage = "/plot home [<page>|<alias>|<area;x;y>|<area> <x;y>|<area> <page>]",
aliases = {"h"}, aliases = {"h"},
requiredType = RequiredType.PLAYER, requiredType = RequiredType.PLAYER,
category = CommandCategory.TELEPORT) category = CommandCategory.TELEPORT)
@ -166,4 +169,28 @@ public class HomeCommand extends Command {
return CompletableFuture.completedFuture(true); return CompletableFuture.completedFuture(true);
} }
@Override
public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) {
final List<Command> 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;
}
} }

View File

@ -40,12 +40,11 @@ import com.plotsquared.core.util.query.PlotQuery;
import com.plotsquared.core.util.query.SortingStrategy; import com.plotsquared.core.util.query.SortingStrategy;
import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import com.plotsquared.core.uuid.UUIDMapping;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -54,7 +53,7 @@ import java.util.concurrent.TimeoutException;
@CommandDeclaration(command = "visit", @CommandDeclaration(command = "visit",
permission = "plots.visit", permission = "plots.visit",
description = "Visit someones plot", description = "Visit someones plot",
usage = "/plot visit <<player>|<alias>|<world>> [#]", usage = "/plot visit <player>|<alias>|<plot> [area]|[#] [#]",
aliases = {"v", "tp", "teleport", "goto", "warp"}, aliases = {"v", "tp", "teleport", "goto", "warp"},
requiredType = RequiredType.PLAYER, requiredType = RequiredType.PLAYER,
category = CommandCategory.TELEPORT) category = CommandCategory.TELEPORT)
@ -153,7 +152,7 @@ public class Visit extends Command {
int page = Integer.MIN_VALUE; int page = Integer.MIN_VALUE;
switch (args.length) { switch (args.length) {
// /p v [...] [...] <page> // /p v <user> <area> <page>
case 3: case 3:
if (!MathMan.isInteger(args[2])) { if (!MathMan.isInteger(args[2])) {
Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)"); Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)");
@ -228,24 +227,32 @@ public class Visit extends Command {
} }
@Override public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) { @Override public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) {
final List<Command> completions = new LinkedList<>(); final List<Command> completions = new ArrayList<>();
switch (args.length - 1) { switch (args.length - 1) {
case 0: case 0:
this.completeNumbers(completions, args[0], 0);
completions.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); completions.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList()));
break; break;
case 1: 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; break;
} }
this.completeNumbers(completions, args[1], 0); completions.addAll(
this.completeAreas(completions, args[1]); TabCompletions.completeNumbers(args[1], 10, 999));
break; break;
case 2: 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; break;
} }
this.completeNumbers(completions, args[2], 0); completions.addAll(
TabCompletions.completeNumbers(args[2], 10, 999));
break; break;
} }

View File

@ -34,6 +34,7 @@ import com.plotsquared.core.command.RequiredType;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.uuid.UUIDMapping; import com.plotsquared.core.uuid.UUIDMapping;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -136,6 +137,65 @@ public class TabCompletions {
return Collections.emptyList(); 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<Command> 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<String> 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<Command> completeAreas(@NotNull final String input) {
final List<Command> 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<Command> asCompletions(String... toFilter) {
final List<Command> 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 cacheIdentifier Cache key
* @param input Command input * @param input Command input