From 2ff3c765a6789b3bb15063e875ca5b56d3aefd18 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Tue, 20 Feb 2018 22:13:57 +0100 Subject: [PATCH] Improved checking for abnormally closed servers --- src/net/knarcraft/serverlauncher/Main.java | 16 +++++++ .../serverlauncher/server/Server.java | 21 +++++++++ .../serverlauncher/userinterface/GUI.java | 43 +++++++++++++------ 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/net/knarcraft/serverlauncher/Main.java b/src/net/knarcraft/serverlauncher/Main.java index 0eea466..fb15930 100644 --- a/src/net/knarcraft/serverlauncher/Main.java +++ b/src/net/knarcraft/serverlauncher/Main.java @@ -83,6 +83,22 @@ public class Main { } } } + if (!serversRunning()) { + Profile.getGUI().stopped(); + Profile.getGUI().setStatus("Servers are stopped"); + } else { + Profile.getGUI().running(); + } + } + + private static boolean serversRunning() { + int num = 0; + for (Collection collection : Profile.getCurrent().getCollections()) { + if (collection.getServer().isStarted() || (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) { + num++; + } + } + return num > 0; } /** diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index a385668..dee0ffc 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -41,6 +41,7 @@ public class Server { private String snapshotVersion; private String spongeVanillaVersion; private String bungeeVersion; + private boolean started; public Server(String name) { this.name = name; @@ -88,6 +89,10 @@ public class Server { return this.name; } + public boolean isStarted() { + return started; + } + public String getTypeName() { return this.type.getName(); } @@ -136,6 +141,7 @@ public class Server { process = null; writer = null; reader = null; + started = false; } /** @@ -228,6 +234,7 @@ public class Server { } server.writer.flush(); server.writer = null; + server.started = false; } } } @@ -240,6 +247,13 @@ public class Server { for (Collection collection : Profile.getCurrent().getCollections()) { if (!collection.getServer().run()) { Profile.getGUI().setStatus("An error occurred. Start aborted"); + try { + Server.stop(); + } catch (IOException e) { + e.printStackTrace(); + } + Profile.getGUI().stopped(); + return; } } } @@ -249,6 +263,7 @@ public class Server { */ private boolean run() { if (this.enabled) { + this.started = true; if (!Profile.getCurrent().getDownloadJars()) { try { Profile.getGUI().setStatus("Downloading jar..."); @@ -257,6 +272,7 @@ public class Server { } catch (FileNotFoundException e) { Profile.getGUI().setStatus("Error: Jar file not found"); e.printStackTrace(); + this.started = false; return false; } } @@ -265,6 +281,8 @@ public class Server { TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup()); } catch (InterruptedException e) { e.printStackTrace(); + this.started = false; + return false; } try { ProcessBuilder builder; @@ -299,12 +317,15 @@ public class Server { InputStream stdout = this.process.getInputStream(); this.reader = new BufferedReader (new InputStreamReader(stdout)); Profile.getGUI().setStatus("Servers are running"); + this.started = true; return true; } catch (IOException e) { Profile.getGUI().setStatus("Could not start server"); + this.started = false; return false; } } else { + this.started = false; return true; } } diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index 268032c..c06a8a3 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -409,6 +409,7 @@ public class GUI implements ActionListener { trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup); trayIcon.setImageAutoSize(true); ActionListener exitListener= e -> { + stop(); try { Profile.getCurrent().save(); } catch (FileNotFoundException e1) { @@ -445,12 +446,12 @@ public class GUI implements ActionListener { e1.printStackTrace(); } } else { + stop(); try { Profile.getCurrent().save(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } - stop(); System.exit(0); } } @@ -509,8 +510,6 @@ public class GUI implements ActionListener { background(); } else if (e.getSource() == chckbxmntmDelayStartup) { delay(); - } else if (e.getSource() == targetServer) { - updatePlayers(); } else if (e.getSource() == chckbxmntmDownloadJars) { downloadJars(); } else if (e.getSource() == mntmErrors) { @@ -555,12 +554,12 @@ public class GUI implements ActionListener { } else if (e.getSource() == mntmStory) { goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); } else if (e.getSource() == btnStartServer) { - try { - Profile.getCurrent().save(); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } - Executors.newSingleThreadExecutor().execute(Server::startServers); + try { + Profile.getCurrent().save(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } + Executors.newSingleThreadExecutor().execute(Server::startServers); } else if (e.getSource() == btnStopServer) { stop(); } else if (e.getSource() == addServer) { @@ -580,11 +579,11 @@ public class GUI implements ActionListener { updateProfiles(); } } else if (e.getSource() == profiles) { - try { - changeProfile(); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } + try { + changeProfile(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } } else if (e.getSource() == btnKick) { if (selectedServerValue != null && selectedPlayerValue != null) { Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue); @@ -616,9 +615,25 @@ public class GUI implements ActionListener { } } else if (e.getSource() == btnServerConsoles) { ServerConsoles.show(); + } else if (e.getSource() == targetServer) { + updatePlayers(); } } + public void running() { + profiles.setEnabled(false); + addProfile.setEnabled(false); + delProfile.setEnabled(false); + btnStartServer.setEnabled(false); + } + + public void stopped() { + profiles.setEnabled(true); + addProfile.setEnabled(true); + delProfile.setEnabled(true); + btnStartServer.setEnabled(true); + } + /** * Saves the previous profile and loads data from the new profile. */