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 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 commandNames() { return commands.keySet(); } @Override public Map 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; } }