From 70d064e5905c8ae01cb22f9f3cd8f8e236ecc7b5 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 13 Aug 2020 03:04:02 +0200 Subject: [PATCH] Makes snapshot a vanilla version and loads all jars from the jars folder --- pom.xml | 2 +- .../profile/Profile.java | 6 +- .../server/Server.java | 13 +- .../server/ServerTypeHandler.java | 13 +- .../server/servertypes/CraftBukkit.java | 3 + .../server/servertypes/Snapshot.java | 19 --- .../server/servertypes/Vanilla.java | 43 +++++-- src/main/resources/servertypes.csv | 3 +- .../DownloadTests.java | 18 --- .../server/ServerTypeHandlerTest.java | 38 ++++++ .../CommonFunctionsTest.java} | 8 +- .../utility/JarDownloaderTest.java | 111 ++++++++++++++++++ 12 files changed, 210 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java delete mode 100644 src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java create mode 100644 src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java rename src/test/java/net/knarcraft/minecraftserverlauncher/{Tests.java => utility/CommonFunctionsTest.java} (74%) create mode 100644 src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java diff --git a/pom.xml b/pom.xml index 6f8a492..c6530a0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.knarcraft.minecraftserverlauncher minecraft-server-launcher - 1.0-SNAPSHOT + 1.1-SNAPSHOT jar diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java index b68cf75..ce12b67 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java @@ -326,7 +326,11 @@ public class Profile implements java.io.Serializable { ServerTab serverTab = collection.getServerTab(); server.setPath(serverTab.getPath()); server.setMaxRam(serverTab.getMaxRam()); - server.setType(ServerTypeHandler.getByName(serverTab.getType())); + try { + server.setType(ServerTypeHandler.getByName(serverTab.getType())); + } catch (ConfigurationException e) { + e.printStackTrace(); + } try { server.setServerVersion(serverTab.getVersion()); } catch (IllegalArgumentException e) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index 7d104ef..ef0358e 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -5,6 +5,7 @@ import net.knarcraft.minecraftserverlauncher.profile.Collection; import net.knarcraft.minecraftserverlauncher.profile.Profile; import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import javax.naming.ConfigurationException; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -73,7 +74,7 @@ public class Server implements java.io.Serializable { * @param serverVersion

The currently selected server version for the given server type

* @param maxRam

The maximum amount of ram the server is allowed to use

*/ - public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) { + public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) throws ConfigurationException { this.name = name; this.path = path; this.enabled = enabled; @@ -133,7 +134,7 @@ public class Server implements java.io.Serializable { * @param saveString

The string containing necessary data regarding the server

* @return

A server in the same state it was saved in

*/ - public static Server fromString(String saveString) { + public static Server fromString(String saveString) throws ConfigurationException { String[] data = saveString.split(";"); return new Server(data[0], data[1], Boolean.parseBoolean(data[2]), data[3], data[4], data[5]); } @@ -385,7 +386,7 @@ public class Server implements java.io.Serializable { } else { serverFile = this.type.getName() + serverVersion + ".jar"; } - if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) { + if (!type.getName().equals("Custom")) { serverPath = jarDirectory + serverFile; } else { serverPath = this.path + File.separator + serverFile; @@ -444,9 +445,11 @@ public class Server implements java.io.Serializable { * @throws FileNotFoundException

If the file was not found and could not be acquired

*/ private void downloadJar() throws IOException { - String path = this.path + File.separator; + String path; if (this.type.getName().equals("Custom")) { - path += this.serverVersion; + path = this.path + File.separator + this.serverVersion; + } else { + path = jarDirectory; } File file = new File(path); if (!(file.isFile() || type.downloadJar(path, this.serverVersion))) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java index d1fcef9..ce5c4f2 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java @@ -6,7 +6,6 @@ import net.knarcraft.minecraftserverlauncher.server.servertypes.Custom; import net.knarcraft.minecraftserverlauncher.server.servertypes.MCPCPlus; import net.knarcraft.minecraftserverlauncher.server.servertypes.Paper; import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; -import net.knarcraft.minecraftserverlauncher.server.servertypes.Snapshot; import net.knarcraft.minecraftserverlauncher.server.servertypes.Spigot; import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeVanilla; import net.knarcraft.minecraftserverlauncher.server.servertypes.Travertine; @@ -33,6 +32,9 @@ public class ServerTypeHandler { * @return

A list of strings

*/ public static String[] getTypeNames() throws ConfigurationException { + if (serverTypes.isEmpty()) { + loadServerTypes(); + } ArrayList types = getServerTypes(); String[] serverTypeNames = new String[types.size()]; for (int i = 0; i < types.size(); i++) { @@ -59,7 +61,10 @@ public class ServerTypeHandler { * @param name

Then name of the server type

* @return

A AbstractServerType

*/ - public static ServerType getByName(String name) { + public static ServerType getByName(String name) throws ConfigurationException { + if (serverTypes.isEmpty()) { + loadServerTypes(); + } for (ServerType serverType : serverTypes) { if (serverType.getName().equals(name)) { return serverType; @@ -117,10 +122,6 @@ public class ServerTypeHandler { newType = new Vanilla("Vanilla", false, serverVersions, serverTypeInfo[2], serverTypeInfo[3]); break; - case "Snapshot": - newType = new Snapshot("Snapshot", false, serverVersions, serverTypeInfo[2], - serverTypeInfo[3]); - break; case "MCPCplus": newType = new MCPCPlus("MCPCplus", false, serverVersions, serverTypeInfo[2], serverTypeInfo[3]); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java index a1313e2..a56e389 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/CraftBukkit.java @@ -5,6 +5,9 @@ import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; +/** + * This class represents the CraftBukkit Minecraft server type + */ public class CraftBukkit extends AbstractServerType { private String downloadURLPart; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java deleted file mode 100644 index 6e172b2..0000000 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Snapshot.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.knarcraft.minecraftserverlauncher.server.servertypes; - -public class Snapshot extends Vanilla { - - /** - * Instantiates a snapshot server type - * - * @param typeName

The name of this server type

- * @param isProxy

Whether this server type is a proxy server

- * @param versions

Available versions for this server type

- * @param versionURL

The URL used for downloading the version document

- * @param downloadURL

The URL used for downloading the new file

- */ - public Snapshot(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) { - super(typeName, isProxy, versions, versionURL, downloadURL); - this.releaseType = "snapshot"; - } - -} diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java index 365277f..fd2a5b7 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -13,11 +13,14 @@ import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; +/** + * This class represents the regular vanilla server type + */ public class Vanilla extends AbstractServerType { private final String versionURL; - String releaseType; - private String lastVersion; + private String lastVanillaVersion; + private String lastSnapshotVersion; /** * Instantiates a vanilla server type @@ -31,33 +34,51 @@ public class Vanilla extends AbstractServerType { public Vanilla(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) { super(typeName, isProxy, versions, downloadURL); this.versionURL = versionURL; - this.releaseType = "release"; } @Override public boolean downloadJar(String folder, String version) throws IOException { String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); - if (version.equals("Latest")) { - String[] latestData = getLatestFile(); - String latest = latestData[0]; - String jarFile = latestData[1]; - String currentVersion = lastVersion; - lastVersion = latest; - return (filePath.isFile() && latest.equals(currentVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); + if (version.equals("Latest") || version.equals("Snapshot")) { + String releaseType = version.equals("Latest") ? "release" : "snapshot"; + String lastVersion = version.equals("Latest") ? lastVanillaVersion : lastSnapshotVersion; + return downloadLatestJar(filePath, releaseType, lastVersion); } else { String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version)); return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI())); } } + /** + * Downloads the latest .jar file found if necessary + * + * @param filePath

The path of the jar file to download

+ * @param releaseType

The release type used for downloading

+ * @param lastVersion

The last server version found

+ * @return

True if the jar exists and is the latest version or was downloaded

+ * @throws IOException

If the .jar cannot be downloaded

+ */ + private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion) throws IOException { + String[] latestData = getLatestFile(releaseType); + String latest = latestData[0]; + String jarFile = latestData[1]; + if (releaseType.equals("release")) { + lastVanillaVersion = latest; + } else { + lastSnapshotVersion = latest; + } + return (filePath.isFile() && latest.equals(lastVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); + } + /** * Gets the URL to the .jar file for the newest version * + * @param releaseType

The type of release to read latest version from

* @return

An array containing the latest version and a link to its file

* @throws IOException

If the remote resource cannot be readFromServer

*/ - private String[] getLatestFile() throws IOException { + private String[] getLatestFile(String releaseType) throws IOException { String versionText = readFile(versionURL); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString(); diff --git a/src/main/resources/servertypes.csv b/src/main/resources/servertypes.csv index 535be3b..0e6ac59 100644 --- a/src/main/resources/servertypes.csv +++ b/src/main/resources/servertypes.csv @@ -6,6 +6,5 @@ Paper;1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.8;https://stati Bungee;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 Waterfall;Latest;https://papermc.io/api/v1/waterfall/1.16;"latest":";";https://papermc.io/api/v1/waterfall/1.16/ Travertine;Latest;https://papermc.io/api/v1/travertine/1.16;"latest":";";https://papermc.io/api/v1/travertine/1.16/ -Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/ -Vanilla;Latest,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,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/ +Vanilla;Latest,Snapshot,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,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/ Custom;; \ No newline at end of file diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java b/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java deleted file mode 100644 index b2afd3d..0000000 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.knarcraft.minecraftserverlauncher; - -import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; -import net.knarcraft.minecraftserverlauncher.utility.JarDownloader; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; - -public class DownloadTests { - - @Test - public void downloadJarsTest() throws IOException { - String targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "testjars" + File.separator; - JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory); - downloader.downloadJars(); - } -} diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java new file mode 100644 index 0000000..d466cfb --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java @@ -0,0 +1,38 @@ +package net.knarcraft.minecraftserverlauncher.server; + +import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import org.junit.Test; + +import javax.naming.ConfigurationException; + +import java.util.Arrays; + +import static junit.framework.TestCase.assertEquals; + +public class ServerTypeHandlerTest { + + @Test + public void getTypeNamesTest() throws ConfigurationException { + String[] serverTypeNames = ServerTypeHandler.getTypeNames(); + + String[] typeNames = new String[serverTypeNames.length]; + int index = 0; + for (ServerType serverType : ServerTypeHandler.getServerTypes()) { + typeNames[index++] = serverType.getName(); + } + assertEquals(Arrays.asList(typeNames), Arrays.asList(serverTypeNames)); + } + + @Test + public void getTypeFromNameTest() throws ConfigurationException { + ServerType targetType = null; + for (ServerType serverType : ServerTypeHandler.getServerTypes()) { + if (serverType.getName().equals("Vanilla")) { + targetType = serverType; + } + } + ServerType typeFromName = ServerTypeHandler.getByName("Vanilla"); + assertEquals(targetType, typeFromName); + } + +} diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java similarity index 74% rename from src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java rename to src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java index 61f95d5..6a74f0f 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/Tests.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctionsTest.java @@ -1,4 +1,4 @@ -package net.knarcraft.minecraftserverlauncher; +package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.profile.Profile; import org.junit.Test; @@ -8,16 +8,16 @@ import java.io.FileNotFoundException; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween; import static org.junit.Assert.assertEquals; -public class Tests { +public class CommonFunctionsTest { @Test - public void saveProfile() throws FileNotFoundException { //Make sure we can write profiles to disk + public void saveProfileTest() throws FileNotFoundException { Profile.addProfile("Test"); Profile.getCurrent().save(); } @Test - public void stringBetweenTest() { //Make sure stringBetween is not creating exceptions + public void stringBetweenTest() { String substring = stringBetween("fish'nchips", "f", "'"); assertEquals("ish", substring); substring = stringBetween("something", "whale", "fish"); diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java new file mode 100644 index 0000000..157eecb --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java @@ -0,0 +1,111 @@ +package net.knarcraft.minecraftserverlauncher.utility; + +import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler; +import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.naming.ConfigurationException; +import java.io.File; +import java.io.IOException; + +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +public class JarDownloaderTest { + private static String targetDirectory; + + @BeforeClass + public static void setUp() { + targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + + "testjars" + File.separator; + removeDownloadedFiles(); + } + + @AfterClass + public static void cleanUp() { + removeDownloadedFiles(); + } + + @Test + public void downloadJarsTest() throws IOException { + JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory); + downloader.downloadJars(); + } + + @Test + public void spongeVanillaDownloadTest() throws IOException, ConfigurationException { + singleDownloadTest(ServerTypeHandler.getByName("SpongeVanilla")); + } + + @Test + public void spigotDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Spigot")); + } + + @Test + public void mcpcPlusDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("MCPCplus")); + } + + @Test + public void craftbukkitDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Bukkit")); + } + + @Test + public void paperDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Paper")); + } + + @Test + public void bungeeDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Bungee")); + } + + @Test + public void waterfallDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Waterfall")); + } + + @Test + public void travertineDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Travertine")); + } + + @Test + public void vanillaDownloadTest() throws ConfigurationException, IOException { + singleDownloadTest(ServerTypeHandler.getByName("Vanilla")); + } + + /** + * Downloads all .jar files for a single server type and asserts it works + * + * @param serverType

The server type to test

+ * @throws IOException

If unable to download any of the .jar files

+ */ + private void singleDownloadTest(ServerType serverType) throws IOException { + assertNotNull(serverType); + for (String serverVersion : serverType.getVersions()) { + System.out.println("Downloading " + serverType.getName() + serverVersion + ".jar"); + serverType.downloadJar(targetDirectory, serverVersion); + assertTrue(new File(targetDirectory + serverType.getName() + serverVersion + ".jar").exists()); + } + } + + /** + * Removes downloaded test jars + */ + private static void removeDownloadedFiles() { + File[] oldFiles = new File(targetDirectory).listFiles(); + if (oldFiles == null) { + throw new IllegalArgumentException("Unable to list files in jar test directory"); + } + for (File file : oldFiles) { + assertTrue(file.delete()); + } + } +}