реализация CommandService
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user