Skip to content

Commit fd9a766

Browse files
committed
Added support for sub-command aliases
1 parent 729a21d commit fd9a766

File tree

4 files changed

+90
-15
lines changed

4 files changed

+90
-15
lines changed

.github/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ To add this project as a dependency to your project, add the following to your p
3737
<dependency>
3838
<groupId>com.github.Despical</groupId>
3939
<artifactId>CommandFramework</artifactId>
40-
<version>1.3.1</version>
40+
<version>1.3.2</version>
4141
<scope>compile</scope>
4242
</dependency>
4343
```
@@ -50,7 +50,7 @@ repositories {
5050
```
5151
```
5252
dependencies {
53-
compileOnly group: "com.github.Despical", name: "CommandFramework", version: "1.3.1";
53+
compileOnly group: "com.github.Despical", name: "CommandFramework", version: "1.3.2";
5454
}
5555
```
5656

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>me.despical</groupId>
55
<artifactId>command-framework</artifactId>
6-
<version>1.3.1</version>
6+
<version>1.3.2</version>
77

88
<name>Command Framework</name>
99
<inceptionYear>2020</inceptionYear>

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public CommandFramework(@NotNull Plugin plugin) {
115115
field.setAccessible(true);
116116

117117
commandMap = (CommandMap) field.get(manager);
118-
} catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException e) {
119-
e.printStackTrace();
118+
} catch (ReflectiveOperationException exception) {
119+
exception.printStackTrace();
120120
}
121121
}
122122
}
@@ -159,6 +159,10 @@ public void registerCommands(@NotNull Object instance) {
159159
}
160160

161161
registerCommand(command, method, instance);
162+
163+
// If aliases are registered as a sub-command then register them as a plugin command.
164+
// Otherwise, they will not be recognized as a command and will not be proceeded.
165+
Stream.of(command.aliases()).filter(alias -> alias.contains(".")).forEach(alias -> registerCommand(Utils.createCommand(command, alias), method, instance));
162166
} else if (method.isAnnotationPresent(Completer.class)) {
163167
if (!List.class.isAssignableFrom(method.getReturnType())) {
164168
plugin.getLogger().log(Level.WARNING, "Skipped registration of ''{0}'' because it is not returning java.util.List type.", method.getName());
@@ -177,18 +181,11 @@ public void registerCommands(@NotNull Object instance) {
177181

178182
subCommands.forEach((key, value) -> {
179183
final String splitName = key.name().split("\\.")[0];
180-
boolean shouldThrowException = false;
181184

182-
// This is an unsupported behaviour at least for now.
183-
// All sub-commands must have their own main command to be registered.
185+
// Framework is going to work properly but this should not be handled that way.
184186
if (commands.keySet().stream().noneMatch(cmd -> cmd.name().equals(splitName))) {
185-
shouldThrowException = true;
186-
187-
unregisterCommand(key.name());
187+
registerCommand(Utils.createCommand(key, splitName), null, null);
188188
}
189-
190-
if (shouldThrowException)
191-
throw new CommandException("You can not create sub-commands without a main command! (%s)", key.name());
192189
});
193190
}
194191

@@ -217,7 +214,7 @@ private void registerCommand(Command command, Method method, Object instance) {
217214
pluginCommand.setUsage(command.usage());
218215
pluginCommand.setPermission(!command.permission().isEmpty() ? null : command.permission());
219216
pluginCommand.setDescription(command.desc());
220-
pluginCommand.setAliases(Stream.of(command.aliases()).map(String::toLowerCase).collect(Collectors.toList()));
217+
pluginCommand.setAliases(Stream.of(command.aliases()).filter(alias -> !alias.contains(".")).collect(Collectors.toList()));
221218

222219
commandMap.register(cmdName, pluginCommand);
223220
} catch (Exception exception) {
@@ -406,6 +403,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
406403
final Method method = entry.getValue().getKey();
407404
final Object instance = entry.getValue().getValue();
408405

406+
if (method == null)
407+
return;
408+
409409
if (method.isAnnotationPresent(NoCommandArguments.class)) {
410410
method.invoke(instance);
411411
return;

src/main/java/me/despical/commandframework/utils/Utils.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
package me.despical.commandframework.utils;
2020

21+
import me.despical.commandframework.Command;
22+
import org.jetbrains.annotations.ApiStatus;
23+
24+
import java.lang.annotation.Annotation;
2125
import java.util.AbstractMap;
2226
import java.util.Arrays;
2327
import java.util.Map;
@@ -30,6 +34,7 @@
3034
*
3135
* This class is a part of Despical's Commons library.
3236
*/
37+
@ApiStatus.Internal
3338
public class Utils {
3439

3540
private Utils() {
@@ -189,4 +194,74 @@ public static <K, V> Map<K, V> mapOf(K a, V b) {
189194
public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... a) {
190195
return Arrays.stream(a).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (b, c) -> c));
191196
}
197+
198+
public static Command createCommand(final Command command, final String commandName) {
199+
return new Command() {
200+
201+
@Override
202+
public String name() {
203+
return commandName;
204+
}
205+
206+
@Override
207+
public String permission() {
208+
return command.permission();
209+
}
210+
211+
@Override
212+
public String[] aliases() {
213+
return new String[0];
214+
}
215+
216+
@Override
217+
public String desc() {
218+
return command.desc();
219+
}
220+
221+
@Override
222+
public String usage() {
223+
return command.usage();
224+
}
225+
226+
@Override
227+
public int min() {
228+
return command.min();
229+
}
230+
231+
@Override
232+
public int max() {
233+
return command.max();
234+
}
235+
236+
@Override
237+
public int cooldown() {
238+
return command.cooldown();
239+
}
240+
241+
@Override
242+
public boolean allowInfiniteArgs() {
243+
return command.allowInfiniteArgs();
244+
}
245+
246+
@Override
247+
public boolean onlyOp() {
248+
return command.onlyOp();
249+
}
250+
251+
@Override
252+
public boolean async() {
253+
return command.async();
254+
}
255+
256+
@Override
257+
public SenderType senderType() {
258+
return command.senderType();
259+
}
260+
261+
@Override
262+
public Class<? extends Annotation> annotationType() {
263+
return command.annotationType();
264+
}
265+
};
266+
}
192267
}

0 commit comments

Comments
 (0)