- 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;
public class ServerRunner {
private static final int RESTART_MESSAGE_COUNT = 30;
private static final String SERVER_VERSION = "1.9.2";
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 PrintWriter output;
private static volatile Thread rt;
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Starting server...");
serverprocess = startServer();
output = new PrintWriter(serverprocess.getOutputStream());
rt = Thread.currentThread();
final Thread it = new Thread() {
@Override
public void run() {
@ -28,20 +32,20 @@ public class ServerRunner {
try {
while (!stop) {
String readLine = br.readLine();
if (readLine.equalsIgnoreCase("restart"))
output.println("stop");
else {
if (readLine.equalsIgnoreCase("stop"))
stop = true;
output.println(readLine);
System.out.println("Read line: " + readLine);
} // TODO: RunnerStates, stop Input- and OutputThread and restart them after backup
/*
* if (readLine.equalsIgnoreCase("restart")) output.println("stop"); else {
*/
if (readLine.equalsIgnoreCase("stop"))
ServerRunner.stop();
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) {
e.printStackTrace();
}
stop = true;
ServerRunner.stop();
System.out.println("Stopped " + Thread.currentThread().getName());
}
};
@ -57,7 +61,7 @@ public class ServerRunner {
if ((line = input.readLine()) != null) {
System.out.println(line);
if (line.contains("FAILED TO BIND TO PORT")) {
stop = true;
ServerRunner.stop();
System.out.println("A server is already running!");
}
} else if (!stop) {
@ -73,7 +77,7 @@ public class ServerRunner {
serverprocess = startServer();
input = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
output = new PrintWriter(serverprocess.getOutputStream());
restartcounter = 30;
restartcounter = RESTART_MESSAGE_COUNT;
} else
break;
}
@ -81,26 +85,30 @@ public class ServerRunner {
} catch (IOException e) {
e.printStackTrace();
}
stop = true;
ServerRunner.stop();
System.out.println("Stopped " + Thread.currentThread().getName());
}
}; // TODO: Rename start.sh
}; // TODO: Rename start.sh and put empty one
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();
try {
if (restartcounter >= 0) {
if (restartcounter == RESTART_MESSAGE_COUNT)
Thread.sleep(24 * 60 * 60 * 1000);
// 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 for restart...");
output.println("restart");
output.flush();
}
restartcounter--;
}
restartcounter--;
} catch (InterruptedException e) { // The while checks if stop is true and then stops
}
}
System.out.println("Stopped " + Thread.currentThread().getName());
@ -113,6 +121,12 @@ public class ServerRunner {
private static void sendMessage(PrintWriter output, String color, String text) {
output.println("tellraw @a {\"text\":\"" + text + "\",\"color\":\"" + color + "\"}");
output.flush();
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
}
}