diff --git a/src/Main.java b/src/Main.java index f904928..0ccd1cd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -25,10 +25,11 @@ public class Main { */ private static void addServerTypes() { //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 ServerType("Snapshot", new String[]{"Latest"})); + 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 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("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[]{""})); diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index b526921..936d68f 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -11,11 +11,14 @@ import java.util.ArrayList; import java.io.FileNotFoundException; /* Contains all necessary information to create, run and manage a Minecraft 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 BUKKITURL = BASEURL + "/Bukkit/"; private static final String MCPCURL = BASEURL + "/MCPC+/"; 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 path; private boolean enabled; @@ -76,12 +79,17 @@ public class Server { * Custom files must exist, or trigger a message. */ 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()) { 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": - staticJar(SPIGOTURL); + if (!file.isFile()) { + staticJar(SPIGOTURL); + } break; case "Craftbukkit": staticJar(BUKKITURL); @@ -90,17 +98,27 @@ public class Server { staticJar(MCPCURL); break; case "Vanilla": + case "Snapshot": 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 { - + downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath); } break; case "Sponge": 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. - } } 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. * * @param path The full url of the file to read. * @return True if successfull. False otherwise. */ - public static boolean readFile(String path) { - //We might need to change this to look for specific lines in the file to find vanilla and snapshot versions. - //The version should probably be returned. Empty or null on failiure. - 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; - } + private static String readFile(String path) throws IOException { + URL url = new URL(path); + return new Scanner(url.openStream()).useDelimiter("\\Z").next(); } /** @@ -141,16 +150,23 @@ public class Server { */ private static boolean downloadFile(String path, Path outfile) { try { - URL url = new URL("https://knarcraft.net/Api/View/Jawns/Jawns%20instructions.txt"); + URL url = new URL(path); InputStream in = url.openStream(); Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING); return true; } catch (IOException e) { - System.out.println("Error"); 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) { return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end)); }