- Fixed player message sending
- Fixed RestarterThread not stopping instantly
This commit is contained in:
Norbi Peti 2016-08-26 17:59:36 +02:00
parent a5f6e943b9
commit 10d5ff8c28

View file

@ -10,17 +10,21 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
public class ServerRunner { public class ServerRunner {
private static final int RESTART_MESSAGE_COUNT = 30;
private static final String SERVER_VERSION = "1.9.2"; private static final String SERVER_VERSION = "1.9.2";
private static volatile boolean stop = false; private static volatile boolean stop = false;
private static volatile int restartcounter = 30; 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 output;
private static volatile Thread rt;
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Starting server..."); System.out.println("Starting server...");
serverprocess = startServer(); serverprocess = startServer();
output = new PrintWriter(serverprocess.getOutputStream()); output = new PrintWriter(serverprocess.getOutputStream());
rt = Thread.currentThread();
final Thread it = new Thread() { final Thread it = new Thread() {
@Override @Override
public void run() { public void run() {
@ -28,20 +32,20 @@ public class ServerRunner {
try { try {
while (!stop) { while (!stop) {
String readLine = br.readLine(); String readLine = br.readLine();
if (readLine.equalsIgnoreCase("restart")) /*
output.println("stop"); * if (readLine.equalsIgnoreCase("restart")) output.println("stop"); else {
else { */
if (readLine.equalsIgnoreCase("stop")) if (readLine.equalsIgnoreCase("stop"))
stop = true; ServerRunner.stop();
output.println(readLine); output.println(readLine);
System.out.println("Read line: " + 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(); output.flush();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
stop = true; ServerRunner.stop();
System.out.println("Stopped " + Thread.currentThread().getName()); System.out.println("Stopped " + Thread.currentThread().getName());
} }
}; };
@ -57,7 +61,7 @@ public class ServerRunner {
if ((line = input.readLine()) != null) { if ((line = input.readLine()) != null) {
System.out.println(line); System.out.println(line);
if (line.contains("FAILED TO BIND TO PORT")) { if (line.contains("FAILED TO BIND TO PORT")) {
stop = true; ServerRunner.stop();
System.out.println("A server is already running!"); System.out.println("A server is already running!");
} }
} else if (!stop) { } else if (!stop) {
@ -73,7 +77,7 @@ public class ServerRunner {
serverprocess = startServer(); serverprocess = startServer();
input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream())); input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
output = new PrintWriter(serverprocess.getOutputStream()); output = new PrintWriter(serverprocess.getOutputStream());
restartcounter = 30; restartcounter = RESTART_MESSAGE_COUNT;
} else } else
break; break;
} }
@ -81,27 +85,31 @@ public class ServerRunner {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
stop = true; ServerRunner.stop();
System.out.println("Stopped " + Thread.currentThread().getName()); System.out.println("Stopped " + Thread.currentThread().getName());
} }
}; // TODO: Rename start.sh }; // TODO: Rename start.sh and put empty one
ot.setName("OutputThread"); ot.setName("OutputThread");
ot.start(); ot.start();
Thread.currentThread().setName("RestarterThread"); Thread.currentThread().setName("RestarterThread");
while (!stop) { while (!stop) {
try {
if (restartcounter >= 0) { if (restartcounter >= 0) {
if (restartcounter == 30) if (restartcounter == RESTART_MESSAGE_COUNT)
Thread.sleep(10000); Thread.sleep(24 * 60 * 60 * 1000);
// Thread.sleep(10000);
else if (restartcounter > 0) { else if (restartcounter > 0) {
sendMessage(output, "red", "-- Server restarting in " + restartcounter + " seconds!"); sendMessage(output, "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..."); System.out.println("Stopping server for restart...");
output.println("stop"); output.println("restart");
output.flush(); output.flush();
} }
restartcounter--; restartcounter--;
} }
} catch (InterruptedException e) { // The while checks if stop is true and then stops
}
} }
System.out.println("Stopped " + Thread.currentThread().getName()); System.out.println("Stopped " + Thread.currentThread().getName());
} }
@ -113,6 +121,12 @@ 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();
System.out.println(text); System.out.println(text);
} }
private static void stop() {
stop = true;
rt.interrupt(); // The restarter thread sleeps for a long time and keeps the program running
}
} }