Port more commands and add more tab completion utilities

This commit is contained in:
Alexander Söderberg 2020-05-19 19:34:33 +02:00
parent 123ca8efe9
commit 973c18623f
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
4 changed files with 118 additions and 94 deletions

View File

@ -29,6 +29,7 @@ import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.PlotSquared.SortType; import com.plotsquared.core.PlotSquared.SortType;
import com.plotsquared.core.configuration.CaptionUtility; import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
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.plot.PlotArea;
@ -44,20 +45,25 @@ import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.StringComparison; import com.plotsquared.core.util.StringComparison;
import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import com.plotsquared.core.util.uuid.UUIDHandler; import com.plotsquared.core.uuid.UUIDMapping;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
@CommandDeclaration(command = "list", @CommandDeclaration(command = "list",
aliases = {"l", "find", "search"}, aliases = {"l", "find", "search"},
description = "List plots", description = "List plots",
permission = "plots.list", permission = "plots.list",
category = CommandCategory.INFO, category = CommandCategory.INFO,
usage = "/plot list <forsale|mine|shared|world|top|all|unowned|unknown|player|world|done|fuzzy <search...>> [#]") usage = "/plot list <forsale|mine|shared|world|top|all|unowned|player|world|done|fuzzy <search...>> [#]")
public class ListCmd extends SubCommand { public class ListCmd extends SubCommand {
private String[] getArgumentList(PlotPlayer player) { private String[] getArgumentList(PlotPlayer player) {
@ -84,9 +90,6 @@ public class ListCmd extends SubCommand {
if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNOWNED)) { if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNOWNED)) {
args.add("unowned"); args.add("unowned");
} }
if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNKNOWN)) {
args.add("unknown");
}
if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_PLAYER)) { if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_PLAYER)) {
args.add("<player>"); args.add("<player>");
} }
@ -115,25 +118,44 @@ public class ListCmd extends SubCommand {
noArgs(player); noArgs(player);
return false; return false;
} }
int page = 0;
final int page;
if (args.length > 1) { if (args.length > 1) {
int tempPage = -1;
try { try {
page = Integer.parseInt(args[args.length - 1]); tempPage = Integer.parseInt(args[args.length - 1]);
--page; --tempPage;
if (page < 0) { if (tempPage < 0) {
page = 0; tempPage = 0;
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
page = -1;
} }
page = tempPage;
} else {
page = 0;
} }
List<Plot> plots = null;
String world = player.getLocation().getWorld(); String world = player.getLocation().getWorld();
PlotArea area = player.getApplicablePlotArea(); PlotArea area = player.getApplicablePlotArea();
String arg = args[0].toLowerCase(); String arg = args[0].toLowerCase();
boolean sort = true; final boolean[] sort = new boolean[] {true};
final Consumer<Collection<Plot>> plotConsumer = plots -> {
if (plots == null) {
sendMessage(player, Captions.DID_YOU_MEAN,
new StringComparison<>(args[0], new String[] {"mine", "shared", "world", "all"})
.getBestMatch());
return;
}
if (plots.isEmpty()) {
MainUtil.sendMessage(player, Captions.FOUND_NO_PLOTS);
return;
}
displayPlots(player, new ArrayList<>(plots), 12, page, area, args, sort[0]);
};
final List<Plot> plots = new ArrayList<>();
switch (arg) { switch (arg) {
case "mine": case "mine":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_MINE)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_MINE)) {
@ -141,8 +163,9 @@ public class ListCmd extends SubCommand {
.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_MINE); .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_MINE);
return false; return false;
} }
sort = false; sort[0] = false;
plots = PlotSquared.get().sortPlotsByTemp(PlotSquared.get().getBasePlots(player)); plotConsumer.accept(
PlotSquared.get().sortPlotsByTemp(PlotSquared.get().getBasePlots(player)));
break; break;
case "shared": case "shared":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_SHARED)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_SHARED)) {
@ -150,13 +173,13 @@ public class ListCmd extends SubCommand {
Captions.PERMISSION_LIST_SHARED); Captions.PERMISSION_LIST_SHARED);
return false; return false;
} }
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) { for (Plot plot : PlotSquared.get().getPlots()) {
if (plot.getTrusted().contains(player.getUUID()) || plot.getMembers() if (plot.getTrusted().contains(player.getUUID()) || plot.getMembers()
.contains(player.getUUID())) { .contains(player.getUUID())) {
plots.add(plot); plots.add(plot);
} }
} }
plotConsumer.accept(plots);
break; break;
case "world": case "world":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_WORLD)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_WORLD)) {
@ -171,7 +194,7 @@ public class ListCmd extends SubCommand {
world)); world));
return false; return false;
} }
plots = new ArrayList<>(PlotSquared.get().getPlots(world)); plotConsumer.accept(PlotSquared.get().getPlots(world));
break; break;
case "expired": case "expired":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_EXPIRED)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_EXPIRED)) {
@ -179,9 +202,9 @@ public class ListCmd extends SubCommand {
Captions.PERMISSION_LIST_EXPIRED); Captions.PERMISSION_LIST_EXPIRED);
return false; return false;
} }
plots = ExpireManager.IMP == null ? plotConsumer.accept(ExpireManager.IMP == null ?
new ArrayList<Plot>() : new ArrayList<>() :
new ArrayList<>(ExpireManager.IMP.getPendingExpired()); new ArrayList<>(ExpireManager.IMP.getPendingExpired()));
break; break;
case "area": case "area":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_AREA)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_AREA)) {
@ -196,7 +219,8 @@ public class ListCmd extends SubCommand {
world)); world));
return false; return false;
} }
plots = area == null ? new ArrayList<Plot>() : new ArrayList<>(area.getPlots()); plotConsumer.accept(
area == null ? new ArrayList<Plot>() : new ArrayList<>(area.getPlots()));
break; break;
case "all": case "all":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_ALL)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_ALL)) {
@ -204,7 +228,7 @@ public class ListCmd extends SubCommand {
.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_ALL); .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_ALL);
return false; return false;
} }
plots = new ArrayList<>(PlotSquared.get().getPlots()); plotConsumer.accept(new ArrayList<>(PlotSquared.get().getPlots()));
break; break;
case "done": case "done":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_DONE)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_DONE)) {
@ -212,7 +236,6 @@ public class ListCmd extends SubCommand {
.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_DONE); .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_DONE);
return false; return false;
} }
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) { for (Plot plot : PlotSquared.get().getPlots()) {
if (DoneFlag.isDone(plot)) { if (DoneFlag.isDone(plot)) {
plots.add(plot); plots.add(plot);
@ -229,7 +252,8 @@ public class ListCmd extends SubCommand {
} }
return 1; return 1;
}); });
sort = false; sort[0] = false;
plotConsumer.accept(plots);
break; break;
case "top": case "top":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_TOP)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_TOP)) {
@ -237,7 +261,7 @@ public class ListCmd extends SubCommand {
.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_TOP); .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_LIST_TOP);
return false; return false;
} }
plots = new ArrayList<>(PlotSquared.get().getPlots()); plots.addAll(PlotSquared.get().getPlots());
plots.sort((p1, p2) -> { plots.sort((p1, p2) -> {
double v1 = 0; double v1 = 0;
int p1s = p1.getSettings().getRatings().size(); int p1s = p1.getSettings().getRatings().size();
@ -262,7 +286,8 @@ public class ListCmd extends SubCommand {
} }
return (int) Math.signum(v2 - v1); return (int) Math.signum(v2 - v1);
}); });
sort = false; sort[0] = false;
plotConsumer.accept(plots);
break; break;
case "forsale": case "forsale":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) {
@ -273,12 +298,12 @@ public class ListCmd extends SubCommand {
if (EconHandler.manager == null) { if (EconHandler.manager == null) {
break; break;
} }
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) { for (Plot plot : PlotSquared.get().getPlots()) {
if (plot.getFlag(PriceFlag.class) > 0) { if (plot.getFlag(PriceFlag.class) > 0) {
plots.add(plot); plots.add(plot);
} }
} }
plotConsumer.accept(plots);
break; break;
case "unowned": case "unowned":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNOWNED)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNOWNED)) {
@ -286,28 +311,12 @@ public class ListCmd extends SubCommand {
Captions.PERMISSION_LIST_UNOWNED); Captions.PERMISSION_LIST_UNOWNED);
return false; return false;
} }
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) { for (Plot plot : PlotSquared.get().getPlots()) {
if (plot.getOwner() == null) { if (plot.getOwner() == null) {
plots.add(plot); plots.add(plot);
} }
} }
break; plotConsumer.accept(plots);
case "unknown":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_UNKNOWN)) {
MainUtil.sendMessage(player, Captions.NO_PERMISSION,
Captions.PERMISSION_LIST_UNKNOWN);
return false;
}
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) {
if (plot.getOwner() == null) {
continue;
}
if (UUIDHandler.getName(plot.getOwner()) == null) {
plots.add(plot);
}
}
break; break;
case "fuzzy": case "fuzzy":
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_FUZZY)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_FUZZY)) {
@ -325,8 +334,8 @@ public class ListCmd extends SubCommand {
} else { } else {
term = StringMan.join(Arrays.copyOfRange(args, 1, args.length), " "); term = StringMan.join(Arrays.copyOfRange(args, 1, args.length), " ");
} }
plots = MainUtil.getPlotsBySearch(term); sort[0] = false;
sort = false; plotConsumer.accept(MainUtil.getPlotsBySearch(term));
break; break;
default: default:
if (PlotSquared.get().hasPlotArea(args[0])) { if (PlotSquared.get().hasPlotArea(args[0])) {
@ -343,40 +352,39 @@ public class ListCmd extends SubCommand {
args[0])); args[0]));
return false; return false;
} }
plots = new ArrayList<>(PlotSquared.get().getPlots(args[0])); plotConsumer.accept(new ArrayList<>(PlotSquared.get().getPlots(args[0])));
break;
}
UUID uuid = UUIDHandler.getUUID(args[0], null);
if (uuid == null) {
try {
uuid = UUID.fromString(args[0]);
} catch (Exception ignored) {
}
}
if (uuid != null) {
if (!Permissions.hasPermission(player, Captions.PERMISSION_LIST_PLAYER)) {
MainUtil.sendMessage(player, Captions.NO_PERMISSION,
Captions.PERMISSION_LIST_PLAYER);
return false;
}
sort = false;
plots = PlotSquared.get().sortPlotsByTemp(PlotSquared.get().getPlots(uuid));
break; break;
} }
PlotSquared.get().getImpromptuUUIDPipeline()
.getSingle(args[0], (uuid, throwable) -> {
if (throwable instanceof TimeoutException) {
MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT);
} else if (throwable != null) {
if (uuid == null) {
try {
uuid = UUID.fromString(args[0]);
} catch (Exception ignored) {
}
}
}
if (uuid == null) {
MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]);
} else {
if (!Permissions
.hasPermission(player, Captions.PERMISSION_LIST_PLAYER)) {
MainUtil.sendMessage(player, Captions.NO_PERMISSION,
Captions.PERMISSION_LIST_PLAYER);
} else {
sort[0] = false;
plotConsumer.accept(PlotSquared.get()
.sortPlotsByTemp(PlotSquared.get().getPlots(uuid)));
}
}
});
break;
} }
if (plots == null) {
sendMessage(player, Captions.DID_YOU_MEAN,
new StringComparison<>(args[0], new String[] {"mine", "shared", "world", "all"})
.getBestMatch());
return false;
}
if (plots.isEmpty()) {
MainUtil.sendMessage(player, Captions.FOUND_NO_PLOTS);
return false;
}
displayPlots(player, plots, 12, page, area, args, sort);
return true; return true;
} }
@ -417,22 +425,25 @@ public class ListCmd extends SubCommand {
.command("/plot info " + plot.getArea() + ";" + plot.getId()).color(color) .command("/plot info " + plot.getArea() + ";" + plot.getId()).color(color)
.text(" - ").color("$2"); .text(" - ").color("$2");
String prefix = ""; String prefix = "";
for (UUID uuid : plot.getOwners()) {
String name = UUIDHandler.getName(uuid); try {
if (name == null) { final List<UUIDMapping> names = PlotSquared.get().getImpromptuUUIDPipeline()
message = message.text(prefix).color("$4").text("unknown").color("$2") .getNames(plot.getOwners()).get(Settings.UUID.BLOCKING_TIMEOUT, TimeUnit.MILLISECONDS);
.tooltip(uuid.toString()).suggest(uuid.toString()); for (final UUIDMapping uuidMapping : names) {
} else { PlotPlayer pp = PlotSquared.imp().getPlayerManager().getPlayerIfExists(uuidMapping.getUuid());
PlotPlayer pp = UUIDHandler.getPlayer(uuid);
if (pp != null) { if (pp != null) {
message = message.text(prefix).color("$4").text(name).color("$1") message = message.text(prefix).color("$4").text(uuidMapping.getUsername()).color("$1")
.tooltip(new PlotMessage("Online").color("$4")); .tooltip(new PlotMessage("Online").color("$4"));
} else { } else {
message = message.text(prefix).color("$4").text(name).color("$1") message = message.text(prefix).color("$4").text(uuidMapping.getUsername()).color("$1")
.tooltip(new PlotMessage("Offline").color("$3")); .tooltip(new PlotMessage("Offline").color("$3"));
} }
prefix = ", ";
} }
prefix = ", "; } catch (InterruptedException | ExecutionException e) {
MainUtil.sendMessage(player, Captions.INVALID_PLAYER);
} catch (TimeoutException e) {
MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT);
} }
} }
}, "/plot list " + args[0], Captions.PLOT_LIST_HEADER_PAGED.getTranslated()); }, "/plot list " + args[0], Captions.PLOT_LIST_HEADER_PAGED.getTranslated());

View File

@ -37,6 +37,7 @@ import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.PatternUtil; import com.plotsquared.core.util.PatternUtil;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.WorldUtil; import com.plotsquared.core.util.WorldUtil;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.world.block.BlockCategory; import com.sk89q.worldedit.world.block.BlockCategory;
@ -158,11 +159,7 @@ public class Set extends SubCommand {
@Override @Override
public Collection<Command> tab(final PlotPlayer player, final String[] args, public Collection<Command> tab(final PlotPlayer player, final String[] args,
final boolean space) { final boolean space) {
return PatternUtil.getSuggestions(player, StringMan.join(args, ",").trim()).stream() return TabCompletions.completePatterns(StringMan.join(args, ","));
.map(value -> value.toLowerCase(Locale.ENGLISH).replace("minecraft:", ""))
.filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
.map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {
}).collect(Collectors.toList());
} }
}; };
} }

View File

@ -62,7 +62,7 @@ public class PatternUtil {
return parse(plotPlayer, input, true); return parse(plotPlayer, input, true);
} }
public static List<String> getSuggestions(PlotPlayer plotPlayer, String input) { public static List<String> getSuggestions(String input) {
try { try {
return WorldEdit.getInstance().getPatternFactory().getSuggestions(input); return WorldEdit.getInstance().getPatternFactory().getSuggestions(input);
} catch (final Exception ignored) { } catch (final Exception ignored) {

View File

@ -45,7 +45,8 @@ import java.util.stream.Collectors;
/** /**
* Tab completion utilities * Tab completion utilities
*/ */
@UtilityClass public class TabCompletions { @UtilityClass
public class TabCompletions {
private final Cache<String, List<String>> cachedCompletionValues = private final Cache<String, List<String>> cachedCompletionValues =
CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(); CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build();
@ -79,4 +80,19 @@ import java.util.stream.Collectors;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
/**
* Get a list of completions corresponding to WorldEdit(/FAWE) patterns. This uses
* WorldEdit's pattern completer internally.
*
* @param input Command input
* @return List of completions
*/
@NotNull public List<Command> completePatterns(@NotNull final String input) {
return PatternUtil.getSuggestions(input.trim()).stream()
.map(value -> value.toLowerCase(Locale.ENGLISH).replace("minecraft:", ""))
.filter(value -> value.startsWith(input.toLowerCase(Locale.ENGLISH)))
.map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {
}).collect(Collectors.toList());
}
} }