Improved checking for abnormally closed servers

This commit is contained in:
Kristian Knarvik 2018-02-20 22:13:57 +01:00
parent 3e7c69d7a2
commit 2ff3c765a6
3 changed files with 66 additions and 14 deletions

View File

@ -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;
}
/**

View File

@ -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;
}
}

View File

@ -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.
*/