From fa2ad8ab22a32b8d690cb66941f1a15dc9277ae4 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 29 Jun 2020 13:48:18 +0200 Subject: [PATCH] Improve plot alias command (tab complete, admin permission) fixes PS-63 --- .../com/plotsquared/core/command/Alias.java | 79 +++++++++++++------ .../core/configuration/Captions.java | 2 + .../core/util/query/PlotQuery.java | 24 ++++++ 3 files changed, 80 insertions(+), 25 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Alias.java b/Core/src/main/java/com/plotsquared/core/command/Alias.java index a10bf8a6e..59c0da49c 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Alias.java +++ b/Core/src/main/java/com/plotsquared/core/command/Alias.java @@ -33,17 +33,24 @@ import com.plotsquared.core.plot.Plot; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.Permissions; +import com.plotsquared.core.util.query.PlotQuery; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.concurrent.TimeoutException; -@CommandDeclaration(command = "setalias", +@CommandDeclaration(command = "alias", permission = "plots.alias", description = "Set the plot name", usage = "/plot alias ", - aliases = {"alias", "sa", "name", "rename", "setname", "seta", "nameplot"}, + aliases = {"setalias", "sa", "name", "rename", "setname", "seta", "nameplot"}, category = CommandCategory.SETTINGS, requiredType = RequiredType.PLAYER) public class Alias extends SubCommand { + private static final Command SET_COMMAND = new Command(null, false, "set", null, RequiredType.NONE, null) {}; + private static final Command REMOVE_COMMAND = new Command(null, false, "remove", null, RequiredType.NONE, null) {}; @Override public boolean onCommand(PlotPlayer player, String[] args) { @@ -63,13 +70,11 @@ public class Alias extends SubCommand { return false; } - if (!plot.isOwner(player.getUUID())) { - MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS); - return false; - } - boolean result = false; + boolean owner = plot.isOwner(player.getUUID()); + boolean permission; + boolean admin; switch (args[0].toLowerCase()) { case "set": if (args.length != 2) { @@ -77,18 +82,34 @@ public class Alias extends SubCommand { return false; } - if (canExecuteCommand(player, Captions.PERMISSION_ALIAS_SET, false) - || canExecuteCommand(player, Captions.PERMISSION_ALIAS_SET_OBSOLETE, false)) { + permission = isPermitted(player, Captions.PERMISSION_ALIAS_SET) + || isPermitted(player, Captions.PERMISSION_ALIAS_SET_OBSOLETE); + admin = isPermitted(player, Captions.PERMISSION_ADMIN_ALIAS_SET); + if (!admin && !owner) { + MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS); + return false; + } + if (permission) { // is either admin or owner setAlias(player, plot, args[1]); return true; } else { - MainUtil.sendMessage(player, Captions.NO_PERMISSION); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, + Captions.PERMISSION_ALIAS_SET.getTranslated()); } break; case "remove": - if (canExecuteCommand(player, Captions.PERMISSION_ALIAS_REMOVE, true)) { + permission = isPermitted(player, Captions.PERMISSION_ALIAS_REMOVE); + admin = isPermitted(player, Captions.PERMISSION_ADMIN_ALIAS_REMOVE); + if (!admin && !owner) { + MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS); + return false; + } + if (permission) { result = removeAlias(player, plot); + } else { + MainUtil.sendMessage(player, Captions.NO_PERMISSION, + Captions.PERMISSION_ALIAS_REMOVE.getTranslated()); } break; default: @@ -99,6 +120,20 @@ public class Alias extends SubCommand { return result; } + @Override + public Collection tab(PlotPlayer player, String[] args, boolean space) { + final List commands = new ArrayList<>(2); + if (args.length == 1) { + if ("set".startsWith(args[0])) { + commands.add(SET_COMMAND); + } + if ("remove".startsWith(args[0])) { + commands.add(REMOVE_COMMAND); + } + return commands; + } + return Collections.emptySet(); + } private void setAlias(PlotPlayer player, Plot plot, String alias) { if (alias.isEmpty()) { @@ -110,11 +145,11 @@ public class Alias extends SubCommand { } else if (MathMan.isInteger(alias)) { Captions.NOT_VALID_VALUE.send(player); } else { - for (Plot p : PlotSquared.get().getPlots(plot.getArea())) { - if (p.getAlias().equalsIgnoreCase(alias)) { - MainUtil.sendMessage(player, Captions.ALIAS_IS_TAKEN); - return; - } + if (PlotQuery.newQuery().inArea(plot.getArea()) + .withAlias(alias) + .anyMatch()) { + MainUtil.sendMessage(player, Captions.ALIAS_IS_TAKEN); + return; } PlotSquared.get().getImpromptuUUIDPipeline().getSingle(alias, ((uuid, throwable) -> { if (throwable instanceof TimeoutException) { @@ -130,19 +165,13 @@ public class Alias extends SubCommand { } } - private boolean removeAlias(PlotPlayer player, Plot plot) { + private boolean removeAlias(PlotPlayer player, Plot plot) { plot.setAlias(null); MainUtil.sendMessage(player, Captions.ALIAS_REMOVED.getTranslated()); return true; } - private boolean canExecuteCommand(PlotPlayer player, Captions caption, boolean sendMessage) { - if (!Permissions.hasPermission(player, caption)) { - if (sendMessage) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION); - } - return false; - } - return true; + private boolean isPermitted(PlotPlayer player, Captions caption) { + return Permissions.hasPermission(player, caption); } } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java index 03874da48..dbcf463ac 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java @@ -182,7 +182,9 @@ public enum Captions implements Caption { PERMISSION_HOME("plots.home", "static.permissions"), PERMISSION_ALIAS_SET_OBSOLETE("plots.set.alias", "static.permissions"), // Note this is for backwards compatibility PERMISSION_ALIAS_SET("plots.alias.set", "static.permissions"), + PERMISSION_ADMIN_ALIAS_SET("plots.admin.alias.set", "static.permissions"), PERMISSION_ALIAS_REMOVE("plots.alias.remove", "static.permissions"), + PERMISSION_ADMIN_ALIAS_REMOVE("plots.admin.alias.remove", "static.permissions"), PERMISSION_ADMIN_CHAT_BYPASS("plots.admin.chat.bypass", "static.permissions"), PERMISSION_BACKUP("plots.backup", "static.permissions"), PERMISSION_BACKUP_SAVE("plots.backup.save", "static.permissions"), diff --git a/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java b/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java index 40d883062..27b4352eb 100644 --- a/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java +++ b/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java @@ -376,6 +376,30 @@ public final class PlotQuery { return this.asList(); } + /** + * Get whether any provided plot matches the given filters. + * If no plot was provided, false will be returned. + * + * @return true if any provided plot matches the filters. + */ + public boolean anyMatch() { + if (this.filters.isEmpty()) { + return !this.plotProvider.getPlots().isEmpty(); + } else { + final Collection plots = this.plotProvider.getPlots(); + outer: for (final Plot plot : plots) { + // a plot must pass all filters to match the criteria + for (final PlotFilter filter : this.filters) { + if (!filter.accepts(plot)) { + continue outer; + } + } + return true; // a plot passed all filters, so we have a match + } + return false; + } + } + @NotNull private PlotQuery addFilter(@NotNull final PlotFilter filter) { this.filters.add(filter); return this;