From 5ac22babab0f038b4169d120d524f6adfde42140 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Thu, 29 Mar 2018 16:09:08 +0200 Subject: [PATCH] Bugfixes and optimizations Fixes a bug where the server version is not updated on load. Optimized downloading of all jar files. --- .../serverlauncher/profile/Profile.java | 193 ++++++------------ .../serverlauncher/server/Server.java | 36 +--- .../userinterface/ServerTab.java | 2 +- 3 files changed, 73 insertions(+), 158 deletions(-) diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index 1c34eeb..7869f87 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -461,6 +461,12 @@ public class Profile { } } + /** + * Parses a profile, and creates a profile with the data. + * + * @param profileData The data of the new profile + * @return The new profile + */ private static Profile parseProfile(String[] profileData) { return new Profile( profileData[0], @@ -470,6 +476,12 @@ public class Profile { ); } + /** + * Parses a server, and creates a new collection. + * + * @param profile The profile which to add the collection + * @param serverData The data to parse + */ private static void parseServer(Profile profile, String[] serverData) { profile.collections.add(new Collection( serverData[0], @@ -485,6 +497,11 @@ public class Profile { ); } + /** + * Downloads all jars to the program directory. + * + * @throws IOException On version file failure or folder creation failure + */ public static void downloadJars() throws IOException { if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) { JOptionPane.showMessageDialog( @@ -495,29 +512,58 @@ public class Profile { ); throw new FileNotFoundException("Unable to create jars folder"); } - downloadSimple("Spigot"); - downloadSimple("Craftbukkit"); - downloadSimple("MCPCplus"); - downloadMixed("Vanilla"); - downloadMixed("Snapshot"); - downloadSponge(); - downloadBungee(); - gui.setStatus("Finished downloading jars"); + try { + downloadAll(); + gui.setStatus("Finished downloading jars"); + } catch (FileNotFoundException e) { + gui.setStatus("One or more downloads failed: " + e.getMessage()); + } } - private static void downloadSimple(String typeName) throws FileNotFoundException { - ServerType type = ServerType.getByName(typeName); - String url = Objects.requireNonNull(type).getDownloadURL(); - String name = type.getName(); - Boolean success; - for (String version : type.getVersions()) { - File file = new File(jarDir + type.getName() + version + ".jar"); - if (!file.isFile()) { - Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); + /** + * Downloads jar files for all possible server versions. + * + * @throws IOException If a jar fails to download. + */ + private static void downloadAll() throws IOException { + for (ServerType type : ServerType.getServerTypes()) { + String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion; + AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null; + for (String version : type.getVersions()) { + Boolean success; if (gui != null) { gui.setStatus("Downloading: " + name + version + ".jar"); } - success = downloadFile(url + name + version + ".jar", filePath); + File file = new File(jarDir + type.getName() + version + ".jar"); + Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); + switch (type.getName()) { + case "Vanilla": + case "Snapshot": + if (version.equals("Latest")) { + newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd()); + setVersion(name, newestVersion); + success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath); + } else { + success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath); + } + break; + case "Spigot": + case "Craftbukkit": + case "MCPCplus": + success = file.isFile() || downloadFile(url + name + version + ".jar", filePath); + break; + case "SpongeVanilla": + newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL() + version), advType.getSrcStart(), advType.getSrcEnd()); + success = file.isFile() || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath); + break; + case "Bungee": + newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd()); + setVersion(name, newestVersion); + success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url, filePath); + break; + default: + success = true; + } if (!success) { if (gui != null) { gui.setStatus("Error downloading: " + name + version + ".jar"); @@ -528,117 +574,6 @@ public class Profile { } } - private static void downloadMixed(String typeName) throws IOException { - AdvancedServerType type = (AdvancedServerType) ServerType.getByName(typeName); - String url = Objects.requireNonNull(type).getDownloadURL(); - String name = type.getName(); - String versionText; - String newestVersion; - Boolean success; - for (String version : type.getVersions()) { - File file = new File(jarDir + type.getName() + version + ".jar"); - Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); - if (gui != null) { - gui.setStatus("Downloading: " + name + version + ".jar"); - } - if (version.equals("Latest")) { - try { - versionText = readFile(type.getVersionURL()); - } catch (IOException e) { - throw new IOException("Error reading: " + type.getVersionURL()); - } - newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); - if (!file.isFile() || !newestVersion.equals(getVersion(name))) { - success = downloadFile( - url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", - filePath - ); - setVersion(name, newestVersion); - if (!success) { - if (gui != null) { - gui.setStatus("Error downloading: " + name + version + ".jar"); - } - throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); - } - } - } else { - if (!file.isFile()) { - success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath); - if (!success) { - if (gui != null) { - gui.setStatus("Error downloading: " + name + version + ".jar"); - } - throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); - } - } - } - } - } - - private static void downloadSponge() throws IOException { - AdvancedServerType type = (AdvancedServerType) ServerType.getByName("SpongeVanilla"); - String url = Objects.requireNonNull(type).getDownloadURL(); - String name = type.getName(); - String versionText; - String newestVersion; - Boolean success; - for (String version : type.getVersions()) { - File file = new File(jarDir + name + version + ".jar"); - Path filePath = Paths.get(jarDir + name + version + ".jar"); - if (gui != null) { - gui.setStatus("Downloading: " + name + version + ".jar"); - } - try { - versionText = readFile(type.getVersionURL() + version); - } catch (IOException e) { - throw new IOException("Error reading: " + type.getVersionURL()); - } - newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); - if (!file.isFile()) { - success = downloadFile( - url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", - filePath - ); - if (!success) { - if (gui != null) { - gui.setStatus("Error downloading: " + name + version + ".jar"); - } - throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); - } - } - } - } - - private static void downloadBungee() throws IOException { - AdvancedServerType type = (AdvancedServerType) ServerType.getByName("Bungee"); - String url = Objects.requireNonNull(type).getDownloadURL(); - String name = type.getName(); - String versionText; - String newestVersion; - Boolean success; - File file = new File(jarDir + type.getName() + ".jar"); - Path filePath = Paths.get(jarDir + type.getName() + ".jar"); - if (gui != null) { - gui.setStatus("Downloading: " + name + ".jar"); - } - try { - versionText = readFile(type.getVersionURL()); - } catch (IOException e) { - throw new IOException("Error reading: " + type.getVersionURL()); - } - newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); - if (!file.isFile() || !newestVersion.equals(getVersion(name))) { - success = downloadFile(url, filePath); - setVersion(name, newestVersion); - if (!success) { - if (gui != null) { - gui.setStatus("Error downloading: " + name + ".jar"); - } - throw new FileNotFoundException("Error downloading: " + name + ".jar"); - } - } - } - /** * Returns the current version of a type * diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index a61b6c8..3c30cfa 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -351,12 +351,7 @@ public class Server { AdvancedServerType type; File file = new File(this.path + File.separator + this.getType()); Path filePath = Paths.get(this.path + File.separator + this.getType()); - String versionText; - String newestVersion; - String url = this.type.getDownloadURL(); - String name = this.type.getName(); - String ver = this.serverVersion; - boolean success; + String versionText, newestVersion, url = this.type.getDownloadURL(), name = this.type.getName(), ver = this.serverVersion; switch (this.type.getName()) { case "Custom": if (!file.isFile()) { @@ -366,11 +361,8 @@ public class Server { case "Spigot": case "Craftbukkit": case "MCPCplus": - if (!file.isFile()) { - success = downloadFile(url + name + ver + ".jar", filePath); - if (!success) { - throw new FileNotFoundException("Jar file could not be downloaded."); - } + if (!(file.isFile() || downloadFile(url + name + ver + ".jar", filePath))) { + throw new FileNotFoundException("Jar file could not be downloaded."); } break; case "Vanilla": @@ -384,21 +376,14 @@ public class Server { } newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { - success = downloadFile( - url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", - filePath - ); this.setVersion(name, newestVersion); - if (!success) { + if (!downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) { throw new FileNotFoundException("Jar file could not be downloaded."); } } } else { - if (!file.isFile()) { - success = downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath); - if (!success) { - throw new FileNotFoundException("Jar file could not be downloaded."); - } + if (!(file.isFile() || downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath))) { + throw new FileNotFoundException("Jar file could not be downloaded."); } } break; @@ -411,12 +396,8 @@ public class Server { } newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { - success = downloadFile( - url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", - filePath - ); this.setVersion(name, newestVersion); - if (!success) { + if (!downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) { throw new FileNotFoundException("Jar file could not be downloaded."); } } @@ -430,9 +411,8 @@ public class Server { } newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { - success = downloadFile(url, filePath); this.setVersion(name, newestVersion); - if (!success) { + if (!downloadFile(url, filePath)) { throw new FileNotFoundException("Jar file could not be downloaded."); } } diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java index 47d7b71..d2e726d 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java @@ -38,7 +38,7 @@ public class ServerTab implements ActionListener { this.chckbxEnabled.setSelected(enabled); this.serverTypes.setSelectedItem(typeName); this.serverTypes(); - this.serverTypes.setSelectedItem(serverVersion); + this.serverVersions.setSelectedItem(serverVersion); this.maxRam.setSelectedItem(maxRam); }