Makes snapshot a vanilla version and loads all jars from the jars folder
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit

This commit is contained in:
2020-08-13 03:04:02 +02:00
parent ab6453cdc3
commit 70d064e590
12 changed files with 210 additions and 67 deletions

View File

@ -326,7 +326,11 @@ public class Profile implements java.io.Serializable {
ServerTab serverTab = collection.getServerTab();
server.setPath(serverTab.getPath());
server.setMaxRam(serverTab.getMaxRam());
server.setType(ServerTypeHandler.getByName(serverTab.getType()));
try {
server.setType(ServerTypeHandler.getByName(serverTab.getType()));
} catch (ConfigurationException e) {
e.printStackTrace();
}
try {
server.setServerVersion(serverTab.getVersion());
} catch (IllegalArgumentException e) {

View File

@ -5,6 +5,7 @@ import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import javax.naming.ConfigurationException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
@ -73,7 +74,7 @@ public class Server implements java.io.Serializable {
* @param serverVersion <p>The currently selected server version for the given server type</p>
* @param maxRam <p>The maximum amount of ram the server is allowed to use</p>
*/
public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) {
public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam) throws ConfigurationException {
this.name = name;
this.path = path;
this.enabled = enabled;
@ -133,7 +134,7 @@ public class Server implements java.io.Serializable {
* @param saveString <p>The string containing necessary data regarding the server</p>
* @return <p>A server in the same state it was saved in</p>
*/
public static Server fromString(String saveString) {
public static Server fromString(String saveString) throws ConfigurationException {
String[] data = saveString.split(";");
return new Server(data[0], data[1], Boolean.parseBoolean(data[2]), data[3], data[4], data[5]);
}
@ -385,7 +386,7 @@ public class Server implements java.io.Serializable {
} else {
serverFile = this.type.getName() + serverVersion + ".jar";
}
if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) {
if (!type.getName().equals("Custom")) {
serverPath = jarDirectory + serverFile;
} else {
serverPath = this.path + File.separator + serverFile;
@ -444,9 +445,11 @@ public class Server implements java.io.Serializable {
* @throws FileNotFoundException <p>If the file was not found and could not be acquired</p>
*/
private void downloadJar() throws IOException {
String path = this.path + File.separator;
String path;
if (this.type.getName().equals("Custom")) {
path += this.serverVersion;
path = this.path + File.separator + this.serverVersion;
} else {
path = jarDirectory;
}
File file = new File(path);
if (!(file.isFile() || type.downloadJar(path, this.serverVersion))) {

View File

@ -6,7 +6,6 @@ import net.knarcraft.minecraftserverlauncher.server.servertypes.Custom;
import net.knarcraft.minecraftserverlauncher.server.servertypes.MCPCPlus;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Paper;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Snapshot;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Spigot;
import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeVanilla;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Travertine;
@ -33,6 +32,9 @@ public class ServerTypeHandler {
* @return <p>A list of strings</p>
*/
public static String[] getTypeNames() throws ConfigurationException {
if (serverTypes.isEmpty()) {
loadServerTypes();
}
ArrayList<ServerType> types = getServerTypes();
String[] serverTypeNames = new String[types.size()];
for (int i = 0; i < types.size(); i++) {
@ -59,7 +61,10 @@ public class ServerTypeHandler {
* @param name <p>Then name of the server type</p>
* @return <p>A AbstractServerType</p>
*/
public static ServerType getByName(String name) {
public static ServerType getByName(String name) throws ConfigurationException {
if (serverTypes.isEmpty()) {
loadServerTypes();
}
for (ServerType serverType : serverTypes) {
if (serverType.getName().equals(name)) {
return serverType;
@ -117,10 +122,6 @@ public class ServerTypeHandler {
newType = new Vanilla("Vanilla", false, serverVersions, serverTypeInfo[2],
serverTypeInfo[3]);
break;
case "Snapshot":
newType = new Snapshot("Snapshot", false, serverVersions, serverTypeInfo[2],
serverTypeInfo[3]);
break;
case "MCPCplus":
newType = new MCPCPlus("MCPCplus", false, serverVersions, serverTypeInfo[2],
serverTypeInfo[3]);

View File

@ -5,6 +5,9 @@ import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
/**
* This class represents the CraftBukkit Minecraft server type
*/
public class CraftBukkit extends AbstractServerType {
private String downloadURLPart;

View File

@ -1,19 +0,0 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Snapshot extends Vanilla {
/**
* Instantiates a snapshot server type
*
* @param typeName <p>The name of this server type</p>
* @param isProxy <p>Whether this server type is a proxy server</p>
* @param versions <p>Available versions for this server type</p>
* @param versionURL <p>The URL used for downloading the version document</p>
* @param downloadURL <p>The URL used for downloading the new file</p>
*/
public Snapshot(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) {
super(typeName, isProxy, versions, versionURL, downloadURL);
this.releaseType = "snapshot";
}
}

View File

@ -13,11 +13,14 @@ import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
/**
* This class represents the regular vanilla server type
*/
public class Vanilla extends AbstractServerType {
private final String versionURL;
String releaseType;
private String lastVersion;
private String lastVanillaVersion;
private String lastSnapshotVersion;
/**
* Instantiates a vanilla server type
@ -31,33 +34,51 @@ public class Vanilla extends AbstractServerType {
public Vanilla(String typeName, boolean isProxy, String[] versions, String versionURL, String downloadURL) {
super(typeName, isProxy, versions, downloadURL);
this.versionURL = versionURL;
this.releaseType = "release";
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
String file = this.getName() + version + ".jar";
File filePath = new File(folder + file);
if (version.equals("Latest")) {
String[] latestData = getLatestFile();
String latest = latestData[0];
String jarFile = latestData[1];
String currentVersion = lastVersion;
lastVersion = latest;
return (filePath.isFile() && latest.equals(currentVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI()));
if (version.equals("Latest") || version.equals("Snapshot")) {
String releaseType = version.equals("Latest") ? "release" : "snapshot";
String lastVersion = version.equals("Latest") ? lastVanillaVersion : lastSnapshotVersion;
return downloadLatestJar(filePath, releaseType, lastVersion);
} else {
String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version));
return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI()));
}
}
/**
* Downloads the latest .jar file found if necessary
*
* @param filePath <p>The path of the jar file to download</p>
* @param releaseType <p>The release type used for downloading</p>
* @param lastVersion <p>The last server version found</p>
* @return <p>True if the jar exists and is the latest version or was downloaded</p>
* @throws IOException <p>If the .jar cannot be downloaded</p>
*/
private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion) throws IOException {
String[] latestData = getLatestFile(releaseType);
String latest = latestData[0];
String jarFile = latestData[1];
if (releaseType.equals("release")) {
lastVanillaVersion = latest;
} else {
lastSnapshotVersion = latest;
}
return (filePath.isFile() && latest.equals(lastVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI()));
}
/**
* Gets the URL to the .jar file for the newest version
*
* @param releaseType <p>The type of release to read latest version from</p>
* @return <p>An array containing the latest version and a link to its file</p>
* @throws IOException <p>If the remote resource cannot be readFromServer</p>
*/
private String[] getLatestFile() throws IOException {
private String[] getLatestFile(String releaseType) throws IOException {
String versionText = readFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString();