@@ -22,7 +22,8 @@ public class CommandManager {
22
22
/**
23
23
* Registers a new command
24
24
* @param command The command
25
- * @throws IllegalArgumentException If {@code command}'s name or one of {@code command}'s aliases is empty or contains spaces
25
+ * @throws IllegalArgumentException If {@code command}'s name or one of its aliases is empty or contains spaces.
26
+ * If {@code command}'s name or one of its aliases are already used
26
27
*/
27
28
public void registerCommand (@ NotNull CommandBuilder command ) {
28
29
Command builtCommand = command .build ();
@@ -32,11 +33,12 @@ public void registerCommand(@NotNull CommandBuilder command) {
32
33
InitOptions options = CommandCore .getInstance ().getOptions ();
33
34
options .warnIf (Warning .MISSING_DESCRIPTION , !builtCommand .hasDescription (), name );
34
35
options .warnIf (Warning .MISSING_PERMISSION , !builtCommand .hasPermission (), name );
35
- commandMap .put (name , builtCommand );
36
+ if ( commandMap .putIfAbsent (name , builtCommand ) != null ) throw new IllegalArgumentException ( "Command name already in use" );
36
37
for (String alias : builtCommand .getAliases ()) {
37
38
if (alias .contains (" " )) throw new IllegalArgumentException ("Command aliases cannot contain spaces" );
38
39
if (alias .isEmpty ()) throw new IllegalArgumentException ("Command aliases cannot be empty" );
39
- aliasesMap .put (alias , builtCommand );
40
+ Command prev = aliasesMap .putIfAbsent (alias , builtCommand );
41
+ if (prev != null ) throw new IllegalArgumentException ("Command alias already in use (used by " + prev .getName () + ")" );
40
42
}
41
43
}
42
44
0 commit comments