Fixes some bugs preventing servers from starting
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
This commit is contained in:
@ -48,6 +48,11 @@ public class Server {
|
||||
private final String bungeeVersion;
|
||||
private boolean started;
|
||||
|
||||
/**
|
||||
* Initializes a new server with default values
|
||||
*
|
||||
* @param name <p>The name of the server</p>
|
||||
*/
|
||||
public Server(String name) {
|
||||
this.name = name;
|
||||
this.path = "";
|
||||
@ -65,6 +70,20 @@ public class Server {
|
||||
this.bungeeVersion = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a server with the given values
|
||||
*
|
||||
* @param name <p>The name of the server</p>
|
||||
* @param path <p>The file path of the folder containing the server files</p>
|
||||
* @param enabled <p>Whether the server is enabled to start the next time servers are started</p>
|
||||
* @param typeName <p>The name of the server type currently in use on the server</p>
|
||||
* @param serverVersion <p>The currently selected server version for the given server type</p>
|
||||
* @param maxRam <p>The maximum amount of ram the server is allowed to use</p>
|
||||
* @param vanillaVersion <p>The version of the "latest" downloaded vanilla version</p>
|
||||
* @param snapshotVersion <p>The version of the "latest" downloaded snapshot version</p>
|
||||
* @param spongeVanillaVersion <p>The version of the "latest" SpongeVanilla jar downloaded</p>
|
||||
* @param bungeeVersion <p>The version of the "latest" bungee jar downloaded</p>
|
||||
*/
|
||||
public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam,
|
||||
String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) {
|
||||
this.name = name;
|
||||
@ -80,18 +99,38 @@ public class Server {
|
||||
this.playerList = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the server
|
||||
*
|
||||
* @return <p>The name of the server</p>
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the server has been started
|
||||
*
|
||||
* @return <p>True if the server has been started. False otherwise</p>
|
||||
*/
|
||||
public boolean isStarted() {
|
||||
return started;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the server type used by this server
|
||||
*
|
||||
* @return <p>The name of the server type used by this server</p>
|
||||
*/
|
||||
public String getTypeName() {
|
||||
return this.type.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the version used given server type used
|
||||
*
|
||||
* @return <p>The server version given server type</p>
|
||||
*/
|
||||
public String getServerVersion() {
|
||||
return this.serverVersion;
|
||||
}
|
||||
@ -255,76 +294,39 @@ public class Server {
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Minecraft server.
|
||||
* Runs a Minecraft server
|
||||
*
|
||||
* @return <p>True if nothing went wrong</p>
|
||||
*/
|
||||
private boolean run() {
|
||||
if (this.enabled) {
|
||||
this.started = true;
|
||||
if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) {
|
||||
try {
|
||||
Profile.getGUI().setStatus("Downloading jar...");
|
||||
this.downloadJar();
|
||||
Profile.getGUI().setStatus("File downloaded");
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
Profile.getGUI().setStatus("Error: Jar file not found");
|
||||
e.printStackTrace();
|
||||
this.started = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
try {
|
||||
Profile.getGUI().setStatus("Delaying startup");
|
||||
TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
this.started = false;
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
ProcessBuilder builder;
|
||||
String serverPath;
|
||||
if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) {
|
||||
serverPath = jarDirectory + this.getType();
|
||||
} else {
|
||||
serverPath = this.path + File.separator + this.getType();
|
||||
}
|
||||
builder = new ProcessBuilder(
|
||||
"java",
|
||||
"-Xmx" + this.maxRam,
|
||||
"-Xms512M",
|
||||
"-Djline.terminal=jline.UnsupportedTerminal",
|
||||
"-Dcom.mojang.eula.agree=true",
|
||||
"-jar",
|
||||
serverPath,
|
||||
"nogui"
|
||||
);
|
||||
builder.directory(new File(this.path));
|
||||
builder.redirectErrorStream(true);
|
||||
this.process = builder.start();
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream()));
|
||||
this.reader = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
|
||||
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 {
|
||||
if (!this.enabled) {
|
||||
this.started = false;
|
||||
return true;
|
||||
}
|
||||
this.started = true;
|
||||
if (!initializeJarDownload() || !delayStartup()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
startServerProcess();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all available output from the server process.
|
||||
* Reads all available output from the server process
|
||||
*
|
||||
* @return The server output
|
||||
* @throws IOException If reading from the reader fails
|
||||
* @return <p>The server output</p>
|
||||
* @throws IOException <p>If reading from the reader fails</p>
|
||||
*/
|
||||
public String read() throws IOException {
|
||||
public String readFromServer() throws IOException {
|
||||
String line;
|
||||
StringBuilder text = new StringBuilder();
|
||||
while (reader.ready() && (line = reader.readLine()) != null) {
|
||||
@ -334,10 +336,77 @@ public class Server {
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads necessary .jar file for the server.
|
||||
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
|
||||
* Starts the process running this server
|
||||
*
|
||||
* @throws FileNotFoundException if the file was not found and could not be acquired.
|
||||
* @throws IOException <p>If the process cannot be started</p>
|
||||
*/
|
||||
private void startServerProcess() throws IOException {
|
||||
ProcessBuilder builder;
|
||||
String serverPath;
|
||||
String serverFile;
|
||||
if (type.getName().equals("Custom")) {
|
||||
serverFile = serverVersion;
|
||||
} else {
|
||||
serverFile = this.type.getName() + serverVersion + ".jar";
|
||||
}
|
||||
if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) {
|
||||
serverPath = jarDirectory + serverFile;
|
||||
} else {
|
||||
serverPath = this.path + File.separator + serverFile;
|
||||
}
|
||||
builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M",
|
||||
"-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath,
|
||||
"nogui");
|
||||
builder.directory(new File(this.path));
|
||||
builder.redirectErrorStream(true);
|
||||
this.process = builder.start();
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream()));
|
||||
this.reader = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delays the server's startup for the given amount of time
|
||||
*
|
||||
* @return <p>True if the delay was successful</p>
|
||||
*/
|
||||
private boolean delayStartup() {
|
||||
try {
|
||||
Profile.getGUI().setStatus("Delaying startup");
|
||||
TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup());
|
||||
return true;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
this.started = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts downloading the necessary .jar file
|
||||
*
|
||||
* @return <p>True if nothing went wrong</p>
|
||||
*/
|
||||
private boolean initializeJarDownload() {
|
||||
if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) {
|
||||
try {
|
||||
Profile.getGUI().setStatus("Downloading jar...");
|
||||
this.downloadJar();
|
||||
Profile.getGUI().setStatus("File downloaded");
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
Profile.getGUI().setStatus("Error: Jar file not found");
|
||||
e.printStackTrace();
|
||||
this.started = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads necessary .jar file for the server.
|
||||
*
|
||||
* @throws FileNotFoundException <p>If the file was not found and could not be acquired</p>
|
||||
*/
|
||||
private void downloadJar() throws IOException {
|
||||
String path = this.path + File.separator;
|
||||
|
@ -54,7 +54,7 @@ public class Vanilla extends AbstractServerType {
|
||||
* Gets the URL to the .jar file for the newest version
|
||||
*
|
||||
* @return <p>An array containing the latest version and a link to its file</p>
|
||||
* @throws IOException <p>If the remote resource cannot be read</p>
|
||||
* @throws IOException <p>If the remote resource cannot be readFromServer</p>
|
||||
*/
|
||||
private String[] getLatestFile() throws IOException {
|
||||
String versionText = readFile(versionURL);
|
||||
@ -70,7 +70,7 @@ public class Vanilla extends AbstractServerType {
|
||||
*
|
||||
* @param versionURL <p>The URL to the version document describing the .jar file</p>
|
||||
* @return <p>The URL necessary do download the .jar file</p>
|
||||
* @throws IOException <p>If the remote resource cannot be read</p>
|
||||
* @throws IOException <p>If the remote resource cannot be readFromServer</p>
|
||||
*/
|
||||
private String getVanillaDownloadURL(String versionURL) throws IOException {
|
||||
String versionText = readFile(versionURL);
|
||||
|
Reference in New Issue
Block a user