Reads server versions from file

This commit is contained in:
2018-01-25 17:34:45 +01:00
parent 3c3a6a3efa
commit 5e4b956e19
5 changed files with 113 additions and 67 deletions

View File

@ -1,14 +1,11 @@
package net.knarcraft.serverlauncher.server;
import java.io.File;
import java.io.*;
import java.net.URL;
import java.util.Scanner;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.util.ArrayList;
import java.io.FileNotFoundException;
/* Contains all necessary information to create, run and manage a Minecraft server. */
public class Server {
@ -78,6 +75,11 @@ public class Server {
this.type = type;
}
/**
* Sets the server's server version to a valid version, or ignores the request.
*
* @param serverVersion New version number.
*/
public void setServerVersion(String serverVersion) {
String[] versions = this.type.getVersions();
for (String version : versions) {
@ -95,6 +97,29 @@ public class Server {
this.pid = pid;
}
/**
* Runs the Minecraft server.
*/
public void run() {
if (this.isEnabled()) {
try {
this.downloadJar();
System.out.println("File downloaded.");
} catch (FileNotFoundException e) {
System.out.println("File was not found.");
return;
}
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path));
this.pid = pr.pid();
System.out.println("Success");
} catch (IOException e) {
System.out.println("Error");
}
}
}
/**
* Downloads necessary .jar file for the server.
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
@ -132,7 +157,6 @@ public class Server {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
System.out.println(newestVersion);
success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
@ -147,7 +171,7 @@ public class Server {
}
}
break;
case "Sponge":
case "SpongeVanilla":
try {
versionText = readFile(this.type.getVersionURL() + this.serverVersion);
} catch (IOException e) {

View File

@ -1,8 +1,5 @@
package net.knarcraft.serverlauncher.server;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Has a name and contains a list of valid server versions.
*/