diff --git a/core/src/main/java/co/aikar/commands/BaseCommand.java b/core/src/main/java/co/aikar/commands/BaseCommand.java index 119975c6..830e9b0d 100644 --- a/core/src/main/java/co/aikar/commands/BaseCommand.java +++ b/core/src/main/java/co/aikar/commands/BaseCommand.java @@ -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 subCommands = HashMultimap.create(); final Map, 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 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; diff --git a/core/src/main/java/co/aikar/commands/CommandHelp.java b/core/src/main/java/co/aikar/commands/CommandHelp.java index 11b35ea0..d25bc796 100644 --- a/core/src/main/java/co/aikar/commands/CommandHelp.java +++ b/core/src/main/java/co/aikar/commands/CommandHelp.java @@ -53,7 +53,7 @@ public class CommandHelp { Set 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; } diff --git a/core/src/main/java/co/aikar/commands/RegisteredCommand.java b/core/src/main/java/co/aikar/commands/RegisteredCommand.java index 9c4daf7e..3fcf2151 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -72,7 +72,7 @@ public class RegisteredCommand { 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 registered = subCommands.get(key); diff --git a/core/src/main/java/co/aikar/commands/annotation/CatchAll.java b/core/src/main/java/co/aikar/commands/annotation/CatchAll.java index 672c50bf..7d3726f6 100644 --- a/core/src/main/java/co/aikar/commands/annotation/CatchAll.java +++ b/core/src/main/java/co/aikar/commands/annotation/CatchAll.java @@ -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 {} diff --git a/core/src/main/java/co/aikar/commands/annotation/CatchUnknown.java b/core/src/main/java/co/aikar/commands/annotation/CatchUnknown.java new file mode 100644 index 00000000..0b406a36 --- /dev/null +++ b/core/src/main/java/co/aikar/commands/annotation/CatchUnknown.java @@ -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 { +}