Skip to content

Commit 13ab710

Browse files
added exceptions to make sure all command names and aliases are unique
1 parent 1b21c6c commit 13ab710

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main/java/com/datasiqn/commandcore/managers/CommandManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class CommandManager {
2222
/**
2323
* Registers a new command
2424
* @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
2627
*/
2728
public void registerCommand(@NotNull CommandBuilder command) {
2829
Command builtCommand = command.build();
@@ -32,11 +33,12 @@ public void registerCommand(@NotNull CommandBuilder command) {
3233
InitOptions options = CommandCore.getInstance().getOptions();
3334
options.warnIf(Warning.MISSING_DESCRIPTION, !builtCommand.hasDescription(), name);
3435
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");
3637
for (String alias : builtCommand.getAliases()) {
3738
if (alias.contains(" ")) throw new IllegalArgumentException("Command aliases cannot contain spaces");
3839
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() + ")");
4042
}
4143
}
4244

0 commit comments

Comments
 (0)