-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathQuickExitCliHandler.java
89 lines (76 loc) · 2.98 KB
/
QuickExitCliHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package me.earth.headlessmc.launcher;
import lombok.CustomLog;
import lombok.experimental.UtilityClass;
import lombok.val;
import me.earth.headlessmc.command.line.Listener;
/**
* @see me.earth.headlessmc.api.QuickExitCli
*/
@CustomLog
@UtilityClass
public class QuickExitCliHandler {
/**
* Checks if the args contain a "--command". If they do the rest of the args
* will get collected into a command. If the args contain no "--command",
* the command is empty or "cli" {@code false} will be returned. Otherwise
* {@code true} will be returned, the given {@link Launcher}s command
* context will be run on the given command and {@link
* Launcher#setQuickExitCli(boolean)} will be set to
* {@code true}. If a command needs more input it can set
* {@link Launcher#setWaitingForInput(boolean)} to {@code true} which will
* cause this method call the given {@link Listener}. An exception are the
* args "--version", "-version" or "version". These will also return
* {@code true} and print {@link Launcher#VERSION}.
*
* @param launcher the launcher.
* @param in the Listener to call when a command waits for input.
* @param args the arguments to check.
* @return {@code true} if the launcher shouldn't listen to more commands.
*/
public static boolean checkQuickExit(Launcher launcher, Listener in,
String... args) {
val cmd = collectArgs(launcher, args);
if (cmd != null) {
if (cmd.isEmpty() || "cli".equalsIgnoreCase(cmd)) {
return false;
}
if (isVersion(cmd)) {
launcher.log("HeadlessMc - " + Launcher.VERSION);
return true;
}
launcher.setQuickExitCli(true);
launcher.getCommandContext().execute(cmd);
if (launcher.isWaitingForInput()) {
log.debug("Waiting for more input...");
in.listen(launcher);
}
log.debug("Exiting QuickExitCli");
}
return cmd != null;
}
private static String collectArgs(Launcher launcher, String... args) {
boolean quickExitCli = false;
val cmd = new StringBuilder();
for (val arg : args) {
if (arg == null) {
continue;
}
if (!quickExitCli && isVersion(arg)) {
launcher.log("HeadlessMc - " + Launcher.VERSION);
return "--version";
}
if (quickExitCli) {
cmd.append(arg).append(" ");
}
if (arg.equalsIgnoreCase("--command")) {
quickExitCli = true;
}
}
return quickExitCli ? cmd.toString().trim() : null;
}
private static boolean isVersion(String string) {
return string.equalsIgnoreCase("--version")
|| string.equalsIgnoreCase("-version")
|| string.equalsIgnoreCase("version");
}
}