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

View File

@ -532,9 +532,9 @@ public class Server {
/**
* Returns the first regex capture group found in a pattern
* @param pattern <p>The regex pattern to use.</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>
* @param pattern <p>The regex pattern to use</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>
*/
private String getFirstRegexCaptureGroup(String pattern, String text) {
Pattern compiledPattern = Pattern.compile(pattern);

View File

@ -21,6 +21,7 @@ public class ServerVersionContainer {
private Map<String, String> travertineVersions;
private Map<String, String> spongeVanillaVersions;
private Map<String, String> 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 <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 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)) {

View File

@ -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";

View File

@ -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 <p>If the remote resource cannot be readFromServer</p>
*/
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 <p>If the remote resource cannot be readFromServer</p>
*/
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 <p>If the file cannot be downloaded</p>
*/
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) {

View File

@ -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