From 043f2045e611bc63d8ae19d1ebc2b9cfe965e646 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 2 Aug 2021 16:09:22 +0200 Subject: [PATCH] Makes some improvements Saves the downloaded BuildTools version to the server version container Adds a check to see whether the BuildTools file exists Adds a helper function for reading local files --- .../profile/Controller.java | 4 +-- .../server/Server.java | 6 ++-- .../server/ServerVersionContainer.java | 30 +++++++++++++++++-- .../server/servertypes/BungeeCord.java | 4 +-- .../server/servertypes/SpongeVanilla.java | 2 +- .../server/servertypes/Vanilla.java | 8 ++--- .../server/servertypes/Waterfall.java | 4 +-- .../userinterface/Console.java | 6 ++-- .../utility/CommonFunctions.java | 14 ++++++++- .../utility/JarBuilder.java | 23 +++++++++----- .../utility/Updater.java | 4 +-- .../utility/JarBuilderTest.java | 3 +- 12 files changed, 77 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java index 4a1d82d..6365ba4 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java @@ -191,7 +191,7 @@ public class Controller { //Loads data regarding this controller String currentProfile = null; if (new File(mainFile).exists()) { - String controllerData = CommonFunctions.readBufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(mainFile)))); + String controllerData = CommonFunctions.readFile(mainFile); currentProfile = this.fromString(controllerData); } else { this.serverLauncherGUI = new ServerLauncherGUI(); @@ -274,7 +274,7 @@ public class Controller { try { CommonFunctions.createAllFolders(); PrintWriter mFile = new PrintWriter(mainFile); - mFile.println(this.toString()); + mFile.println(this); mFile.close(); PrintWriter pFile = new PrintWriter(profilesFile); for (Profile profile : profileList) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index 7f4addb..bc90bd2 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -532,9 +532,9 @@ public class Server { /** * Returns the first regex capture group found in a pattern - * @param pattern

The regex pattern to use.

- * @param text

The string to execute the pattern on.

- * @return

The first capture group if a match is found. An empty string otherwise.

+ * @param pattern

The regex pattern to use

+ * @param text

The string to execute the pattern on

+ * @return

The first capture group if a match is found. An empty string otherwise

*/ private String getFirstRegexCaptureGroup(String pattern, String text) { Pattern compiledPattern = Pattern.compile(pattern); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java index 23d7a7b..5380ea5 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java @@ -21,6 +21,7 @@ public class ServerVersionContainer { private Map travertineVersions; private Map spongeVanillaVersions; private Map spongeForgeVersions; + private String downloadedBuildToolsVersion; /** * Initializes a new server version container @@ -56,6 +57,7 @@ public class ServerVersionContainer { this.travertineVersions = new HashMap<>(); this.spongeVanillaVersions = new HashMap<>(); this.spongeForgeVersions = new HashMap<>(); + this.downloadedBuildToolsVersion = null; } @Override @@ -66,7 +68,8 @@ public class ServerVersionContainer { "waterfallVersions;" + mapToString(waterfallVersions) + "\n" + "travertineVersions;" + mapToString(travertineVersions) + "\n" + "spongeVanillaVersions;" + mapToString(spongeVanillaVersions) + "\n" + - "spongeForgeVersions;" + mapToString(spongeForgeVersions); + "spongeForgeVersions;" + mapToString(spongeForgeVersions) + "\n" + + "downloadedBuildToolsVersion;" + downloadedBuildToolsVersion; } /** @@ -105,7 +108,7 @@ public class ServerVersionContainer { } } file = new PrintWriter(versionFile); - file.println(this.toString()); + file.println(this); file.close(); } catch (IOException e) { e.printStackTrace(); @@ -163,6 +166,9 @@ public class ServerVersionContainer { case "spongeForgeVersions": parseVersionsToMap(spongeForgeVersions, variableValue); break; + case "downloadedBuildToolsVersion": + downloadedBuildToolsVersion = variableValue; + break; default: throw new IllegalArgumentException("Invalid key encountered in the server version file."); } @@ -323,5 +329,25 @@ public class ServerVersionContainer { spongeForgeVersions.put(mapKey, newValue); saveState(); } + + /** + * Gets the version of the downloaded BuildTools file + * + * @return

The version of the downloaded BuildTools file

+ */ + public String getDownloadedBuildToolsVersion() { + return this.downloadedBuildToolsVersion; + } + + /** + * Sets the version of the downloaded BuildTools file + * + * @param newValue

The new version

+ */ + public void setDownloadedBuildToolsVersion(String newValue) { + this.downloadedBuildToolsVersion = newValue; + saveState(); + } + } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java index 6aee13b..473543c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/BungeeCord.java @@ -7,7 +7,7 @@ import java.io.IOException; import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; -import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; +import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readRemoteFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween; /** @@ -65,7 +65,7 @@ public class BungeeCord extends AbstractServerType { private boolean downloadLatestJar(File filePath) throws IOException { ServerVersionContainer versionContainer = ServerVersionContainer.getInstance(); - String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); + String newestVersion = stringBetween(readRemoteFile(versionURL), srcStart, srcEnd); String oldVersion = versionContainer.getBungeeVersion(); //The file is already the newest version if (filePath.isFile() && newestVersion.equals(oldVersion)) { 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 8585f13..4d19d94 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java @@ -53,7 +53,7 @@ public class SpongeVanilla extends AbstractServerType { String file = this.getName() + version + ".jar"; File filePath = new File(folder + file); - String versionText = CommonFunctions.readFile(versionURL + version); + String versionText = CommonFunctions.readRemoteFile(versionURL + version); String newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd); String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar"; 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 9e05408..f722e42 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -12,7 +12,7 @@ import java.io.IOException; import java.nio.file.Paths; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; -import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; +import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readRemoteFile; /** * This class represents the regular vanilla Minecraft server type @@ -90,7 +90,7 @@ public class Vanilla extends AbstractServerType { * @throws IOException

If the remote resource cannot be readFromServer

*/ private String[] getLatestFile(String releaseType) throws IOException { - String versionText = readFile(versionURL); + String versionText = readRemoteFile(versionURL); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString(); String versionURL = getServerFileVersionURL(latest); @@ -106,7 +106,7 @@ public class Vanilla extends AbstractServerType { * @throws IOException

If the remote resource cannot be readFromServer

*/ private String getVanillaDownloadURL(String versionURL) throws IOException { - String versionText = readFile(versionURL); + String versionText = readRemoteFile(versionURL); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); return jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString(); } @@ -119,7 +119,7 @@ public class Vanilla extends AbstractServerType { * @throws IOException

If the file cannot be downloaded

*/ private String getServerFileVersionURL(String targetVersion) throws IOException { - String versionText = readFile(versionURL); + String versionText = readRemoteFile(versionURL); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); JsonArray availableVersions = jsonObject.getAsJsonArray("versions"); for (JsonElement availableVersion : availableVersions) { 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 13f644c..cb9f655 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Waterfall.java @@ -9,7 +9,7 @@ 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; +import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readRemoteFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween; /** @@ -50,7 +50,7 @@ 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 + version), srcStart, srcEnd); + String newestVersion = stringBetween(readRemoteFile(versionURL + version), srcStart, srcEnd); String fullURL = downloadURL + version + "/" + newestVersion + "/download"; String oldVersion = oldVersionFunction.apply(version); //The file is already the newest version diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java index 8e27080..4eaf35f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java @@ -77,7 +77,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { /** * Truncates the first 50 lines if the console output has reached the max limit - * @param outputLines

The currently readable lines in the console output field.

+ * @param outputLines

The currently readable lines in the console output field

*/ private void truncateConsole(int outputLines) { String oldText = this.textOutput.getText(); @@ -123,7 +123,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { * Shows the previously executed command in the input field * *

Shows the previously executed command if a command was just executed. - * Shows the command executed earlier if already showing a previously executed command.

+ * Shows the command executed earlier if already showing a previously executed command

*/ private void showPreviousCommand() { if (commands.size() > 0 && commandIndex > 0) { @@ -135,7 +135,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { * Shows the next previously executed command or clears the input field * *

Shows the next previously executed command if such a command exists. - * Clears the input field if no next used command exists.

+ * Clears the input field if no next used command exists

*/ private void showNextCommand() { if (commands.size() > 0) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index bcf385f..c16edb4 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -100,12 +100,24 @@ public final class CommonFunctions { * * @param path

The full url of the file to readFromServer

* @return

True if successful. False otherwise

+ * @throws IOException

If unable to find or read the file

*/ - public static String readFile(String path) throws IOException { + public static String readRemoteFile(String path) throws IOException { URL url = new URL(path); return new Scanner(url.openStream()).useDelimiter("\\Z").next(); } + /** + * Reads a file from disk + * + * @param path

The path of the file to read

+ * @return

The contents of the file

+ * @throws IOException

If unable to find or read the file

+ */ + public static String readFile(String path) throws IOException { + return CommonFunctions.readBufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(path)))); + } + /** * Downloads a file from a website and replaces the target file * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index e5d9926..ea3e57f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -1,6 +1,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -12,13 +13,14 @@ import java.nio.file.Paths; public class JarBuilder { - private String downloadedBuildToolsVersion = "#124"; private final String buildDirectory; private final String jarDirectory; + private final ServerVersionContainer versionContainer; - public JarBuilder(String buildDirectory, String jarDirectory) { + public JarBuilder(String buildDirectory, String jarDirectory, ServerVersionContainer versionContainer) { this.buildDirectory = buildDirectory; this.jarDirectory = jarDirectory; + this.versionContainer = versionContainer; } /** @@ -26,7 +28,7 @@ public class JarBuilder { */ public void buildSpigotJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\java.exe", "-jar", "BuildTools.jar", "--rev", + ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); System.out.println("Finished building spigot .jar. Moving it to the correct location"); @@ -39,7 +41,7 @@ public class JarBuilder { */ public void buildBukkitJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\java.exe", "-jar", "BuildTools.jar", "--compile", + ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile", "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); System.out.println("Finished building craftbukkit .jar. Moving it to the correct location"); @@ -53,11 +55,16 @@ public class JarBuilder { public void downloadBuildTools() { try { String latestVersion = getLatestBuildToolsVersion(); - if (!latestVersion.equals(downloadedBuildToolsVersion)) { - boolean success = CommonFunctions.downloadFile("https://hub.spigotmc.org/jenkins/job/BuildTools/" + - "lastSuccessfulBuild/artifact/target/BuildTools.jar", Paths.get(buildDirectory + "BuildTools.jar")); + boolean exists = new File(buildDirectory + "BuildTools.jar").exists(); + boolean isUpdated = latestVersion.equals(versionContainer.getDownloadedBuildToolsVersion()); + + if (!exists || !isUpdated) { + String buildToolsURL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar"; + boolean success = CommonFunctions.downloadFile(buildToolsURL, Paths.get(buildDirectory + "BuildTools.jar")); if (!success) { Main.getController().getGUI().setStatus("Unable to download the latest BuildTools"); + } else { + versionContainer.setDownloadedBuildToolsVersion(latestVersion); } } } catch (IOException e) { @@ -120,7 +127,7 @@ public class JarBuilder { */ String getLatestBuildToolsVersion() throws IOException { String versionDocument; - versionDocument = CommonFunctions.readFile("https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/"); + versionDocument = CommonFunctions.readRemoteFile("https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/"); return CommonFunctions.stringBetween(versionDocument, "BuildTools #", " ["); } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java index 2b14493..7fdef10 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java @@ -13,7 +13,7 @@ import java.util.Scanner; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner; -import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; +import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readRemoteFile; /** * A utility used for updating the software @@ -44,7 +44,7 @@ public final class Updater { String oldVer = file.nextLine(); file.close(); - String data = readFile(updateURL); + String data = readRemoteFile(updateURL); JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString(); diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java index 63d7039..42f3e2e 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java @@ -1,6 +1,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; @@ -26,7 +27,7 @@ public class JarBuilderTest { "BuildTools" + File.separator; jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "testjars" + File.separator; - jarBuilder = new JarBuilder(targetDirectory, jarDirectory); + jarBuilder = new JarBuilder(targetDirectory, jarDirectory, ServerVersionContainer.getInstance()); removeBuildToolsFiles(); }