diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java index a5b16f1..a6aa25c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java @@ -17,8 +17,8 @@ public class ServerVersionContainer { private String vanillaVersion; private String snapshotVersion; private String bungeeVersion; - private String waterfallVersion; - private String travertineVersion; + private Map waterfallVersions; + private Map travertineVersions; private Map spongeVanillaVersions; private Map spongeForgeVersions; @@ -26,8 +26,10 @@ public class ServerVersionContainer { * Initializes a new server version container */ private ServerVersionContainer() { - spongeVanillaVersions = new HashMap<>(); - spongeForgeVersions = new HashMap<>(); + this.waterfallVersions = new HashMap<>(); + this.travertineVersions = new HashMap<>(); + this.spongeVanillaVersions = new HashMap<>(); + this.spongeForgeVersions = new HashMap<>(); loadState(); } @@ -50,8 +52,8 @@ public class ServerVersionContainer { this.vanillaVersion = null; this.snapshotVersion = null; this.bungeeVersion = null; - this.waterfallVersion = null; - this.travertineVersion = null; + this.waterfallVersions = new HashMap<>(); + this.travertineVersions = new HashMap<>(); this.spongeVanillaVersions = new HashMap<>(); this.spongeForgeVersions = new HashMap<>(); } @@ -61,8 +63,8 @@ public class ServerVersionContainer { return "vanillaVersion;" + vanillaVersion + "\n" + "snapshotVersion;" + snapshotVersion + "\n" + "bungeeVersion;" + bungeeVersion + "\n" + - "waterfallVersion;" + waterfallVersion + "\n" + - "travertineVersion;" + travertineVersion + "\n" + + "waterfallVersions;" + mapToString(waterfallVersions) + "\n" + + "travertineVersions;" + mapToString(travertineVersions) + "\n" + "spongeVanillaVersions;" + mapToString(spongeVanillaVersions) + "\n" + "spongeForgeVersions;" + mapToString(spongeForgeVersions); } @@ -151,17 +153,17 @@ public class ServerVersionContainer { case "bungeeVersion": bungeeVersion = variableValue; break; - case "waterfallVersion": - waterfallVersion = variableValue; + case "waterfallVersions": + parseVersionsToMap(waterfallVersions, variableValue); break; - case "travertineVersion": - travertineVersion = variableValue; + case "travertineVersions": + parseVersionsToMap(travertineVersions, variableValue); break; case "spongeVanillaVersions": - parseSpongeVersions(spongeVanillaVersions, variableValue); + parseVersionsToMap(spongeVanillaVersions, variableValue); break; case "spongeForgeVersions": - parseSpongeVersions(spongeForgeVersions, variableValue); + parseVersionsToMap(spongeForgeVersions, variableValue); break; default: throw new IllegalArgumentException("Invalid key encountered in the server version file."); @@ -169,12 +171,12 @@ public class ServerVersionContainer { } /** - * Reads sponge versions from a text string and updates the version map + * Reads versions from a text string and updates the version map * * @param targetMap

The map to update

* @param data

The data string to parse

*/ - private void parseSpongeVersions(Map targetMap, String data) { + private void parseVersionsToMap(Map targetMap, String data) { String[] versions = data.split(","); for (String version : versions) { String[] versionData = version.split("!"); @@ -241,40 +243,44 @@ public class ServerVersionContainer { } /** - * Gets the current waterfall version + * Gets a specific waterfall version * + * @param versionKey

The version to check current version of

* @return

The current waterfall version

*/ - public String getWaterfallVersion() { - return this.waterfallVersion; + public String getWaterfallVersion(String versionKey) { + return this.waterfallVersions.get(versionKey); } /** - * Sets the waterfall server version + * Sets the current version for a given waterfall version * - * @param newVersion

The new waterfall server version

+ * @param mapKey

The version key to set version for

+ * @param newValue

The new current version

*/ - public void setWaterfallVersion(String newVersion) { - this.waterfallVersion = newVersion; + public void setWaterfallVersion(String mapKey, String newValue) { + this.waterfallVersions.put(mapKey, newValue); saveState(); } /** - * Gets the current travertine server version + * Gets a specific travertine version * + * @param versionKey

The version to check current version of

* @return

The current travertine version

*/ - public String getTravertineVersion() { - return this.travertineVersion; + public String getTravertineVersion(String versionKey) { + return this.travertineVersions.get(versionKey); } /** - * Sets the travertine server version + * Sets the current version for a given travertine version * - * @param newVersion

The new travertine server version

+ * @param mapKey

The version key to set version for

+ * @param newValue

The new current version

*/ - public void setTravertineVersion(String newVersion) { - this.travertineVersion = newVersion; + public void setTravertineVersion(String mapKey, String newValue) { + this.travertineVersions.put(mapKey, newValue); saveState(); } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java index 87e993d..8585f13 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java @@ -7,7 +7,6 @@ import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.function.BiConsumer; -import java.util.function.Consumer; import java.util.function.Function; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java index b58f597..784a02d 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Travertine.java @@ -1,7 +1,5 @@ package net.knarcraft.minecraftserverlauncher.server.servertypes; -import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; - /** * This class represents the Travertine proxy server type */ @@ -21,7 +19,7 @@ public class Travertine extends Waterfall { public Travertine(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL) { super(typeName, isProxy, versions, versionURL, srcStart, srcEnd, downloadURL); - this.oldVersion = ServerVersionContainer.getInstance().getTravertineVersion(); + this.oldVersionFunction = serverVersionContainer::getTravertineVersion; this.versionUpdateFunction = serverVersionContainer::setTravertineVersion; } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java index 0676a1b..13f644c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java @@ -5,7 +5,8 @@ import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import java.io.File; import java.io.IOException; import java.nio.file.Paths; -import java.util.function.Consumer; +import java.util.function.BiConsumer; +import java.util.function.Function; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; @@ -19,8 +20,8 @@ public class Waterfall extends AbstractServerType { private final String srcStart; private final String srcEnd; private final String versionURL; - String oldVersion; - Consumer versionUpdateFunction; + Function oldVersionFunction; + BiConsumer versionUpdateFunction; final ServerVersionContainer serverVersionContainer; /** @@ -41,7 +42,7 @@ public class Waterfall extends AbstractServerType { this.srcStart = srcStart; this.srcEnd = srcEnd; this.versionURL = versionURL; - this.oldVersion = serverVersionContainer.getWaterfallVersion(); + this.oldVersionFunction = serverVersionContainer::getWaterfallVersion; this.versionUpdateFunction = serverVersionContainer::setWaterfallVersion; } @@ -49,17 +50,18 @@ public class Waterfall extends AbstractServerType { public boolean downloadJar(String folder, String version) throws IOException { String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); - String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); - String fullURL = downloadURL + newestVersion + "/download"; + String newestVersion = stringBetween(readFile(versionURL + version), srcStart, srcEnd); + String fullURL = downloadURL + version + "/" + newestVersion + "/download"; + String oldVersion = oldVersionFunction.apply(version); //The file is already the newest version - if (filePath.isFile() && newestVersion.equals(this.oldVersion)) { + if (filePath.isFile() && newestVersion.equals(oldVersion)) { return true; } //The new jar file could not be downloaded if (!downloadFile(fullURL, Paths.get(filePath.toURI()))) { return false; } - versionUpdateFunction.accept(newestVersion); + versionUpdateFunction.accept(version, newestVersion); return true; } } diff --git a/src/main/resources/servertypes.csv b/src/main/resources/servertypes.csv index f90a955..72e80a2 100644 --- a/src/main/resources/servertypes.csv +++ b/src/main/resources/servertypes.csv @@ -6,6 +6,6 @@ Craftbukkit;1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.8,1.7.10-R0.1,1.6.4-R2.0,1.5.2 SpongeForge;1.12.2,1.11.2,1.10.2;https://dl-api.spongepowered.org/v1/org.spongepowered/spongeforge/downloads?type=stable&minecraft=;"version":";",;https://repo.spongepowered.org/maven/org/spongepowered/spongeforge/;/spongeforge- MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://static.knarcraft.net/archive/downloads/minecraftserverjars/MCPC+/;mcpcplus Bungee;Latest,1.7.10,1.6.4,1.5.2,1.4.7;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;https://static.knarcraft.net/archive/downloads/minecraftserverjars/BungeeCord/;BungeeCord- -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/ +Waterfall;1.16,1.15,1.14,1.13,1.12,1.11;https://papermc.io/api/v1/waterfall/;"latest":";";https://papermc.io/api/v1/waterfall/ +Travertine;1.16,1.15,1.14,1.13,1.12;https://papermc.io/api/v1/travertine/;"latest":";";https://papermc.io/api/v1/travertine/ Custom;; \ No newline at end of file diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java index c60ece9..2a54a00 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java @@ -23,14 +23,19 @@ public class ServerVersionContainerTest { } catch (FileNotFoundException e) { e.printStackTrace(); } + File versionFileFile = new File(versionFile); + if (versionFileFile.exists() && !versionFileFile.delete()) { + throw new IllegalArgumentException("Unable to remove old version file."); + } serverVersionContainer = ServerVersionContainer.getInstance(); } @Test public void toStringTest() { serverVersionContainer.reset(); - assertEquals("vanillaVersion;null\nsnapshotVersion;null\nbungeeVersion;null\nwaterfallVersion;null\n" + - "travertineVersion;null\nspongeVanillaVersions;", + System.out.println(serverVersionContainer.toString()); + assertEquals("vanillaVersion;null\nsnapshotVersion;null\nbungeeVersion;null\nwaterfallVersions;\n" + + "travertineVersions;\nspongeVanillaVersions;\nspongeForgeVersions;", serverVersionContainer.toString()); }