Add @CatchAll as a better named replacement to @UnknownHandler

UnknownHandler will still work until we remove it in some future major update
This commit is contained in:
Aikar
2018-01-14 19:01:26 -05:00
parent 235914abc1
commit a14b30caea
9 changed files with 56 additions and 19 deletions
@@ -65,7 +65,7 @@ public class BukkitRootCommand extends Command implements RootCommand {
}
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
this.defCommand = command;
this.setPermission(command.permission);
//this.setDescription(command.getDescription());
@@ -54,7 +54,7 @@ public class BungeeRootCommand extends Command implements RootCommand, TabExecut
@Override
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
this.defCommand = command;
}
@@ -23,6 +23,7 @@
package co.aikar.commands;
import co.aikar.commands.annotation.CatchAll;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Default;
@@ -60,7 +61,7 @@ import java.util.stream.Stream;
@SuppressWarnings("unused")
public abstract class BaseCommand {
public static final String UNKNOWN = "__unknown";
public static final String CATCHALL = "__catchall";
public static final String DEFAULT = "__default";
final SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
private Method preCommandHandler;
@@ -138,7 +139,7 @@ public abstract class BaseCommand {
}
boolean foundDefault = false;
boolean foundUnknown = false;
boolean foundCatchAll = false;
boolean isParentEmpty = parentSubcommand.isEmpty();
for (Method method : self.getMethods()) {
method.setAccessible(true);
@@ -172,14 +173,16 @@ public abstract class BaseCommand {
}
UnknownHandler unknown = method.getAnnotation(UnknownHandler.class);
CatchAll catchAll = method.getAnnotation(CatchAll.class);
PreCommand preCommand = method.getAnnotation(PreCommand.class);
if (unknown != null || (!foundUnknown && helpCommand != null)) {
if (!foundUnknown) {
if (unknown != null) {
this.subCommands.get(UNKNOWN).clear();
foundUnknown = true;
boolean hasCatchAll = catchAll != null || unknown != null;
if (hasCatchAll || (!foundCatchAll && helpCommand != null)) {
if (!foundCatchAll) {
if (hasCatchAll) {
this.subCommands.get(CATCHALL).clear();
foundCatchAll = true;
}
registerSubcommand(method, UNKNOWN);
registerSubcommand(method, CATCHALL);
} else {
ACFUtil.sneaky(new IllegalStateException("Multiple @UnknownHandler/@HelpCommand commands, duplicate on " + method.getDeclaringClass().getName() + "#" + method.getName()));
}
@@ -353,8 +356,8 @@ public abstract class BaseCommand {
if (subCommands.get(DEFAULT) != null && args.length == 0) {
executeSubcommand(commandContext, DEFAULT, issuer, args);
} else if (subCommands.get(UNKNOWN) != null) {
if (!executeSubcommand(commandContext, UNKNOWN, issuer, args)) {
} else if (subCommands.get(CATCHALL) != null) {
if (!executeSubcommand(commandContext, CATCHALL, issuer, args)) {
help(issuer, args);
}
} else if (subCommands.get(DEFAULT) != null) {
@@ -473,8 +476,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(UNKNOWN).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(UNKNOWN)), args, 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(DEFAULT).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(DEFAULT)), args, commandLabel, isAsync));
}
@@ -490,7 +493,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) && !UNKNOWN.equals(key) && !DEFAULT.equals(key)) {
if (key.startsWith(argString) && !CATCHALL.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("__default") || key.equals("__unknown")){
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHALL)){
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 ("__unknown".equals(prefSubCommand) || "__default".equals(prefSubCommand)) {
if (BaseCommand.CATCHALL.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.UNKNOWN)) {
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHALL)) {
return;
}
Set<RegisteredCommand> registered = subCommands.get(key);
@@ -0,0 +1,30 @@
/*
* 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 CatchAll {}
@@ -27,5 +27,9 @@ package co.aikar.commands.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @deprecated Use {@link CatchAll instead, which is more accurately named}
*/
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
public @interface UnknownHandler {}
@@ -96,7 +96,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
}
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
this.defCommand = command;
}
addChildShared(this.children, this.subCommands, command);