Deprecate CatchAll in favour of CatchUnknown (#87)

This commit is contained in:
kashike
2018-01-23 20:05:52 -08:00
committed by Daniel Ennis
parent 42a6b112de
commit f858e71d7e
6 changed files with 53 additions and 19 deletions
@@ -24,6 +24,7 @@
package co.aikar.commands;
import co.aikar.commands.annotation.CatchAll;
import co.aikar.commands.annotation.CatchUnknown;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Default;
@@ -62,7 +63,7 @@ import java.util.stream.Stream;
@SuppressWarnings("unused")
public abstract class BaseCommand {
public static final String CATCHALL = "__catchall";
public static final String CATCHUNKNOWN = "__catchunknown";
public static final String DEFAULT = "__default";
final SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
final Map<Class<?>, String> contextFlags = Maps.newHashMap();
@@ -141,7 +142,7 @@ public abstract class BaseCommand {
}
boolean foundDefault = false;
boolean foundCatchAll = false;
boolean foundCatchUnknown = false;
boolean isParentEmpty = parentSubcommand.isEmpty();
for (Method method : self.getMethods()) {
method.setAccessible(true);
@@ -174,17 +175,15 @@ public abstract class BaseCommand {
sublist = helpCommand.value();
}
UnknownHandler unknown = method.getAnnotation(UnknownHandler.class);
CatchAll catchAll = method.getAnnotation(CatchAll.class);
PreCommand preCommand = method.getAnnotation(PreCommand.class);
boolean hasCatchAll = catchAll != null || unknown != null;
if (hasCatchAll || (!foundCatchAll && helpCommand != null)) {
if (!foundCatchAll) {
if (hasCatchAll) {
this.subCommands.get(CATCHALL).clear();
foundCatchAll = true;
boolean hasCatchUnknown = method.isAnnotationPresent(CatchUnknown.class) || method.isAnnotationPresent(CatchAll.class) || method.isAnnotationPresent(UnknownHandler.class);
if (hasCatchUnknown || (!foundCatchUnknown && helpCommand != null)) {
if (!foundCatchUnknown) {
if (hasCatchUnknown) {
this.subCommands.get(CATCHUNKNOWN).clear();
foundCatchUnknown = true;
}
registerSubcommand(method, CATCHALL);
registerSubcommand(method, CATCHUNKNOWN);
} else {
ACFUtil.sneaky(new IllegalStateException("Multiple @UnknownHandler/@HelpCommand commands, duplicate on " + method.getDeclaringClass().getName() + "#" + method.getName()));
}
@@ -358,8 +357,8 @@ public abstract class BaseCommand {
if (subCommands.get(DEFAULT) != null && args.length == 0) {
executeSubcommand(commandContext, DEFAULT, issuer, args);
} else if (subCommands.get(CATCHALL) != null) {
if (!executeSubcommand(commandContext, CATCHALL, issuer, args)) {
} else if (subCommands.get(CATCHUNKNOWN) != null) {
if (!executeSubcommand(commandContext, CATCHUNKNOWN, issuer, args)) {
help(issuer, args);
}
} else if (subCommands.get(DEFAULT) != null) {
@@ -478,8 +477,8 @@ public abstract class BaseCommand {
if (search != null) {
cmds.addAll(completeCommand(issuer, search.cmd, Arrays.copyOfRange(args, search.argIndex, args.length), commandLabel, isAsync));
} else if (subCommands.get(CATCHALL).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(CATCHALL)), args, commandLabel, isAsync));
} else if (subCommands.get(CATCHUNKNOWN).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(CATCHUNKNOWN)), args, commandLabel, isAsync));
} else if (subCommands.get(DEFAULT).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(DEFAULT)), args, commandLabel, isAsync));
}
@@ -495,7 +494,7 @@ public abstract class BaseCommand {
String argString = ApacheCommonsLangUtil.join(args, " ").toLowerCase();
for (Map.Entry<String, RegisteredCommand> entry : subCommands.entries()) {
final String key = entry.getKey();
if (key.startsWith(argString) && !CATCHALL.equals(key) && !DEFAULT.equals(key)) {
if (key.startsWith(argString) && !CATCHUNKNOWN.equals(key) && !DEFAULT.equals(key)) {
final RegisteredCommand value = entry.getValue();
if (!value.hasPermission(issuer)) {
continue;
@@ -53,7 +53,7 @@ public class CommandHelp {
Set<RegisteredCommand> seen = new HashSet<>();
subCommands.entries().forEach(e -> {
String key = e.getKey();
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHALL)){
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)){
return;
}
@@ -72,7 +72,7 @@ public class RegisteredCommand <CEC extends CommandExecutionContext<CEC, ? exten
RegisteredCommand(BaseCommand scope, String command, Method method, String prefSubCommand) {
this.scope = scope;
this.manager = this.scope.manager;
if (BaseCommand.CATCHALL.equals(prefSubCommand) || BaseCommand.DEFAULT.equals(prefSubCommand)) {
if (BaseCommand.CATCHUNKNOWN.equals(prefSubCommand) || BaseCommand.DEFAULT.equals(prefSubCommand)) {
prefSubCommand = "";
}
this.command = command + (method.getAnnotation(CommandAlias.class) == null && !prefSubCommand.isEmpty() ? prefSubCommand : "");
@@ -43,7 +43,7 @@ interface RootCommand {
command.subCommands.entries().forEach(e -> {
String key = e.getKey();
RegisteredCommand registeredCommand = e.getValue();
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHALL)) {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)) {
return;
}
Set<RegisteredCommand> registered = subCommands.get(key);
@@ -26,5 +26,9 @@ package co.aikar.commands.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @deprecated Use {@link CatchUnknown instead, which is more accurately named}
*/
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
public @interface CatchAll {}
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2016-2018 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface CatchUnknown {
}