Server restarting/stopping works!

This commit is contained in:
Norbi Peti 2016-08-25 16:11:32 +02:00
parent 761071d941
commit a5f6e943b9

View file

@ -12,40 +12,107 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class ServerRunner { public class ServerRunner {
private static final String SERVER_VERSION = "1.9.2"; private static final String SERVER_VERSION = "1.9.2";
private static boolean stop = false; private static volatile boolean stop = false;
private static volatile int restartcounter = 30;
private static volatile Process serverprocess;
private static volatile PrintWriter output;
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
YamlConfiguration yc = YamlConfiguration.loadConfiguration(new File("thebuttonmc.yml")); System.out.println("Starting server...");
ConfigurationSection cs = yc.getConfigurationSection("players"); serverprocess = startServer();
for (String key : cs.getKeys(false)) { output = new PrintWriter(serverprocess.getOutputStream());
ConfigurationSection cs2 = cs.getConfigurationSection(key); final Thread it = new Thread() {
String uuid = cs2.getString("uuid"); @Override
YamlConfiguration pyc = new YamlConfiguration(); public void run() {
copy(pyc, cs2, "playername"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
copy(pyc, cs2, "username"); try {
copy(pyc, cs2, "flairtime"); while (!stop) {
copy(pyc, cs2, "flairstate"); String readLine = br.readLine();
copy(pyc, cs2, "uuid"); if (readLine.equalsIgnoreCase("restart"))
copy(pyc, cs2, "usernames"); output.println("stop");
copy(pyc, cs2, "fcount"); else {
copy(pyc, cs2, "fdeaths"); if (readLine.equalsIgnoreCase("stop"))
copy(pyc, cs2, "flaircheater"); stop = true;
pyc.save(new File(uuid + ".yml")); output.println(readLine);
System.out.println("Read line: " + readLine);
} // TODO: RunnerStates, stop Input- and OutputThread and restart them after backup
output.flush();
} }
/* } catch (IOException e) {
* System.out.println("Starting server..."); Process p = Runtime.getRuntime().exec(new String[] { "java", "-Xms512M", "-Xmx1024M", "-XX:MaxPermSize=128M", "-jar", "spigot-" + SERVER_VERSION + e.printStackTrace();
* ".jar" }); final Thread it = new Thread() { }
* @Override public void run() { PrintWriter output = new PrintWriter(p.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { while (!stop) { stop = true;
* String readLine = br.readLine(); output.println(readLine); output.flush(); if (readLine.contains("stop")) stop = true; } } catch (IOException e) { e.printStackTrace(); } stop = true; System.out.println("Stopped " + Thread.currentThread().getName());
* System.out.println("Stopped " + Thread.currentThread().getName()); } }; it.setName("InputThread"); it.start(); final Thread ot = new Thread() { }
* @Override public void run() { try { BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null && !stop) { };
* System.out.println(line); } input.close(); } catch (IOException e) { e.printStackTrace(); } stop = true; System.out.println("Stopped " + Thread.currentThread().getName()); } }; it.setName("InputThread");
* ot.setName("OutputThread"); ot.start(); Thread.currentThread().setName("RestarterThread"); while (!stop) { Thread.sleep(10000); System.out.println("RESTART"); } it.start();
* System.out.println("Stopped " + Thread.currentThread().getName()); final Thread ot = new Thread() {
*/ @Override
public void run() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
String line;
while (true) {
if ((line = input.readLine()) != null) {
System.out.println(line);
if (line.contains("FAILED TO BIND TO PORT")) {
stop = true;
System.out.println("A server is already running!");
}
} else if (!stop) {
try {
input.close();
} catch (Exception e) {
}
try {
output.close();
} catch (Exception e) {
}
System.out.println("Server stopped! Restarting...");
serverprocess = startServer();
input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
output = new PrintWriter(serverprocess.getOutputStream());
restartcounter = 30;
} else
break;
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}
stop = true;
System.out.println("Stopped " + Thread.currentThread().getName());
}
}; // TODO: Rename start.sh
ot.setName("OutputThread");
ot.start();
Thread.currentThread().setName("RestarterThread");
while (!stop) {
if (restartcounter >= 0) {
if (restartcounter == 30)
Thread.sleep(10000);
else if (restartcounter > 0) {
sendMessage(output, "red", "-- Server restarting in " + restartcounter + " seconds!");
Thread.sleep(1000); // TODO: Change to bossbar (plugin)
} else {
System.out.println("Stopping server...");
output.println("stop");
output.flush();
}
restartcounter--;
}
}
System.out.println("Stopped " + Thread.currentThread().getName());
} }
private static void copy(YamlConfiguration pyc, ConfigurationSection cs2, String name) { private static Process startServer() throws IOException {
pyc.set(name, cs2.get(name)); return Runtime.getRuntime().exec(new String[] { "java", "-Xms512M", "-Xmx1024M", "-XX:MaxPermSize=128M", "-jar",
"spigot-" + SERVER_VERSION + ".jar" });
}
private static void sendMessage(PrintWriter output, String color, String text) {
output.println("tellraw @a {\"text\":\"" + text + "\",\"color\":\"" + color + "\"}");
System.out.println(text);
} }
} }