Skip to content

Commit 1b21c6c

Browse files
changed how aliases are stored
1 parent b392dea commit 1b21c6c

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

src/main/java/com/datasiqn/commandcore/CommandCore.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public InitOptions getOptions() {
6868
* @throws IllegalArgumentException If {@code commandName} is not the name of a command
6969
*/
7070
public void sendCommandHelp(@NotNull CommandSender sender, @NotNull String commandName) {
71-
if (!commandManager.hasCommand(commandName)) throw new IllegalArgumentException("Command " + commandName + " does not exist");
72-
Command command = commandManager.getCommand(commandName);
71+
Command command = commandManager.getCommand(commandName, false);
72+
if (command == null) throw new IllegalArgumentException("Command " + commandName + " does not exist");
7373
sender.sendMessage(ChatColor.GOLD + "Command " + commandName,
7474
ChatColor.GRAY + " Description: " + ChatColor.WHITE + (command.hasDescription() ? command.getDescription() : "No description provided"),
7575
ChatColor.GRAY + " Aliases: [" + ChatColor.WHITE + String.join(ChatColor.GRAY + ", " + ChatColor.WHITE, command.getAliases()) + ChatColor.GRAY + "]",
@@ -83,8 +83,8 @@ public void sendCommandHelp(@NotNull CommandSender sender, @NotNull String comma
8383
*/
8484
public void sendHelpMenu(@NotNull CommandSender sender) {
8585
sender.sendMessage(ChatColor.GOLD + (options.hasCustomPluginName() ? options.getPluginName() : plugin.getName()) + " Commands");
86-
commandManager.allCommands().keySet().stream().sorted().forEach(name -> {
87-
Command command = commandManager.getCommand(name);
86+
commandManager.getCommandNames(false).stream().sorted().forEach(name -> {
87+
Command command = commandManager.getCommand(name, false);
8888
if (!command.hasPermission() || sender.hasPermission(command.getPermissionString())) sender.sendMessage(ChatColor.YELLOW + " " + name, ChatColor.GRAY + " ↳ " + command.getDescription());
8989
});
9090
}
@@ -98,10 +98,9 @@ public void sendHelpMenu(@NotNull CommandSender sender) {
9898
*/
9999
@NotNull
100100
public List<String> getUsagesFor(String commandName, int spaces) {
101-
if (!commandManager.hasCommand(commandName))
102-
throw new IllegalArgumentException("Command " + commandName + " does not exist");
101+
Command command = commandManager.getCommand(commandName, false);
102+
if (command == null) throw new IllegalArgumentException("Command " + commandName + " does not exist");
103103
List<String> usages = new ArrayList<>();
104-
Command command = commandManager.getCommand(commandName);
105104
command.getUsages().forEach(usage -> {
106105
StringBuilder addedUsage = new StringBuilder();
107106
for (int i = 0; i < spaces; i++) {

src/main/java/com/datasiqn/commandcore/MainCommand.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.datasiqn.commandcore.argument.ListArguments;
44
import com.datasiqn.commandcore.command.Command;
55
import com.datasiqn.commandcore.command.TabComplete;
6+
import com.datasiqn.commandcore.managers.CommandManager;
67
import com.datasiqn.resultapi.None;
78
import com.datasiqn.resultapi.Result;
89
import org.bukkit.ChatColor;
@@ -28,7 +29,8 @@ public MainCommand(CommandCore commandCore) {
2829
@Override
2930
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String @NotNull [] args) {
3031
if (args.length >= 1) {
31-
Command cmd = commandCore.getCommandManager().getCommand(args[0]);
32+
CommandManager manager = commandCore.getCommandManager();
33+
Command cmd = manager.getCommand(args[0], manager.isAlias(args[0]));
3234
if (cmd == null) {
3335
commandCore.sendHelpMenu(sender);
3436
return true;
@@ -43,7 +45,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
4345
output.ifError(messages -> {
4446
for (String message : messages) sender.sendMessage(ChatColor.RED + message);
4547
sender.sendMessage(ChatColor.GRAY + "Usage(s):");
46-
sender.sendMessage(commandCore.getUsagesFor(args[0], 1).toArray(new String[0]));
48+
sender.sendMessage(commandCore.getUsagesFor(cmd.getName(), 1).toArray(new String[0]));
4749
});
4850
return true;
4951
}
@@ -56,12 +58,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
5658
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String @NotNull [] args) {
5759
List<String> tabComplete = new ArrayList<>();
5860
String matchingString = args[args.length - 1];
61+
CommandManager manager = commandCore.getCommandManager();
5962
if (args.length == 1) {
60-
commandCore.getCommandManager().allCommands().forEach((s, cmd) -> {
61-
if (cmd.getPermissionString() == null || sender.hasPermission(cmd.getPermissionString())) tabComplete.add(s);
63+
manager.getCommandNames(true).forEach(name -> {
64+
Command cmd = manager.getCommand(name, manager.isAlias(name));
65+
if (cmd.getPermissionString() == null || sender.hasPermission(cmd.getPermissionString())) tabComplete.add(name);
6266
});
6367
} else {
64-
Command cmd = commandCore.getCommandManager().getCommand(args[0]);
68+
Command cmd = manager.getCommand(args[0], manager.isAlias(args[0]));
6569
if (cmd == null || (cmd.getPermissionString() != null && !sender.hasPermission(cmd.getPermissionString()))) return new ArrayList<>();
6670
List<String> listArgs = new ArrayList<>(Arrays.asList(args));
6771
listArgs.remove(0);

src/main/java/com/datasiqn/commandcore/argument/type/CommandArgumentType.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.datasiqn.commandcore.CommandCore;
44
import com.datasiqn.commandcore.command.Command;
55
import com.datasiqn.commandcore.command.CommandContext;
6+
import com.datasiqn.commandcore.managers.CommandManager;
67
import com.datasiqn.resultapi.None;
78
import com.datasiqn.resultapi.Result;
89
import org.jetbrains.annotations.NotNull;
@@ -18,13 +19,15 @@ class CommandArgumentType implements SimpleArgumentType<Command> {
1819

1920
@Override
2021
public @NotNull Result<Command, None> parseWord(String word) {
21-
return Result.ofNullable(CommandCore.getInstance().getCommandManager().getCommand(word), None.NONE);
22+
return Result.ofNullable(CommandCore.getInstance().getCommandManager().getCommand(word, false), None.NONE);
2223
}
2324

2425
@Override
2526
public @NotNull List<String> getTabComplete(@NotNull CommandContext context) {
2627
List<String> commandNames = new ArrayList<>();
27-
CommandCore.getInstance().getCommandManager().allCommands().forEach((name, command) -> {
28+
CommandManager manager = CommandCore.getInstance().getCommandManager();
29+
manager.getCommandNames(false).forEach(name -> {
30+
Command command = manager.getCommand(name, false);
2831
if (context.getSource().hasPermission(command.getPermissionString())) commandNames.add(name);
2932
});
3033
return commandNames;

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
import com.datasiqn.commandcore.command.Command;
77
import com.datasiqn.commandcore.command.builder.CommandBuilder;
88
import org.jetbrains.annotations.NotNull;
9-
import org.jetbrains.annotations.UnmodifiableView;
109

11-
import java.util.Collections;
1210
import java.util.HashMap;
11+
import java.util.HashSet;
1312
import java.util.Map;
13+
import java.util.Set;
1414

1515
/**
1616
* Class that manages all commands
1717
*/
1818
public class CommandManager {
19-
private final Map<String, Command> executableCommands = new HashMap<>();
2019
private final Map<String, Command> commandMap = new HashMap<>();
20+
private final Map<String, Command> aliasesMap = new HashMap<>();
2121

2222
/**
2323
* Registers a new command
@@ -33,38 +33,50 @@ public void registerCommand(@NotNull CommandBuilder command) {
3333
options.warnIf(Warning.MISSING_DESCRIPTION, !builtCommand.hasDescription(), name);
3434
options.warnIf(Warning.MISSING_PERMISSION, !builtCommand.hasPermission(), name);
3535
commandMap.put(name, builtCommand);
36-
executableCommands.put(name, builtCommand);
3736
for (String alias : builtCommand.getAliases()) {
3837
if (alias.contains(" ")) throw new IllegalArgumentException("Command aliases cannot contain spaces");
3938
if (alias.isEmpty()) throw new IllegalArgumentException("Command aliases cannot be empty");
40-
executableCommands.put(alias, builtCommand);
39+
aliasesMap.put(alias, builtCommand);
4140
}
4241
}
4342

4443
/**
4544
* Gets the command from its name
4645
* @param name The name of the command
46+
* @param alias Whether {@code name} is a command alias or not
4747
* @return The command, or null if it doesn't exist
4848
*/
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);
5151
}
5252

5353
/**
5454
* Checks whether a command with that name exists or not
5555
* @param name The command name
56+
* @param alias Whether {@code name} is a command alias or not
5657
* @return {@code true} if the command exists, otherwise {@code false}
5758
*/
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);
6061
}
6162

6263
/**
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
6567
*/
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;
6981
}
7082
}

0 commit comments

Comments
 (0)