Skip to content

Commit 2136ea4

Browse files
committed
Fixed completer aliases not working and fixed registering subcommands as a Bukkit command
1 parent 34a474b commit 2136ea4

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

src/main/java/dev/despical/commandframework/internal/CommandRegistry.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,17 @@ public void registerAllInPackage(@NotNull String packageName) {
122122
Object instance = clazz.getDeclaredConstructor().newInstance();
123123

124124
for (Method method : clazz.getDeclaredMethods()) {
125-
Command cmd = method.getAnnotation(Command.class);
126-
if (cmd != null) registerCommand(cmd, method, instance);
125+
Command command = method.getAnnotation(Command.class);
127126

128-
Completer comp = method.getAnnotation(Completer.class);
129-
if (comp != null) registerCompleter(instance, method);
127+
if (command != null) {
128+
registerCommand(command, method, instance);
129+
}
130+
131+
Completer completer = method.getAnnotation(Completer.class);
132+
133+
if (completer != null) {
134+
registerCompleter(instance, method);
135+
}
130136
}
131137
} catch (Exception ignored) {}
132138
}
@@ -189,6 +195,10 @@ private void verifySubCommandHierarchy() {
189195
}
190196

191197
private void registerToBukkitSafely(Command command, String label) {
198+
if (label.contains(".")) {
199+
return;
200+
}
201+
192202
try {
193203
Plugin plugin = CommandFramework.getInstance().getPlugin();
194204
PluginCommand pc = PLUGIN_COMMAND_CONSTRUCTOR.newInstance(label, plugin);
@@ -200,16 +210,24 @@ private void registerToBukkitSafely(Command command, String label) {
200210

201211
String prefix = command.fallbackPrefix().isEmpty() ? plugin.getName() : command.fallbackPrefix();
202212
commandMap.register(prefix, pc);
203-
} catch (Exception e) {
204-
CommandFramework.getInstance().getLogger().log(Level.SEVERE, "Command registration failed for: " + label, e);
213+
} catch (Exception exception) {
214+
CommandFramework.getInstance().getLogger().log(Level.SEVERE, "Command registration failed for: " + label, exception);
205215
}
206216
}
207217

208218
private void registerCompleter(Object instance, Method method) {
209219
if (!List.class.isAssignableFrom(method.getReturnType())) return;
210220

211221
Completer completer = method.getAnnotation(Completer.class);
212-
String[] parts = completer.name().toLowerCase(Locale.ENGLISH).split("\\.");
222+
innerRegisterCompleter(completer.name(), completer, method, instance);
223+
224+
for (String alias : completer.aliases()) {
225+
innerRegisterCompleter(alias, completer, method, instance);
226+
}
227+
}
228+
229+
private void innerRegisterCompleter(String name, Completer completer, Method method, Object instance) {
230+
String[] parts = name.split("\\.");
213231
CommandNode<Completer> node = completionTree.computeIfAbsent(parts[0], k -> new CommandNode<>());
214232

215233
for (int i = 1; i < parts.length; i++) {
@@ -219,13 +237,13 @@ private void registerCompleter(Object instance, Method method) {
219237
try {
220238
MethodHandle handle = MethodHandles.lookup().unreflect(method);
221239
node.setMember(new RegisteredMember<>(instance, method, handle, completer));
222-
} catch (IllegalAccessException e) {
223-
CommandFramework.getInstance().getLogger().log(Level.SEVERE, "Failed to register completer: " + completer.name(), e);
240+
} catch (IllegalAccessException exception) {
241+
CommandFramework.getInstance().getLogger().log(Level.SEVERE, "Failed to register completer: " + name, exception);
224242
}
225243
}
226244

227245
public void unregisterCommand(@NotNull String commandName) {
228-
String rootLabel = commandName.split("\\.")[0].toLowerCase(Locale.ENGLISH);
246+
String rootLabel = commandName.split("\\.")[0];
229247

230248
if (!commandTree.containsKey(rootLabel)) return;
231249

0 commit comments

Comments
 (0)