From 043f2045e611bc63d8ae19d1ebc2b9cfe965e646 Mon Sep 17 00:00:00 2001
From: EpicKnarvik97 The regex pattern to use. The string to execute the pattern on. The first capture group if a match is found. An empty string otherwise. The regex pattern to use The string to execute the pattern on The first capture group if a match is found. An empty string otherwise The version of the downloaded BuildTools file The new version
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 IOExceptionIf 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 IOExceptionIf 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 outputLinesThe currently readable lines in the console output field.
+ * @param outputLinesThe 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 pathThe full url of the file to readFromServer
* @returnTrue if successful. False otherwise
+ * @throws IOExceptionIf 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 pathThe path of the file to read
+ * @returnThe contents of the file
+ * @throws IOExceptionIf 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(); }