Archived
0

реализация CommandService

This commit is contained in:
2020-05-06 13:37:14 +03:00
parent b1efb13b05
commit fba59bb009
6 changed files with 94 additions and 66 deletions

View File

@@ -0,0 +1,51 @@
package mc.server.shell;
import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CommandServiceImpl implements CommandService {
private final Map<String, CommandMethods> commands = new HashMap<>();
public void register(String commandName, Command command) {
commands.put(commandName, new CommandMethods(
command::execute,
cmdName -> Collections.singletonList(NullCompleter.INSTANCE)
));
}
public void execute(CommandSession session, String commandName) {
commands.get(commandName).execute().accept(new Builtins.CommandInput(commandName, new Object[0], session));
}
@Override
public Set<String> commandNames() {
return commands.keySet();
}
@Override
public Map<String, String> commandAliases() {
return Collections.emptyMap();
}
@Override
public boolean hasCommand(String commandName) {
return commands.containsKey(commandName);
}
@Override
public SystemCompleter compileCompleters() {
final SystemCompleter systemCompleter = new SystemCompleter();
systemCompleter.addAliases(commandAliases());
commands.forEach((commandName, command) -> systemCompleter.add(commandName, NullCompleter.INSTANCE));
return systemCompleter;
}
}