Search in sources :

Example 1 with SpongeExecutorService

use of org.spongepowered.api.scheduler.SpongeExecutorService in project LanternServer by LanternPowered.

the class ConsoleManager method readCommandTask.

/**
 * This task handles the commands that are executed through the console.
 */
private void readCommandTask() {
    final LineReader lineReader = TerminalConsoleAppender.getReader();
    final Supplier<String> consoleReader;
    if (lineReader != null) {
        consoleReader = () -> {
            try {
                return lineReader.readLine("> ");
            } catch (EndOfFileException e) {
                return null;
            }
        };
    } else {
        this.logger.info("Falling back to non jline console.");
        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        consoleReader = () -> {
            try {
                return reader.readLine();
            } catch (IOException e) {
                return null;
            }
        };
    }
    final SpongeExecutorService executor = this.scheduler.createSyncExecutor(this.pluginContainer);
    try {
        String command;
        while (active) {
            command = consoleReader.get();
            if (command != null) {
                command = command.trim();
                if (!command.isEmpty()) {
                    final String runCommand = command.startsWith("/") ? command.substring(1) : command;
                    executor.execute(() -> this.commandManager.process(LanternConsoleSource.INSTANCE, runCommand));
                }
            }
        }
    } catch (UserInterruptException e) {
        // Already set the reader to null, to avoid printing a new line
        TerminalConsoleAppender.setReader(null);
        // When a user interrupts the console, for example Ctrl-C
        // Shutdown the server
        executor.execute(() -> Lantern.getServer().shutdown());
    }
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) InputStreamReader(java.io.InputStreamReader) LineReader(org.jline.reader.LineReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) SpongeExecutorService(org.spongepowered.api.scheduler.SpongeExecutorService) UserInterruptException(org.jline.reader.UserInterruptException)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 EndOfFileException (org.jline.reader.EndOfFileException)1 LineReader (org.jline.reader.LineReader)1 UserInterruptException (org.jline.reader.UserInterruptException)1 SpongeExecutorService (org.spongepowered.api.scheduler.SpongeExecutorService)1