Improve plot alias command (tab complete, admin permission) fixes PS-63

This commit is contained in:
Hannes Greule 2020-06-29 13:48:18 +02:00
parent bd9bdc9e03
commit fa2ad8ab22
3 changed files with 80 additions and 25 deletions

View File

@ -33,17 +33,24 @@ import com.plotsquared.core.plot.Plot;
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.Permissions; 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; import java.util.concurrent.TimeoutException;
@CommandDeclaration(command = "setalias", @CommandDeclaration(command = "alias",
permission = "plots.alias", permission = "plots.alias",
description = "Set the plot name", description = "Set the plot name",
usage = "/plot alias <set|remove> <alias>", usage = "/plot alias <set|remove> <alias>",
aliases = {"alias", "sa", "name", "rename", "setname", "seta", "nameplot"}, aliases = {"setalias", "sa", "name", "rename", "setname", "seta", "nameplot"},
category = CommandCategory.SETTINGS, category = CommandCategory.SETTINGS,
requiredType = RequiredType.PLAYER) requiredType = RequiredType.PLAYER)
public class Alias extends SubCommand { 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) { @Override public boolean onCommand(PlotPlayer<?> player, String[] args) {
@ -63,13 +70,11 @@ public class Alias extends SubCommand {
return false; return false;
} }
if (!plot.isOwner(player.getUUID())) {
MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS);
return false;
}
boolean result = false; boolean result = false;
boolean owner = plot.isOwner(player.getUUID());
boolean permission;
boolean admin;
switch (args[0].toLowerCase()) { switch (args[0].toLowerCase()) {
case "set": case "set":
if (args.length != 2) { if (args.length != 2) {
@ -77,18 +82,34 @@ public class Alias extends SubCommand {
return false; return false;
} }
if (canExecuteCommand(player, Captions.PERMISSION_ALIAS_SET, false) permission = isPermitted(player, Captions.PERMISSION_ALIAS_SET)
|| canExecuteCommand(player, Captions.PERMISSION_ALIAS_SET_OBSOLETE, false)) { || 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]); setAlias(player, plot, args[1]);
return true; return true;
} else { } else {
MainUtil.sendMessage(player, Captions.NO_PERMISSION); MainUtil.sendMessage(player, Captions.NO_PERMISSION,
Captions.PERMISSION_ALIAS_SET.getTranslated());
} }
break; break;
case "remove": 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); result = removeAlias(player, plot);
} else {
MainUtil.sendMessage(player, Captions.NO_PERMISSION,
Captions.PERMISSION_ALIAS_REMOVE.getTranslated());
} }
break; break;
default: default:
@ -99,6 +120,20 @@ public class Alias extends SubCommand {
return result; return result;
} }
@Override
public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) {
final List<Command> 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) { private void setAlias(PlotPlayer player, Plot plot, String alias) {
if (alias.isEmpty()) { if (alias.isEmpty()) {
@ -110,11 +145,11 @@ public class Alias extends SubCommand {
} else if (MathMan.isInteger(alias)) { } else if (MathMan.isInteger(alias)) {
Captions.NOT_VALID_VALUE.send(player); Captions.NOT_VALID_VALUE.send(player);
} else { } else {
for (Plot p : PlotSquared.get().getPlots(plot.getArea())) { if (PlotQuery.newQuery().inArea(plot.getArea())
if (p.getAlias().equalsIgnoreCase(alias)) { .withAlias(alias)
MainUtil.sendMessage(player, Captions.ALIAS_IS_TAKEN); .anyMatch()) {
return; MainUtil.sendMessage(player, Captions.ALIAS_IS_TAKEN);
} return;
} }
PlotSquared.get().getImpromptuUUIDPipeline().getSingle(alias, ((uuid, throwable) -> { PlotSquared.get().getImpromptuUUIDPipeline().getSingle(alias, ((uuid, throwable) -> {
if (throwable instanceof TimeoutException) { 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); plot.setAlias(null);
MainUtil.sendMessage(player, Captions.ALIAS_REMOVED.getTranslated()); MainUtil.sendMessage(player, Captions.ALIAS_REMOVED.getTranslated());
return true; return true;
} }
private boolean canExecuteCommand(PlotPlayer player, Captions caption, boolean sendMessage) { private boolean isPermitted(PlotPlayer<?> player, Captions caption) {
if (!Permissions.hasPermission(player, caption)) { return Permissions.hasPermission(player, caption);
if (sendMessage) {
MainUtil.sendMessage(player, Captions.NO_PERMISSION);
}
return false;
}
return true;
} }
} }

View File

@ -182,7 +182,9 @@ public enum Captions implements Caption {
PERMISSION_HOME("plots.home", "static.permissions"), PERMISSION_HOME("plots.home", "static.permissions"),
PERMISSION_ALIAS_SET_OBSOLETE("plots.set.alias", "static.permissions"), // Note this is for backwards compatibility PERMISSION_ALIAS_SET_OBSOLETE("plots.set.alias", "static.permissions"), // Note this is for backwards compatibility
PERMISSION_ALIAS_SET("plots.alias.set", "static.permissions"), 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_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_ADMIN_CHAT_BYPASS("plots.admin.chat.bypass", "static.permissions"),
PERMISSION_BACKUP("plots.backup", "static.permissions"), PERMISSION_BACKUP("plots.backup", "static.permissions"),
PERMISSION_BACKUP_SAVE("plots.backup.save", "static.permissions"), PERMISSION_BACKUP_SAVE("plots.backup.save", "static.permissions"),

View File

@ -376,6 +376,30 @@ public final class PlotQuery {
return this.asList(); 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<Plot> 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) { @NotNull private PlotQuery addFilter(@NotNull final PlotFilter filter) {
this.filters.add(filter); this.filters.add(filter);
return this; return this;