Archived
0
This repository has been archived on 2022-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mc-server/src/main/java/mc/server/shell/CommandServiceImpl.java

52 lines
1.5 KiB
Java

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;
}
}