[FEATURE] Категории комманд

This commit is contained in:
Gravit 2019-04-13 05:26:39 +07:00
parent c3fa410964
commit 6d03a3bcaf
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 83 additions and 14 deletions

View file

@ -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<String, Command> entry : server.commandHandler.commandsMap().entrySet())
for(Map.Entry<String, CommandCategory> category : server.commandHandler.getCategories().entrySet())
{
printCategory(category.getKey());
for (Entry<String, Command> entry : category.getValue().commandsMap().entrySet())
printCommand(entry.getKey(), entry.getValue());
}
printCategory("Base");
for (Entry<String, Command> entry : server.commandHandler.getBaseCategory().commandsMap().entrySet())
printCommand(entry.getKey(), entry.getValue());
}
}

View file

@ -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<String, Command> 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<String, Command> commandsMap() {
return commands;
}
}

View file

@ -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<String, Command> commandsMap();
}

View file

@ -12,7 +12,8 @@
import java.util.concurrent.ConcurrentHashMap;
public abstract class CommandHandler implements Runnable {
private final Map<String, Command> commands = new ConcurrentHashMap<>(32);
private final Map<String, CommandCategory> 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<String, CommandCategory> 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<String, CommandCategory> getCategories() {
return categories;
}
public abstract void bell() throws IOException;
public abstract void clear() throws IOException;
public Map<String, Command> commandsMap() {
return Collections.unmodifiableMap(commands);
}
}