реализация CommandService
This commit is contained in:
@@ -1,28 +1,22 @@
|
|||||||
package mc.server;
|
package mc.server;
|
||||||
|
|
||||||
import mc.server.shell.CommandStore;
|
import mc.server.shell.CommandService;
|
||||||
import org.jline.builtins.Builtins;
|
import mc.server.shell.CommandServiceImpl;
|
||||||
|
import mc.server.shell.SystemPropertiesCommand;
|
||||||
import org.jline.builtins.CommandRegistry;
|
import org.jline.builtins.CommandRegistry;
|
||||||
import org.jline.reader.*;
|
import org.jline.reader.*;
|
||||||
import org.jline.reader.impl.completer.NullCompleter;
|
|
||||||
|
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CommandStore commandStore = new CommandStore();
|
System.out.println("mc-server");
|
||||||
commandStore.register("system-properties", new Builtins.CommandMethods(
|
|
||||||
commandInput -> {
|
|
||||||
final PrintWriter writer = commandInput.terminal().writer();
|
|
||||||
System.getProperties().forEach((key, value) ->
|
|
||||||
writer.println(String.format("%s = %s", key, value.toString())));
|
|
||||||
},
|
|
||||||
commandName -> Collections.singletonList(NullCompleter.INSTANCE)
|
|
||||||
));
|
|
||||||
|
|
||||||
final Completer completer = CommandRegistry.compileCompleters(commandStore);
|
CommandService commandService = new CommandServiceImpl();
|
||||||
|
commandService.register("system-properties", new SystemPropertiesCommand());
|
||||||
|
|
||||||
|
final Completer completer = CommandRegistry.compileCompleters(commandService);
|
||||||
|
|
||||||
final LineReader reader = LineReaderBuilder.builder()
|
final LineReader reader = LineReaderBuilder.builder()
|
||||||
.completer(completer)
|
.completer(completer)
|
||||||
@@ -38,8 +32,8 @@ public class Main {
|
|||||||
final ParsedLine parsedLine = reader.getParser().parse(line, 0);
|
final ParsedLine parsedLine = reader.getParser().parse(line, 0);
|
||||||
final String commandName = parsedLine.word();
|
final String commandName = parsedLine.word();
|
||||||
|
|
||||||
if (commandStore.hasCommand(commandName)) {
|
if (commandService.hasCommand(commandName)) {
|
||||||
commandStore.execute(session, commandName);
|
commandService.execute(session, commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
terminalWriter.println("line = " + String.join(", ", parsedLine.words()));
|
terminalWriter.println("line = " + String.join(", ", parsedLine.words()));
|
||||||
|
|||||||
8
src/main/java/mc/server/shell/Command.java
Normal file
8
src/main/java/mc/server/shell/Command.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package mc.server.shell;
|
||||||
|
|
||||||
|
import org.jline.builtins.Builtins;
|
||||||
|
|
||||||
|
public interface Command {
|
||||||
|
|
||||||
|
void execute(Builtins.CommandInput commandInput);
|
||||||
|
}
|
||||||
10
src/main/java/mc/server/shell/CommandService.java
Normal file
10
src/main/java/mc/server/shell/CommandService.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package mc.server.shell;
|
||||||
|
|
||||||
|
import org.jline.builtins.CommandRegistry;
|
||||||
|
|
||||||
|
public interface CommandService extends CommandRegistry {
|
||||||
|
|
||||||
|
void register(String commandName, Command command);
|
||||||
|
|
||||||
|
void execute(CommandSession session, String commandName);
|
||||||
|
}
|
||||||
51
src/main/java/mc/server/shell/CommandServiceImpl.java
Normal file
51
src/main/java/mc/server/shell/CommandServiceImpl.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package mc.server.shell;
|
|
||||||
|
|
||||||
import org.jline.builtins.Builtins.CommandInput;
|
|
||||||
import org.jline.builtins.Builtins.CommandMethods;
|
|
||||||
import org.jline.builtins.CommandRegistry;
|
|
||||||
import org.jline.builtins.Completers.SystemCompleter;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class CommandStore implements CommandRegistry {
|
|
||||||
|
|
||||||
private final Map<String, CommandMethods> commandExecutors = new HashMap<>();
|
|
||||||
|
|
||||||
public void register(String name, CommandMethods commandMethods) {
|
|
||||||
commandExecutors.put(name, commandMethods);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void execute(CommandSession session, String command) {
|
|
||||||
commandExecutors.get(command).execute().accept(new CommandInput(command, new Object[0], session));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<String> commandNames() {
|
|
||||||
return commandExecutors.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, String> commandAliases() {
|
|
||||||
return Collections.emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasCommand(String command) {
|
|
||||||
return commandExecutors.containsKey(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SystemCompleter compileCompleters() {
|
|
||||||
SystemCompleter systemCompleter = new SystemCompleter();
|
|
||||||
|
|
||||||
commandExecutors.forEach((name, commandMethods) ->
|
|
||||||
systemCompleter.add(name, commandMethods.compileCompleter().apply(name)));
|
|
||||||
systemCompleter.addAliases(commandAliases());
|
|
||||||
|
|
||||||
return systemCompleter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
15
src/main/java/mc/server/shell/SystemPropertiesCommand.java
Normal file
15
src/main/java/mc/server/shell/SystemPropertiesCommand.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package mc.server.shell;
|
||||||
|
|
||||||
|
import org.jline.builtins.Builtins;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
public class SystemPropertiesCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Builtins.CommandInput commandInput) {
|
||||||
|
final PrintWriter writer = commandInput.terminal().writer();
|
||||||
|
System.getProperties().forEach((key, value) ->
|
||||||
|
writer.println(String.format("%s = %s", key, value.toString())));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user