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,165 +9,205 @@ 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[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
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 String name; private String name;
private String path; private String path;
private boolean enabled; private boolean enabled;
private ArrayList<String> playerList; private ArrayList<String> playerList;
private AdvancedServerType type; private AdvancedServerType type;
private String serverVersion; private String serverVersion;
private String maxRam; private String maxRam;
private long pid; private long pid;
public Server(String name) { public Server(String name) {
this.name = name; this.name = name;
this.path = ""; this.path = "";
this.enabled = false; this.enabled = false;
this.playerList = new ArrayList<>(); this.playerList = new ArrayList<>();
this.type = null; this.type = null;
this.serverVersion = null; this.serverVersion = null;
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.pid = -1; this.pid = -1;
} }
public void addPlayer(String name) { public void addPlayer(String name) {
this.playerList.add(name); this.playerList.add(name);
} }
public void removePlayer(String name) { public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) { for (int i = 0; i < playerList.size(); i++) {
if (name.equals(playerList.get(i))) { if (name.equals(playerList.get(i))) {
playerList.remove(i); playerList.remove(i);
return; return;
} }
} }
} }
public String getPath() { public String getPath() {
return this.path; return this.path;
} }
public String getType() { public String getType() {
return this.type.getName() + this.serverVersion + ".jar"; return this.type.getName() + this.serverVersion + ".jar";
} }
public boolean isEnabled() { public boolean isEnabled() {
return this.enabled; return this.enabled;
} }
public String maxRam() { public String maxRam() {
return this.maxRam; return this.maxRam;
} }
public void updatePid(long pid) { public void toggle() {
this.pid = pid; 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) {
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());
switch (this.type.getName()) { String versionText;
case "Custom": String newestVersion;
if (!file.isFile()) { boolean success;
throw new FileNotFoundException("Specified custom jar was not found."); switch (this.type.getName()) {
case "Custom":
if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
break;
case "Spigot":
case "Craftbukkit":
case "MCPCplus":
if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + type.getName() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
} }
case "Spigot": }
if (!file.isFile()) { break;
staticJar(SPIGOTURL); case "Vanilla":
case "Snapshot":
if (this.serverVersion.equals("Latest")) {
try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
} }
break; newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
case "Craftbukkit": success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
staticJar(BUKKITURL); //TODO: Register the new version number, and only download if version mismatch or missing file.
break; if (!success) {
case "MCPCplus": throw new FileNotFoundException("Jar file could not be downloaded.");
staticJar(MCPCURL); }
break; } else {
case "Vanilla": if (!file.isFile()) {
case "Snapshot": success = downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
if (this.serverVersion.equals("Latest")) {
String versionText;
try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
String newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
boolean success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
if (!success) { if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded."); throw new FileNotFoundException("Jar file could not be downloaded.");
} }
} else {
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
} }
break; }
case "Sponge": break;
staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType()); case "Sponge":
} try {
//TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched. versionText = readFile(this.type.getVersionURL() + this.serverVersion);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
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())); * Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read.
* @return True if successfull. False otherwise.
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
} }
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())); * Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
} }
/**
* Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read.
* @return True if successfull. False otherwise.
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
/** /**
* Finds a substring between two substrings in a string. * Finds a substring between two substrings in a string.
* *
* @param string The string containing the substrings. * @param string The string containing the substrings.
* @param start The substring before the wanted substring. * @param start The substring before the wanted substring.
* @param end The substring after the wanted substring. * @param end The substring after the wanted substring.
* @return The wanted substring. * @return The wanted substring.
*/ */
private String stringBetween(String string, String start, String end) { private String stringBetween(String string, String start, String end) {
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end)); return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
} }
} }

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");
}
}
}
}