6
6
import com .datasiqn .commandcore .command .Command ;
7
7
import com .datasiqn .commandcore .command .builder .CommandBuilder ;
8
8
import org .jetbrains .annotations .NotNull ;
9
- import org .jetbrains .annotations .UnmodifiableView ;
10
9
11
- import java .util .Collections ;
12
10
import java .util .HashMap ;
11
+ import java .util .HashSet ;
13
12
import java .util .Map ;
13
+ import java .util .Set ;
14
14
15
15
/**
16
16
* Class that manages all commands
17
17
*/
18
18
public class CommandManager {
19
- private final Map <String , Command > executableCommands = new HashMap <>();
20
19
private final Map <String , Command > commandMap = new HashMap <>();
20
+ private final Map <String , Command > aliasesMap = new HashMap <>();
21
21
22
22
/**
23
23
* Registers a new command
@@ -33,38 +33,50 @@ public void registerCommand(@NotNull CommandBuilder command) {
33
33
options .warnIf (Warning .MISSING_DESCRIPTION , !builtCommand .hasDescription (), name );
34
34
options .warnIf (Warning .MISSING_PERMISSION , !builtCommand .hasPermission (), name );
35
35
commandMap .put (name , builtCommand );
36
- executableCommands .put (name , builtCommand );
37
36
for (String alias : builtCommand .getAliases ()) {
38
37
if (alias .contains (" " )) throw new IllegalArgumentException ("Command aliases cannot contain spaces" );
39
38
if (alias .isEmpty ()) throw new IllegalArgumentException ("Command aliases cannot be empty" );
40
- executableCommands .put (alias , builtCommand );
39
+ aliasesMap .put (alias , builtCommand );
41
40
}
42
41
}
43
42
44
43
/**
45
44
* Gets the command from its name
46
45
* @param name The name of the command
46
+ * @param alias Whether {@code name} is a command alias or not
47
47
* @return The command, or null if it doesn't exist
48
48
*/
49
- public Command getCommand (String name ) {
50
- return executableCommands .get (name );
49
+ public Command getCommand (String name , boolean alias ) {
50
+ return alias ? aliasesMap . get ( name ) : commandMap .get (name );
51
51
}
52
52
53
53
/**
54
54
* Checks whether a command with that name exists or not
55
55
* @param name The command name
56
+ * @param alias Whether {@code name} is a command alias or not
56
57
* @return {@code true} if the command exists, otherwise {@code false}
57
58
*/
58
- public boolean hasCommand (String name ) {
59
- return executableCommands .containsKey (name );
59
+ public boolean hasCommand (String name , boolean alias ) {
60
+ return alias ? aliasesMap . containsKey ( name ) : commandMap .containsKey (name );
60
61
}
61
62
62
63
/**
63
- * Gets a view of all registered commands
64
- * @return All registered commands
64
+ * Gets whether {@code name} is a command alias or not
65
+ * @param name The command name/alias
66
+ * @return {@code true} if {@code name} is a command alias, {@code false} otherwise
65
67
*/
66
- @ UnmodifiableView
67
- public Map <String , Command > allCommands () {
68
- return Collections .unmodifiableMap (commandMap );
68
+ public boolean isAlias (String name ) {
69
+ return aliasesMap .containsKey (name );
70
+ }
71
+
72
+ /**
73
+ * Gets all command names
74
+ * @param includeAliases Whether to include command aliases or not
75
+ * @return All command names
76
+ */
77
+ public @ NotNull Set <String > getCommandNames (boolean includeAliases ) {
78
+ Set <String > names = new HashSet <>(commandMap .keySet ());
79
+ if (includeAliases ) names .addAll (aliasesMap .keySet ());
80
+ return names ;
69
81
}
70
82
}
0 commit comments