Comment and code additions

More comments are added.
More work on downloading server jars is done.
This commit is contained in:
Kristian Knarvik 2018-01-24 22:31:48 +01:00
parent 1ffed9310a
commit 227b00d801
2 changed files with 42 additions and 25 deletions

View File

@ -25,10 +25,11 @@ public class Main {
*/ */
private static void addServerTypes() { private static void addServerTypes() {
//This could be changed to read from a list which is downloaded to the user's computer. //This could be changed to read from a list which is downloaded to the user's computer.
serverTypes.add(new ServerType("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"})); 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 ServerType("Snapshot", new String[]{"Latest"})); 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 ServerType("Spigot", new String[]{"1.12", "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 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("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"}));
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"}));
serverTypes.add(new ServerType("Custom", new String[]{""})); serverTypes.add(new ServerType("Custom", new String[]{""}));

View File

@ -11,11 +11,14 @@ 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. private static final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from.
private static final String BUKKITURL = BASEURL + "/Bukkit/"; private static final String BUKKITURL = BASEURL + "/Bukkit/";
private static final String MCPCURL = BASEURL + "/MCPC+/"; private static final String MCPCURL = BASEURL + "/MCPC+/";
private static final String SPIGOTURL = BASEURL + "/Spigot/"; private static final String SPIGOTURL = BASEURL + "/Spigot/";
private static String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"}; /** 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;
@ -76,12 +79,17 @@ public class Server {
* Custom files must exist, or trigger a message. * Custom files must exist, or trigger a message.
*/ */
public void downloadJar() throws FileNotFoundException { public void downloadJar() throws FileNotFoundException {
if (!new File(this.path + "\\" + this.getType()).isFile()) { File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
switch (this.type.getName()) { switch (this.type.getName()) {
case "Custom": case "Custom":
throw new FileNotFoundException("Specified custom jar was not found."); if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
case "Spigot": case "Spigot":
staticJar(SPIGOTURL); if (!file.isFile()) {
staticJar(SPIGOTURL);
}
break; break;
case "Craftbukkit": case "Craftbukkit":
staticJar(BUKKITURL); staticJar(BUKKITURL);
@ -90,17 +98,27 @@ public class Server {
staticJar(MCPCURL); staticJar(MCPCURL);
break; break;
case "Vanilla": case "Vanilla":
case "Snapshot":
if (this.serverVersion.equals("Latest")) { 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) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
} else { } else {
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
} }
break; break;
case "Sponge": case "Sponge":
staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType()); staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType());
} }
//TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched. //TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched.
}
} }
private boolean staticJar(String url) { private boolean staticJar(String url) {
@ -112,24 +130,15 @@ public class Server {
} }
/** /**
* Reads the first line of a file from a website. * Reads a file from a website.
* This is used to find the newest version of the software. * This is used to find the newest version of the software.
* *
* @param path The full url of the file to read. * @param path The full url of the file to read.
* @return True if successfull. False otherwise. * @return True if successfull. False otherwise.
*/ */
public static boolean readFile(String path) { private static String readFile(String path) throws IOException {
//We might need to change this to look for specific lines in the file to find vanilla and snapshot versions. URL url = new URL(path);
//The version should probably be returned. Empty or null on failiure. return new Scanner(url.openStream()).useDelimiter("\\Z").next();
try {
URL url = new URL(path);
Scanner s = new Scanner(url.openStream());
System.out.println(s.next());
return true;
} catch (IOException e) {
System.out.println("Error");
return false;
}
} }
/** /**
@ -141,16 +150,23 @@ public class Server {
*/ */
private static boolean downloadFile(String path, Path outfile) { private static boolean downloadFile(String path, Path outfile) {
try { try {
URL url = new URL("https://knarcraft.net/Api/View/Jawns/Jawns%20instructions.txt"); URL url = new URL(path);
InputStream in = url.openStream(); InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING); Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error");
return false; return false;
} }
} }
/**
* Finds a substring between two substrings in a string.
*
* @param string The string containing the substrings.
* @param start The substring before the wanted substring.
* @param end The substring after 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));
} }