diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java index f4fb699f..b0d97a92 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java @@ -2,9 +2,11 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.utils.command.Command; +import ru.gravit.utils.command.CommandCategory; import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.LogHelper; +import java.util.Map; import java.util.Map.Entry; public final class HelpCommand extends ru.gravit.launchserver.command.Command { @@ -13,6 +15,11 @@ private static void printCommand(String name, Command command) { LogHelper.subInfo("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()); } + private static void printCategory(String name) + { + LogHelper.info("Category: %s", name); + } + public HelpCommand(LaunchServer server) { super(server); } @@ -43,7 +50,15 @@ private void printCommand(String name) throws CommandException { } private void printCommands() { - for (Entry entry : server.commandHandler.commandsMap().entrySet()) + for(Map.Entry category : server.commandHandler.getCategories().entrySet()) + { + printCategory(category.getKey()); + for (Entry entry : category.getValue().commandsMap().entrySet()) + printCommand(entry.getKey(), entry.getValue()); + } + printCategory("Base"); + for (Entry entry : server.commandHandler.getBaseCategory().commandsMap().entrySet()) printCommand(entry.getKey(), entry.getValue()); + } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/BaseCommandCategory.java b/libLauncher/src/main/java/ru/gravit/utils/command/BaseCommandCategory.java new file mode 100644 index 00000000..c501f926 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/command/BaseCommandCategory.java @@ -0,0 +1,32 @@ +package ru.gravit.utils.command; + +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +public class BaseCommandCategory implements CommandCategory { + private final Map commands = new ConcurrentHashMap<>(32); + @Override + public void registerCommand(String name, Command command) { + VerifyHelper.verifyIDName(name); + VerifyHelper.putIfAbsent(commands, name.toLowerCase(), Objects.requireNonNull(command, "command"), + String.format("Command has been already registered: '%s'", name.toLowerCase())); + } + + @Override + public Command unregisterCommand(String name) { + return commands.remove(name); + } + + @Override + public Command findCommand(String name) { + return commands.get(name); + } + + @Override + public Map commandsMap() { + return commands; + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/CommandCategory.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandCategory.java new file mode 100644 index 00000000..4566493c --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandCategory.java @@ -0,0 +1,10 @@ +package ru.gravit.utils.command; + +import java.util.Map; + +public interface CommandCategory { + void registerCommand(String name, Command command); + Command unregisterCommand(String name); + Command findCommand(String name); + Map commandsMap(); +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java index 30d961a1..b369e994 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java @@ -12,7 +12,8 @@ import java.util.concurrent.ConcurrentHashMap; public abstract class CommandHandler implements Runnable { - private final Map commands = new ConcurrentHashMap<>(32); + private final Map categories = new ConcurrentHashMap<>(8); + private final CommandCategory baseCategory = new BaseCommandCategory(); public void eval(String line, boolean bell) { LogHelper.info("Command '%s'", line); @@ -56,11 +57,24 @@ public void eval(String[] args, boolean bell) { public Command lookup(String name) throws CommandException { - Command command = commands.get(name); + Command command = findCommand(name); if (command == null) throw new CommandException(String.format("Unknown command: '%s'", name)); return command; } + public Command findCommand(String name) + { + Command cmd = baseCategory.findCommand(name); + if(cmd == null) + { + for(Map.Entry entry : categories.entrySet()) + { + cmd = entry.getValue().findCommand(name); + if(cmd != null) return cmd; + } + } + return cmd; + } public abstract String readLine() throws IOException; @@ -72,13 +86,11 @@ private void readLoop() throws IOException { public void registerCommand(String name, Command command) { - VerifyHelper.verifyIDName(name); - VerifyHelper.putIfAbsent(commands, name.toLowerCase(), Objects.requireNonNull(command, "command"), - String.format("Command has been already registered: '%s'", name.toLowerCase())); + baseCategory.registerCommand(name, command); } public Command unregisterCommand(String name) { - return commands.remove(name); + return baseCategory.unregisterCommand(name); } @Override @@ -90,16 +102,16 @@ public void run() { } } + public CommandCategory getBaseCategory() { + return baseCategory; + } + + public Map getCategories() { + return categories; + } public abstract void bell() throws IOException; public abstract void clear() throws IOException; - - - public Map commandsMap() { - return Collections.unmodifiableMap(commands); - } - - }