Makes some improvements
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

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
This commit is contained in:
Kristian Knarvik 2021-08-02 16:09:22 +02:00
parent 51ceac69da
commit 043f2045e6
12 changed files with 77 additions and 31 deletions

View File

@ -191,7 +191,7 @@ public class Controller {
//Loads data regarding this controller //Loads data regarding this controller
String currentProfile = null; String currentProfile = null;
if (new File(mainFile).exists()) { 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); currentProfile = this.fromString(controllerData);
} else { } else {
this.serverLauncherGUI = new ServerLauncherGUI(); this.serverLauncherGUI = new ServerLauncherGUI();
@ -274,7 +274,7 @@ public class Controller {
try { try {
CommonFunctions.createAllFolders(); CommonFunctions.createAllFolders();
PrintWriter mFile = new PrintWriter(mainFile); PrintWriter mFile = new PrintWriter(mainFile);
mFile.println(this.toString()); mFile.println(this);
mFile.close(); mFile.close();
PrintWriter pFile = new PrintWriter(profilesFile); PrintWriter pFile = new PrintWriter(profilesFile);
for (Profile profile : profileList) { for (Profile profile : profileList) {

View File

@ -532,9 +532,9 @@ public class Server {
/** /**
* Returns the first regex capture group found in a pattern * Returns the first regex capture group found in a pattern
* @param pattern <p>The regex pattern to use.</p> * @param pattern <p>The regex pattern to use</p>
* @param text <p>The string to execute the pattern on.</p> * @param text <p>The string to execute the pattern on</p>
* @return <p>The first capture group if a match is found. An empty string otherwise.</p> * @return <p>The first capture group if a match is found. An empty string otherwise</p>
*/ */
private String getFirstRegexCaptureGroup(String pattern, String text) { private String getFirstRegexCaptureGroup(String pattern, String text) {
Pattern compiledPattern = Pattern.compile(pattern); Pattern compiledPattern = Pattern.compile(pattern);

View File

@ -21,6 +21,7 @@ public class ServerVersionContainer {
private Map<String, String> travertineVersions; private Map<String, String> travertineVersions;
private Map<String, String> spongeVanillaVersions; private Map<String, String> spongeVanillaVersions;
private Map<String, String> spongeForgeVersions; private Map<String, String> spongeForgeVersions;
private String downloadedBuildToolsVersion;
/** /**
* Initializes a new server version container * Initializes a new server version container
@ -56,6 +57,7 @@ public class ServerVersionContainer {
this.travertineVersions = new HashMap<>(); this.travertineVersions = new HashMap<>();
this.spongeVanillaVersions = new HashMap<>(); this.spongeVanillaVersions = new HashMap<>();
this.spongeForgeVersions = new HashMap<>(); this.spongeForgeVersions = new HashMap<>();
this.downloadedBuildToolsVersion = null;
} }
@Override @Override
@ -66,7 +68,8 @@ public class ServerVersionContainer {
"waterfallVersions;" + mapToString(waterfallVersions) + "\n" + "waterfallVersions;" + mapToString(waterfallVersions) + "\n" +
"travertineVersions;" + mapToString(travertineVersions) + "\n" + "travertineVersions;" + mapToString(travertineVersions) + "\n" +
"spongeVanillaVersions;" + mapToString(spongeVanillaVersions) + "\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 = new PrintWriter(versionFile);
file.println(this.toString()); file.println(this);
file.close(); file.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -163,6 +166,9 @@ public class ServerVersionContainer {
case "spongeForgeVersions": case "spongeForgeVersions":
parseVersionsToMap(spongeForgeVersions, variableValue); parseVersionsToMap(spongeForgeVersions, variableValue);
break; break;
case "downloadedBuildToolsVersion":
downloadedBuildToolsVersion = variableValue;
break;
default: default:
throw new IllegalArgumentException("Invalid key encountered in the server version file."); throw new IllegalArgumentException("Invalid key encountered in the server version file.");
} }
@ -323,5 +329,25 @@ public class ServerVersionContainer {
spongeForgeVersions.put(mapKey, newValue); spongeForgeVersions.put(mapKey, newValue);
saveState(); saveState();
} }
/**
* Gets the version of the downloaded BuildTools file
*
* @return <p>The version of the downloaded BuildTools file</p>
*/
public String getDownloadedBuildToolsVersion() {
return this.downloadedBuildToolsVersion;
}
/**
* Sets the version of the downloaded BuildTools file
*
* @param newValue <p>The new version</p></p>
*/
public void setDownloadedBuildToolsVersion(String newValue) {
this.downloadedBuildToolsVersion = newValue;
saveState();
}
} }

View File

@ -7,7 +7,7 @@ import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; 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; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween;
/** /**
@ -65,7 +65,7 @@ public class BungeeCord extends AbstractServerType {
private boolean downloadLatestJar(File filePath) throws IOException { private boolean downloadLatestJar(File filePath) throws IOException {
ServerVersionContainer versionContainer = ServerVersionContainer.getInstance(); ServerVersionContainer versionContainer = ServerVersionContainer.getInstance();
String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); String newestVersion = stringBetween(readRemoteFile(versionURL), srcStart, srcEnd);
String oldVersion = versionContainer.getBungeeVersion(); String oldVersion = versionContainer.getBungeeVersion();
//The file is already the newest version //The file is already the newest version
if (filePath.isFile() && newestVersion.equals(oldVersion)) { if (filePath.isFile() && newestVersion.equals(oldVersion)) {

View File

@ -53,7 +53,7 @@ public class SpongeVanilla extends AbstractServerType {
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); 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 newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd);
String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar"; String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar";

View File

@ -12,7 +12,7 @@ import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; 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 * This class represents the regular vanilla Minecraft server type
@ -90,7 +90,7 @@ public class Vanilla extends AbstractServerType {
* @throws IOException <p>If the remote resource cannot be readFromServer</p> * @throws IOException <p>If the remote resource cannot be readFromServer</p>
*/ */
private String[] getLatestFile(String releaseType) throws IOException { private String[] getLatestFile(String releaseType) throws IOException {
String versionText = readFile(versionURL); String versionText = readRemoteFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString(); String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString();
String versionURL = getServerFileVersionURL(latest); String versionURL = getServerFileVersionURL(latest);
@ -106,7 +106,7 @@ public class Vanilla extends AbstractServerType {
* @throws IOException <p>If the remote resource cannot be readFromServer</p> * @throws IOException <p>If the remote resource cannot be readFromServer</p>
*/ */
private String getVanillaDownloadURL(String versionURL) throws IOException { private String getVanillaDownloadURL(String versionURL) throws IOException {
String versionText = readFile(versionURL); String versionText = readRemoteFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
return jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString(); return jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
} }
@ -119,7 +119,7 @@ public class Vanilla extends AbstractServerType {
* @throws IOException <p>If the file cannot be downloaded</p> * @throws IOException <p>If the file cannot be downloaded</p>
*/ */
private String getServerFileVersionURL(String targetVersion) throws IOException { private String getServerFileVersionURL(String targetVersion) throws IOException {
String versionText = readFile(versionURL); String versionText = readRemoteFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
JsonArray availableVersions = jsonObject.getAsJsonArray("versions"); JsonArray availableVersions = jsonObject.getAsJsonArray("versions");
for (JsonElement availableVersion : availableVersions) { for (JsonElement availableVersion : availableVersions) {

View File

@ -9,7 +9,7 @@ import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; 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; 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 { public boolean downloadJar(String folder, String version) throws IOException {
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); 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 fullURL = downloadURL + version + "/" + newestVersion + "/download";
String oldVersion = oldVersionFunction.apply(version); String oldVersion = oldVersionFunction.apply(version);
//The file is already the newest version //The file is already the newest version

View File

@ -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 * Truncates the first 50 lines if the console output has reached the max limit
* @param outputLines <p>The currently readable lines in the console output field.</p> * @param outputLines <p>The currently readable lines in the console output field</p>
*/ */
private void truncateConsole(int outputLines) { private void truncateConsole(int outputLines) {
String oldText = this.textOutput.getText(); 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 in the input field
* *
* <p>Shows the previously executed command if a command was just executed. * <p>Shows the previously executed command if a command was just executed.
* Shows the command executed earlier if already showing a previously executed command.</p> * Shows the command executed earlier if already showing a previously executed command</p>
*/ */
private void showPreviousCommand() { private void showPreviousCommand() {
if (commands.size() > 0 && commandIndex > 0) { 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 or clears the input field
* *
* <p>Shows the next previously executed command if such a command exists. * <p>Shows the next previously executed command if such a command exists.
* Clears the input field if no next used command exists.</p> * Clears the input field if no next used command exists</p>
*/ */
private void showNextCommand() { private void showNextCommand() {
if (commands.size() > 0) { if (commands.size() > 0) {

View File

@ -100,12 +100,24 @@ public final class CommonFunctions {
* *
* @param path <p>The full url of the file to readFromServer</p> * @param path <p>The full url of the file to readFromServer</p>
* @return <p>True if successful. False otherwise</p> * @return <p>True if successful. False otherwise</p>
* @throws IOException <p>If unable to find or read the file</p>
*/ */
public static String readFile(String path) throws IOException { public static String readRemoteFile(String path) throws IOException {
URL url = new URL(path); URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next(); return new Scanner(url.openStream()).useDelimiter("\\Z").next();
} }
/**
* Reads a file from disk
*
* @param path <p>The path of the file to read</p>
* @return <p>The contents of the file</p>
* @throws IOException <p>If unable to find or read the file</p>
*/
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 * Downloads a file from a website and replaces the target file
* *

View File

@ -1,6 +1,7 @@
package net.knarcraft.minecraftserverlauncher.utility; package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -12,13 +13,14 @@ import java.nio.file.Paths;
public class JarBuilder { public class JarBuilder {
private String downloadedBuildToolsVersion = "#124";
private final String buildDirectory; private final String buildDirectory;
private final String jarDirectory; 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.buildDirectory = buildDirectory;
this.jarDirectory = jarDirectory; this.jarDirectory = jarDirectory;
this.versionContainer = versionContainer;
} }
/** /**
@ -26,7 +28,7 @@ public class JarBuilder {
*/ */
public void buildSpigotJar() { public void buildSpigotJar() {
downloadBuildTools(); 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); "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); executeBuildProcess(processBuilder);
System.out.println("Finished building spigot .jar. Moving it to the correct location"); System.out.println("Finished building spigot .jar. Moving it to the correct location");
@ -39,7 +41,7 @@ public class JarBuilder {
*/ */
public void buildBukkitJar() { public void buildBukkitJar() {
downloadBuildTools(); 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); "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); executeBuildProcess(processBuilder);
System.out.println("Finished building craftbukkit .jar. Moving it to the correct location"); System.out.println("Finished building craftbukkit .jar. Moving it to the correct location");
@ -53,11 +55,16 @@ public class JarBuilder {
public void downloadBuildTools() { public void downloadBuildTools() {
try { try {
String latestVersion = getLatestBuildToolsVersion(); String latestVersion = getLatestBuildToolsVersion();
if (!latestVersion.equals(downloadedBuildToolsVersion)) { boolean exists = new File(buildDirectory + "BuildTools.jar").exists();
boolean success = CommonFunctions.downloadFile("https://hub.spigotmc.org/jenkins/job/BuildTools/" + boolean isUpdated = latestVersion.equals(versionContainer.getDownloadedBuildToolsVersion());
"lastSuccessfulBuild/artifact/target/BuildTools.jar", Paths.get(buildDirectory + "BuildTools.jar"));
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) { if (!success) {
Main.getController().getGUI().setStatus("Unable to download the latest BuildTools"); Main.getController().getGUI().setStatus("Unable to download the latest BuildTools");
} else {
versionContainer.setDownloadedBuildToolsVersion(latestVersion);
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -120,7 +127,7 @@ public class JarBuilder {
*/ */
String getLatestBuildToolsVersion() throws IOException { String getLatestBuildToolsVersion() throws IOException {
String versionDocument; 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 #", " ["); return CommonFunctions.stringBetween(versionDocument, "BuildTools #", " [");
} }

View File

@ -13,7 +13,7 @@ import java.util.Scanner;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner; 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 * A utility used for updating the software
@ -44,7 +44,7 @@ public final class Updater {
String oldVer = file.nextLine(); String oldVer = file.nextLine();
file.close(); file.close();
String data = readFile(updateURL); String data = readRemoteFile(updateURL);
JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString(); String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();

View File

@ -1,6 +1,7 @@
package net.knarcraft.minecraftserverlauncher.utility; package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@ -26,7 +27,7 @@ public class JarBuilderTest {
"BuildTools" + File.separator; "BuildTools" + File.separator;
jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"testjars" + File.separator; "testjars" + File.separator;
jarBuilder = new JarBuilder(targetDirectory, jarDirectory); jarBuilder = new JarBuilder(targetDirectory, jarDirectory, ServerVersionContainer.getInstance());
removeBuildToolsFiles(); removeBuildToolsFiles();
} }