Server restarting/stopping works!
This commit is contained in:
parent
761071d941
commit
a5f6e943b9
1 changed files with 97 additions and 30 deletions
|
@ -12,40 +12,107 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||
public class ServerRunner {
|
||||
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 {
|
||||
YamlConfiguration yc = YamlConfiguration.loadConfiguration(new File("thebuttonmc.yml"));
|
||||
ConfigurationSection cs = yc.getConfigurationSection("players");
|
||||
for (String key : cs.getKeys(false)) {
|
||||
ConfigurationSection cs2 = cs.getConfigurationSection(key);
|
||||
String uuid = cs2.getString("uuid");
|
||||
YamlConfiguration pyc = new YamlConfiguration();
|
||||
copy(pyc, cs2, "playername");
|
||||
copy(pyc, cs2, "username");
|
||||
copy(pyc, cs2, "flairtime");
|
||||
copy(pyc, cs2, "flairstate");
|
||||
copy(pyc, cs2, "uuid");
|
||||
copy(pyc, cs2, "usernames");
|
||||
copy(pyc, cs2, "fcount");
|
||||
copy(pyc, cs2, "fdeaths");
|
||||
copy(pyc, cs2, "flaircheater");
|
||||
pyc.save(new File(uuid + ".yml"));
|
||||
System.out.println("Starting server...");
|
||||
serverprocess = startServer();
|
||||
output = new PrintWriter(serverprocess.getOutputStream());
|
||||
final Thread it = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
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
|
||||
output.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
stop = true;
|
||||
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(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("Starting server..."); Process p = Runtime.getRuntime().exec(new String[] { "java", "-Xms512M", "-Xmx1024M", "-XX:MaxPermSize=128M", "-jar", "spigot-" + SERVER_VERSION +
|
||||
* ".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) {
|
||||
* 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()); } }; 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()); } };
|
||||
* ot.setName("OutputThread"); ot.start(); Thread.currentThread().setName("RestarterThread"); while (!stop) { Thread.sleep(10000); System.out.println("RESTART"); }
|
||||
* System.out.println("Stopped " + Thread.currentThread().getName());
|
||||
*/
|
||||
System.out.println("Stopped " + Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
private static void copy(YamlConfiguration pyc, ConfigurationSection cs2, String name) {
|
||||
pyc.set(name, cs2.get(name));
|
||||
private static Process startServer() throws IOException {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue