Skip to content

Commit e83e127

Browse files
committed
Display usage information when using an existing command with wrong arguments.
Fixes #1.
1 parent 1f22c70 commit e83e127

2 files changed

Lines changed: 47 additions & 31 deletions

File tree

src/main/java/de/craften/plugins/bkcommandapi/CommandHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ enum Result {
1818
Done,
1919
/**
2020
* The command was not found.
21+
*
22+
* @deprecated use {@link #InvalidUsage} instead
2123
*/
24+
@Deprecated
2225
CommandNotFound,
26+
/**
27+
* The command was used incorrectly.
28+
*/
29+
InvalidUsage,
2330
/**
2431
* The sender that invoked the command has no permission to use it.
2532
*/

src/main/java/de/craften/plugins/bkcommandapi/SubCommandHandler.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,54 +49,63 @@ public final boolean onCommand(CommandSender sender, org.bukkit.command.Command
4949
}
5050
}
5151

52+
List<Command> possibleCommands = new ArrayList<>();
5253
for (CommandHandler handler : handlers) {
5354
for (Method method : handler.getClass().getMethods()) {
5455
if (method.isAnnotationPresent(Command.class)) {
5556
Command methodCommand = method.getAnnotation(Command.class);
5657
if (((methodCommand.value().length == 0 && subCommand.isEmpty()) || containsAny(methodCommand.value(), subCommand))
57-
&& args.length >= methodCommand.min() && args.length <= methodCommand.max()
5858
&& (methodCommand.allowFromConsole() || sender instanceof Player)) {
59+
possibleCommands.add(methodCommand);
5960

60-
if (!methodCommand.permission().isEmpty() && !sender.hasPermission(methodCommand.permission())) {
61-
onPermissionDenied(sender, command, args);
62-
return true;
63-
}
64-
65-
try {
66-
Object result;
67-
if (methodCommand.min() > 0 || methodCommand.max() > 0
68-
|| method.getParameterTypes().length == 3) { //handler may not have the third argument if there are no arguments
69-
result = method.invoke(handler, sender, args);
70-
} else {
71-
result = method.invoke(handler, sender);
61+
if (args.length >= methodCommand.min() && args.length <= methodCommand.max()) {
62+
if (!methodCommand.permission().isEmpty() && !sender.hasPermission(methodCommand.permission())) {
63+
onPermissionDenied(sender, command, args);
64+
return true;
7265
}
7366

74-
if (result instanceof Boolean) {
75-
return (boolean) result;
76-
} else if (result instanceof CommandHandler.Result) {
77-
switch ((CommandHandler.Result) result) {
78-
case CommandNotFound:
79-
onInvalidCommand(sender);
80-
return false;
81-
case NoPermission:
82-
onPermissionDenied(sender, command, args);
83-
return true;
84-
case Done:
85-
return true;
67+
try {
68+
Object result;
69+
if (methodCommand.min() > 0 || methodCommand.max() > 0
70+
|| method.getParameterTypes().length == 3) { //handler may not have the third argument if there are no arguments
71+
result = method.invoke(handler, sender, args);
72+
} else {
73+
result = method.invoke(handler, sender);
8674
}
87-
} else {
88-
return true;
75+
76+
if (result instanceof Boolean) {
77+
return (boolean) result;
78+
} else if (result instanceof CommandHandler.Result) {
79+
switch ((CommandHandler.Result) result) {
80+
case NoPermission:
81+
onPermissionDenied(sender, command, args);
82+
return true;
83+
case Done:
84+
return true;
85+
case InvalidUsage: // intentionally empty
86+
default:
87+
break;
88+
}
89+
} else {
90+
return true;
91+
}
92+
} catch (IllegalAccessException | InvocationTargetException e) {
93+
Bukkit.getLogger().severe("Error invoking handler for command: " + command.toString());
94+
e.printStackTrace();
8995
}
90-
} catch (IllegalAccessException | InvocationTargetException e) {
91-
Bukkit.getLogger().severe("Error invoking handler for command: " + command.toString());
92-
e.printStackTrace();
9396
}
9497
}
9598
}
9699
}
97100
}
98101

99-
onInvalidCommand(sender);
102+
if (possibleCommands.isEmpty()) {
103+
onInvalidCommand(sender);
104+
} else {
105+
for (Command cmd : possibleCommands) {
106+
sendUsageHelp(sender, cmd);
107+
}
108+
}
100109
return false;
101110
}
102111

0 commit comments

Comments
 (0)