Skip to content

Commit cb757a8

Browse files
committed
Fixed an exception occurring if a sub-command is registered without a main command
1 parent 9fc4599 commit cb757a8

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

src/main/java/me/despical/commandframework/CommandHandler.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ void setRegistry(CommandFramework commandFramework) {
4545

4646
@Override
4747
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull String label, String[] args) {
48-
final Map.Entry<Command, Map.Entry<Method, Object>> entry = registry.getCommandMatcher().getAssociatedCommand(cmd.getName(), args);
48+
Map.Entry<Command, Map.Entry<Method, Object>> entry = registry.getCommandMatcher().getAssociatedCommand(cmd.getName(), args);
4949

5050
if (entry == null) return false;
5151

52-
final Command command = entry.getKey();
53-
final String permission = command.permission();
54-
final String[] splitted = command.name().split("\\.");
55-
final String[] newArgs = Arrays.copyOfRange(args, splitted.length - 1, args.length);
56-
final CommandArguments arguments = new CommandArguments(sender, cmd, command, label, newArgs);
52+
Method method = entry.getValue().getKey();
53+
54+
if (method == null) {
55+
return false;
56+
}
57+
58+
Command command = entry.getKey();
59+
String permission = command.permission();
60+
String[] split = command.name().split("\\.");
61+
String[] newArgs = Arrays.copyOfRange(args, split.length - 1, args.length);
62+
CommandArguments arguments = new CommandArguments(sender, cmd, command, label, newArgs);
5763

5864
if (command.onlyOp() && !sender.isOp()) {
5965
arguments.sendMessage(Message.MUST_HAVE_OP);
@@ -83,8 +89,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
8389
return arguments.sendMessage(Message.LONG_ARG_SIZE);
8490
}
8591

86-
final Method method = entry.getValue().getKey();
87-
final CommandFramework commandFramework = CommandFramework.getInstance();
92+
CommandFramework commandFramework = CommandFramework.getInstance();
8893

8994
if (commandFramework.checkConfirmation(sender, command, method)) {
9095
return true;
@@ -94,7 +99,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
9499
return true;
95100
}
96101

97-
final boolean parseOptions = method.getAnnotationsByType(Option.class).length + method.getAnnotationsByType(Flag.class).length > 0;
102+
boolean parseOptions = method.getAnnotationsByType(Option.class).length + method.getAnnotationsByType(Flag.class).length > 0;
98103

99104
if (parseOptions) {
100105
OptionParser optionParser = new OptionParser(newArgs, method);
@@ -103,9 +108,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
103108
arguments.setParsedFlags(optionParser.parseFlags());
104109
}
105110

106-
final Runnable invocation = () -> {
111+
Runnable invocation = () -> {
107112
try {
108-
final Object instance = entry.getValue().getValue();
113+
Object instance = entry.getValue().getValue();
109114

110115
method.invoke(instance, parameterHandler.getParameterArray(method, arguments));
111116
} catch (Exception exception) {
@@ -118,7 +123,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
118123
};
119124

120125
if (command.async()) {
121-
final Plugin plugin = commandFramework.plugin;
126+
Plugin plugin = commandFramework.plugin;
122127

123128
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, invocation);
124129
} else {
@@ -130,22 +135,22 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
130135

131136
@Override
132137
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull String label, String[] args) {
133-
final Map.Entry<Completer, Map.Entry<Method, Object>> entry = this.registry.getCommandMatcher().getAssociatedCompleter(cmd.getName(), args);
138+
Map.Entry<Completer, Map.Entry<Method, Object>> entry = this.registry.getCommandMatcher().getAssociatedCompleter(cmd.getName(), args);
134139

135140
if (entry == null)
136141
return null;
137142

138-
final String permission = entry.getKey().permission();
143+
String permission = entry.getKey().permission();
139144

140145
if (!permission.isEmpty() && !sender.hasPermission(permission))
141146
return null;
142147

143148
try {
144-
final Method method = entry.getValue().getKey();
145-
final Object instance = entry.getValue().getValue();
146-
final String[] splitName = entry.getKey().name().split("\\.");
147-
final String[] newArgs = Arrays.copyOfRange(args, splitName.length - 1, args.length);
148-
final Object completer = method.invoke(instance, parameterHandler.getParameterArray(method, new CommandArguments(sender, cmd, null, label, newArgs)));
149+
Method method = entry.getValue().getKey();
150+
Object instance = entry.getValue().getValue();
151+
String[] splitName = entry.getKey().name().split("\\.");
152+
String[] newArgs = Arrays.copyOfRange(args, splitName.length - 1, args.length);
153+
Object completer = method.invoke(instance, parameterHandler.getParameterArray(method, new CommandArguments(sender, cmd, null, label, newArgs)));
149154

150155
return (List<String>) completer;
151156
} catch (Exception exception) {

src/main/java/me/despical/commandframework/CommandRegistry.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public void setCommandMap(@NotNull CommandMap commandMap) {
102102
* @param instance the instance of the class from which commands will be registered. Must not be {@code null}.
103103
*/
104104
protected void registerCommands(@NotNull Object instance) {
105-
final CommandFramework commandFramework = CommandFramework.getInstance();
106-
final boolean notDebug = !commandFramework.options().isEnabled(Option.DEBUG);
105+
CommandFramework commandFramework = CommandFramework.getInstance();
106+
boolean notDebug = !commandFramework.options().isEnabled(Option.DEBUG);
107107

108-
for (final Method method : instance.getClass().getMethods()) {
108+
for (Method method : instance.getClass().getMethods()) {
109109
if (notDebug && method.isAnnotationPresent(Debug.class)) {
110110
continue;
111111
}
112112

113-
final Command command = method.getAnnotation(Command.class);
113+
Command command = method.getAnnotation(Command.class);
114114

115115
if (command != null) {
116116
registerCommand(command, method, instance);
@@ -123,7 +123,7 @@ protected void registerCommands(@NotNull Object instance) {
123123
continue;
124124
}
125125

126-
final Completer completer = method.getAnnotation(Completer.class);
126+
Completer completer = method.getAnnotation(Completer.class);
127127

128128
if (completer.name().contains(".")) {
129129
subCommandCompletions.put(completer, Utils.mapEntry(method, instance));
@@ -134,11 +134,11 @@ protected void registerCommands(@NotNull Object instance) {
134134
}
135135

136136
subCommands.forEach((key, value) -> {
137-
final String splitName = key.name().split("\\.")[0];
137+
String splitName = key.name().split("\\.")[0];
138138

139139
// Framework is going to work properly but this should not be handled that way.
140140
if (commands.keySet().stream().noneMatch(cmd -> cmd.name().equals(splitName))) {
141-
commandFramework.getLogger().log(Level.WARNING, "A sub-command (name: ''{0}'') is directly registered without a main command.", splitName);
141+
commandFramework.getLogger().log(Level.WARNING, "A sub-command (name: ''{0}'') is directly registered without a main command.", key.name());
142142

143143
registerCommand(Utils.createCommand(key, splitName), null, null);
144144
}
@@ -154,19 +154,19 @@ protected void registerCommands(@NotNull Object instance) {
154154
* @param instance the instance of the class that contains the command method.
155155
*/
156156
protected void registerCommand(Command command, Method method, Object instance) {
157-
final CommandFramework commandFramework = CommandFramework.getInstance();
158-
final String cmdName = command.name();
157+
CommandFramework commandFramework = CommandFramework.getInstance();
158+
String cmdName = command.name();
159159

160160
if (cmdName.contains(".")) {
161161
subCommands.put(command, Utils.mapEntry(method, instance));
162162
} else {
163163
commands.put(command, Utils.mapEntry(method, instance));
164164

165165
try {
166-
final Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
166+
Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
167167
constructor.setAccessible(true);
168168

169-
final PluginCommand pluginCommand = constructor.newInstance(cmdName, commandFramework.plugin);
169+
PluginCommand pluginCommand = constructor.newInstance(cmdName, commandFramework.plugin);
170170
pluginCommand.setTabCompleter(commandFramework);
171171
pluginCommand.setExecutor(commandFramework);
172172
pluginCommand.setUsage(command.usage());
@@ -189,17 +189,17 @@ protected void registerCommand(Command command, Method method, Object instance)
189189
protected void unregisterCommand(@NotNull String commandName) {
190190
if (commandName.contains(".")) commandName = commandName.split("\\.")[0];
191191

192-
final Map.Entry<Command, Map.Entry<Method, Object>> entry = commandMatcher.getAssociatedCommand(commandName, new String[0]);
193-
final CommandFramework commandFramework = CommandFramework.getInstance();
192+
Map.Entry<Command, Map.Entry<Method, Object>> entry = commandMatcher.getAssociatedCommand(commandName, new String[0]);
193+
CommandFramework commandFramework = CommandFramework.getInstance();
194194

195195
if (entry == null) {
196196
commandFramework.plugin.getLogger().log(Level.WARNING, "Command removal is failed because there is no command named ''{0}''!", commandName);
197197
return;
198198
}
199199

200-
final Command command = entry.getKey();
201-
final String name = command.name();
202-
final PluginCommand pluginCommand = commandFramework.plugin.getServer().getPluginCommand(name);
200+
Command command = entry.getKey();
201+
String name = command.name();
202+
PluginCommand pluginCommand = commandFramework.plugin.getServer().getPluginCommand(name);
203203

204204
Optional.ofNullable(pluginCommand).ifPresent(cmd -> {
205205
// Do not unregister if matched command is not registered from our instance plugin.

src/main/java/me/despical/commandframework/Message.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ public static void setColorFormatter(@NotNull Function<String, String> colorForm
5858
*
5959
* @param message the custom error message.
6060
*/
61-
public void setMessage(final BiFunction<Command, CommandArguments, Boolean> message) {
61+
public void setMessage(BiFunction<Command, CommandArguments, Boolean> message) {
6262
this.message = message;
6363
}
6464

6565
@ApiStatus.Internal
66-
static String applyColorFormatter(final @NotNull String string) {
66+
static String applyColorFormatter(@NotNull String string) {
6767
return colorFormatter.apply(string);
6868
}
6969

7070
@ApiStatus.Internal
71-
boolean sendMessage(final Command command, final CommandArguments arguments) {
71+
boolean sendMessage(Command command, CommandArguments arguments) {
7272
return this.message.apply(command, arguments);
7373
}
7474

0 commit comments

Comments
 (0)