Comment and code additions

More comments are added.
More work on downloading server jars is done.
Server test added.
This commit is contained in:
Kristian Knarvik 2018-01-25 10:53:51 +01:00
parent 227b00d801
commit b6685edc6f
5 changed files with 258 additions and 149 deletions

View File

@ -29,10 +29,10 @@ public class Main {
serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server.")); serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server."));
serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-")); serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-"));
serverTypes.add(new AdvancedServerType("Bungee", new String[]{"Latest"}, "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/", "Artifacts of BungeeCord #", " ", "http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar", "")); serverTypes.add(new AdvancedServerType("Bungee", new String[]{"Latest"}, "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/", "Artifacts of BungeeCord #", " ", "http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar", ""));
serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"})); serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/"));
serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"})); serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/"));
serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"})); serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/"));
serverTypes.add(new ServerType("Custom", new String[]{""})); serverTypes.add(new ServerType("Custom", new String[]{""}, ""));
} }
/** /**

View File

@ -2,17 +2,15 @@ package net.knarcraft.serverlauncher.server;
public class AdvancedServerType extends ServerType { public class AdvancedServerType extends ServerType {
private String versionURL; private String versionURL;
private String downloadURL;
private String downloadURLPart; private String downloadURLPart;
private String srcStart; private String srcStart;
private String srcEnd; private String srcEnd;
public AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) { public AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) {
super(name, versions); super(name, versions, downloadURL);
this.srcStart = srcStart; this.srcStart = srcStart;
this.srcEnd = srcEnd; this.srcEnd = srcEnd;
this.versionURL = versionURL; this.versionURL = versionURL;
this.downloadURL = downloadURL;
this.downloadURLPart = downloadURLPart; this.downloadURLPart = downloadURLPart;
} }
@ -20,10 +18,6 @@ public class AdvancedServerType extends ServerType {
return this.versionURL; return this.versionURL;
} }
public String getDownloadURL() {
return this.downloadURL;
}
public String getDownloadURLPart() { public String getDownloadURLPart() {
return this.downloadURLPart; return this.downloadURLPart;
} }

View File

@ -9,14 +9,12 @@ import java.nio.file.StandardCopyOption;
import java.nio.file.*; import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
/* Contains all necessary information to create, run and manage a Minecraft server. */ /* Contains all necessary information to create, run and manage a Minecraft server. */
public class Server { public class Server {
/** Necessary urls for downloading from knarcraft.net */ /**
private static final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from. * Available ram sizes. For GUI dropdown
private static final String BUKKITURL = BASEURL + "/Bukkit/"; */
private static final String MCPCURL = BASEURL + "/MCPC+/";
private static final String SPIGOTURL = BASEURL + "/Spigot/";
/** Available ram sizes. For GUI dropdown */
private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"}; private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private String name; private String name;
@ -68,65 +66,107 @@ public class Server {
return this.maxRam; return this.maxRam;
} }
public void toggle() {
this.enabled = !this.enabled;
}
public void setPath(String path) {
this.path = path;
}
public void setType(AdvancedServerType type) {
this.type = type;
}
public void setServerVersion(String serverVersion) {
this.serverVersion = serverVersion;
}
public void setMaxRam(String ram) {
this.maxRam = ram;
}
public void updatePid(long pid) { public void updatePid(long pid) {
this.pid = pid; this.pid = pid;
} }
/** /**
* Downloads necessary .jar file for the server. * Downloads necessary .jar file for the server.
* All server versions are downloaded if missing. * This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
* Newest server versions (snapshot, vanilla and bungee) need to be checked against an online json file and downloaded if outdated. *
* Custom files must exist, or trigger a message. * @throws FileNotFoundException if the file was not found and could not be acquired.
*/ */
public void downloadJar() throws FileNotFoundException { public void downloadJar() throws FileNotFoundException {
File file = new File(this.path + "\\" + this.getType()); File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType()); Path filePath = Paths.get(this.path + "\\" + this.getType());
String versionText;
String newestVersion;
boolean success;
switch (this.type.getName()) { switch (this.type.getName()) {
case "Custom": case "Custom":
if (!file.isFile()) { if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found."); throw new FileNotFoundException("Specified custom jar was not found.");
} }
break;
case "Spigot": case "Spigot":
if (!file.isFile()) {
staticJar(SPIGOTURL);
}
break;
case "Craftbukkit": case "Craftbukkit":
staticJar(BUKKITURL);
break;
case "MCPCplus": case "MCPCplus":
staticJar(MCPCURL); if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + type.getName() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
break; break;
case "Vanilla": case "Vanilla":
case "Snapshot": case "Snapshot":
if (this.serverVersion.equals("Latest")) { if (this.serverVersion.equals("Latest")) {
String versionText;
try { try {
versionText = readFile(this.type.getVersionURL()); versionText = readFile(this.type.getVersionURL());
} catch (IOException e) { } catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded."); throw new FileNotFoundException("Version file could not be downloaded.");
} }
String newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
boolean success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath); 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) { if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded."); throw new FileNotFoundException("Jar file could not be downloaded.");
} }
} else { } else {
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath); if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
} }
break; break;
case "Sponge": case "Sponge":
staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType()); try {
versionText = readFile(this.type.getVersionURL() + this.serverVersion);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
} }
//TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched. newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
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) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
break;
case "Bungee":
/*try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());*/
success = downloadFile(type.getDownloadURL(), filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
} }
private boolean staticJar(String url) {
return downloadFile(url + this.getType(), Paths.get(this.path + this.getType()));
} }
private boolean staticJar(String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart, String outfile) {
return downloadFile(versionURL + this.getType(), Paths.get(this.path + this.getType()));
} }
/** /**

View File

@ -7,12 +7,14 @@ import java.net.URL;
* Has a name and contains a list of valid server versions. * Has a name and contains a list of valid server versions.
*/ */
public class ServerType { public class ServerType {
private String name; private final String name;
private String[] versions; private final String[] versions;
private final String downloadURL;
public ServerType(String name, String[] versions) { public ServerType(String name, String[] versions, String downloadURL) {
this.name = name; this.name = name;
this.versions = versions; this.versions = versions;
this.downloadURL = downloadURL;
} }
public String getName() { public String getName() {
@ -22,4 +24,8 @@ public class ServerType {
public String[] getVersions() { public String[] getVersions() {
return this.versions; return this.versions;
} }
public String getDownloadURL() {
return this.downloadURL;
}
} }

69
test/ServerTest.java Normal file
View File

@ -0,0 +1,69 @@
import net.knarcraft.serverlauncher.server.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class ServerTest {
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
private static ArrayList<Server> servers = new ArrayList<>();
public static void main(String[] args) {
addServerTypes();
Server server1 = new Server("Server1");
server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType((AdvancedServerType) serverTypes.get(0));
server1.setServerVersion("Latest");
server1.setMaxRam("1G");
servers.add(server1);
startServers(servers);
}
/**
* Adds all the valid server types and versions.
*/
private static void addServerTypes() {
//This could be changed to read from a list which is downloaded to the user's computer.
serverTypes.add(new AdvancedServerType("Vanilla", new String[]{"Latest", "1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.9", "1.7.10", "1.6.4", "1.5.2", "1.4.7", "1.3.2", "1.2.5"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"release\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server."));
serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server."));
serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-"));
serverTypes.add(new AdvancedServerType("Bungee", new String[]{"Latest"}, "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/", "Artifacts of BungeeCord #", " ", "http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar", ""));
serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/"));
serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/"));
serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/"));
serverTypes.add(new ServerType("Custom", new String[]{""}, ""));
}
/**
* Runs all enabled servers with their settings.
*/
public static void startServers(ArrayList<Server> servers) {
System.out.println("Starting servers.");
for (Server server : servers) {
if (server.isEnabled()) {
String path = server.getPath();
String type = server.getType();
try {
server.downloadJar();
System.out.println("File downloaded.");
} catch (FileNotFoundException e) {
System.out.println("File was not found.");
return;
}
String ram = server.maxRam();
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("java -Xmx" + ram + " -Xms512M -jar " + "\"" + path + "\\" + type + "\" nogui");
long pid = pr.pid();
server.updatePid(pid);
System.out.println("Success");
} catch (IOException e) {
System.out.println("Error");
}
} else {
System.out.println("Server disabled");
}
}
}
}