Using JLine for prompt
This commit is contained in:
parent
176ccf92a9
commit
8270c7396d
3 changed files with 64 additions and 36 deletions
5
pom.xml
5
pom.xml
|
@ -18,5 +18,10 @@
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jline</groupId>
|
||||||
|
<artifactId>jline</artifactId>
|
||||||
|
<version>2.12</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -1,7 +0,0 @@
|
||||||
package buttondevteam.serverrunner;
|
|
||||||
|
|
||||||
import org.bukkit.craftbukkit.Main;
|
|
||||||
|
|
||||||
public class ServerMain extends Main {
|
|
||||||
|
|
||||||
}
|
|
|
@ -6,8 +6,8 @@ import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import jline.console.ConsoleReader;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import jline.console.CursorBuffer;
|
||||||
|
|
||||||
public class ServerRunner {
|
public class ServerRunner {
|
||||||
private static final int RESTART_MESSAGE_COUNT = 30;
|
private static final int RESTART_MESSAGE_COUNT = 30;
|
||||||
|
@ -17,8 +17,10 @@ public class ServerRunner {
|
||||||
private static volatile boolean stop = false;
|
private static volatile boolean stop = false;
|
||||||
private static volatile int restartcounter = RESTART_MESSAGE_COUNT;
|
private static volatile int restartcounter = RESTART_MESSAGE_COUNT;
|
||||||
private static volatile Process serverprocess;
|
private static volatile Process serverprocess;
|
||||||
private static volatile PrintWriter output;
|
private static volatile PrintWriter serveroutput;
|
||||||
private static volatile Thread rt;
|
private static volatile Thread rt;
|
||||||
|
private static volatile ConsoleReader reader;
|
||||||
|
private static volatile PrintWriter runnerout;
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, InterruptedException {
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||||||
String minmem = "512M";
|
String minmem = "512M";
|
||||||
|
@ -45,32 +47,33 @@ public class ServerRunner {
|
||||||
}
|
}
|
||||||
final String fminmem = minmem;
|
final String fminmem = minmem;
|
||||||
final String fmaxmem = maxmem;
|
final String fmaxmem = maxmem;
|
||||||
System.out.println("Starting server...");
|
reader = new ConsoleReader();
|
||||||
|
reader.setPrompt("Runner>");
|
||||||
|
runnerout = new PrintWriter(reader.getOutput());
|
||||||
|
writeToScreen("Starting server...");
|
||||||
serverprocess = startServer(minmem, maxmem);
|
serverprocess = startServer(minmem, maxmem);
|
||||||
output = new PrintWriter(serverprocess.getOutputStream());
|
serveroutput = new PrintWriter(serverprocess.getOutputStream());
|
||||||
rt = Thread.currentThread();
|
rt = Thread.currentThread();
|
||||||
final Thread it = new Thread() {
|
final Thread it = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
||||||
try {
|
try {
|
||||||
while (!stop) {
|
String readLine;
|
||||||
String readLine = br.readLine();
|
while (!stop && (readLine = reader.readLine()) != null) {
|
||||||
/*
|
/*
|
||||||
* if (readLine.equalsIgnoreCase("restart")) output.println("stop"); else {
|
* if (readLine.equalsIgnoreCase("restart")) output.println("stop"); else {
|
||||||
*/
|
*/
|
||||||
if (readLine.equalsIgnoreCase("stop"))
|
if (readLine.equalsIgnoreCase("stop"))
|
||||||
ServerRunner.stop();
|
ServerRunner.stop();
|
||||||
output.println(readLine);
|
serveroutput.println(readLine);
|
||||||
System.out.println("Read line: " + readLine);
|
|
||||||
// } // TODO: RunnerStates, stop Input- and OutputThread and restart them after backup?
|
// } // TODO: RunnerStates, stop Input- and OutputThread and restart them after backup?
|
||||||
output.flush();
|
serveroutput.flush();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
ServerRunner.stop();
|
ServerRunner.stop();
|
||||||
System.out.println("Stopped " + Thread.currentThread().getName());
|
writeToScreen("Stopped " + Thread.currentThread().getName());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
it.setName("InputThread");
|
it.setName("InputThread");
|
||||||
|
@ -79,38 +82,39 @@ public class ServerRunner {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
BufferedReader input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
|
BufferedReader serverinput = new BufferedReader(
|
||||||
|
new InputStreamReader(serverprocess.getInputStream()));
|
||||||
String line;
|
String line;
|
||||||
while (true) {
|
while (true) {
|
||||||
if ((line = input.readLine()) != null) {
|
if ((line = serverinput.readLine()) != null) {
|
||||||
System.out.println(line);
|
writeToScreen(line);
|
||||||
if (line.contains("FAILED TO BIND TO PORT")) {
|
if (line.contains("FAILED TO BIND TO PORT")) {
|
||||||
ServerRunner.stop();
|
ServerRunner.stop();
|
||||||
System.out.println("A server is already running!");
|
writeToScreen("A server is already running!");
|
||||||
}
|
}
|
||||||
} else if (!stop) {
|
} else if (!stop) {
|
||||||
try {
|
try {
|
||||||
input.close();
|
serverinput.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
output.close();
|
serveroutput.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
System.out.println("Server stopped! Restarting...");
|
writeToScreen("Server stopped! Restarting...");
|
||||||
serverprocess = startServer(fminmem, fmaxmem);
|
serverprocess = startServer(fminmem, fmaxmem);
|
||||||
input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
|
serverinput = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
|
||||||
output = new PrintWriter(serverprocess.getOutputStream());
|
serveroutput = new PrintWriter(serverprocess.getOutputStream());
|
||||||
restartcounter = RESTART_MESSAGE_COUNT;
|
restartcounter = RESTART_MESSAGE_COUNT;
|
||||||
} else
|
} else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
input.close();
|
serverinput.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
ServerRunner.stop();
|
ServerRunner.stop();
|
||||||
System.out.println("Stopped " + Thread.currentThread().getName());
|
writeToScreen("Stopped " + Thread.currentThread().getName());
|
||||||
}
|
}
|
||||||
}; // TODO: Rename start.sh and put empty one
|
}; // TODO: Rename start.sh and put empty one
|
||||||
ot.setName("OutputThread");
|
ot.setName("OutputThread");
|
||||||
|
@ -123,19 +127,19 @@ public class ServerRunner {
|
||||||
Thread.sleep(24 * 60 * 60 * 1000);
|
Thread.sleep(24 * 60 * 60 * 1000);
|
||||||
// Thread.sleep(10000);
|
// Thread.sleep(10000);
|
||||||
else if (restartcounter > 0) {
|
else if (restartcounter > 0) {
|
||||||
sendMessage(output, "red", "-- Server restarting in " + restartcounter + " seconds!");
|
sendMessage(serveroutput, "red", "-- Server restarting in " + restartcounter + " seconds!");
|
||||||
Thread.sleep(1000); // TODO: Change to bossbar? (plugin)
|
Thread.sleep(1000); // TODO: Change to bossbar? (plugin)
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Stopping server for restart...");
|
writeToScreen("Stopping server for restart...");
|
||||||
output.println("restart");
|
serveroutput.println("restart");
|
||||||
output.flush();
|
serveroutput.flush();
|
||||||
}
|
}
|
||||||
restartcounter--;
|
restartcounter--;
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) { // The while checks if stop is true and then stops
|
} catch (InterruptedException e) { // The while checks if stop is true and then stops
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Stopped " + Thread.currentThread().getName());
|
writeToScreen("Stopped " + Thread.currentThread().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Process startServer(String minmem, String maxmem) throws IOException {
|
private static Process startServer(String minmem, String maxmem) throws IOException {
|
||||||
|
@ -146,11 +150,37 @@ public class ServerRunner {
|
||||||
private static void sendMessage(PrintWriter output, String color, String text) {
|
private static void sendMessage(PrintWriter output, String color, String text) {
|
||||||
output.println("tellraw @a {\"text\":\"" + text + "\",\"color\":\"" + color + "\"}");
|
output.println("tellraw @a {\"text\":\"" + text + "\",\"color\":\"" + color + "\"}");
|
||||||
output.flush();
|
output.flush();
|
||||||
System.out.println(text);
|
writeToScreen(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void stop() {
|
private static void stop() {
|
||||||
stop = true;
|
stop = true;
|
||||||
rt.interrupt(); // The restarter thread sleeps for a long time and keeps the program running
|
rt.interrupt(); // The restarter thread sleeps for a long time and keeps the program running
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void writeToScreen(String line) {
|
||||||
|
stashLine();
|
||||||
|
runnerout.println(line);
|
||||||
|
unstashLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CursorBuffer stashed;
|
||||||
|
|
||||||
|
private static void stashLine() {
|
||||||
|
stashed = reader.getCursorBuffer().copy();
|
||||||
|
try {
|
||||||
|
reader.getOutput().write("\u001b[1G\u001b[K");
|
||||||
|
reader.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void unstashLine() {
|
||||||
|
try {
|
||||||
|
reader.resetPromptLine(reader.getPrompt(), stashed.toString(), stashed.cursor);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue