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:
Kristian Knarvik 2020-08-13 03:04:02 +02:00
parent ab6453cdc3
commit 70d064e590
12 changed files with 210 additions and 67 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.knarcraft.minecraftserverlauncher</groupId>
<artifactId>minecraft-server-launcher</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>

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());
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();

View File

@ -6,6 +6,5 @@ Paper;1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.8;https://stati
Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar
Waterfall;Latest;https://papermc.io/api/v1/waterfall/1.16;"latest":";";https://papermc.io/api/v1/waterfall/1.16/
Travertine;Latest;https://papermc.io/api/v1/travertine/1.16;"latest":";";https://papermc.io/api/v1/travertine/1.16/
Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/
Vanilla;Latest,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/
Vanilla;Latest,Snapshot,1.16.1,1.15.2,1.14.4,1.13.2,1.12.2,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/
Custom;;
Can't render this file because it contains an unexpected character in line 1 and column 151.

View File

@ -1,18 +0,0 @@
package net.knarcraft.minecraftserverlauncher;
import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI;
import net.knarcraft.minecraftserverlauncher.utility.JarDownloader;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class DownloadTests {
@Test
public void downloadJarsTest() throws IOException {
String targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "testjars" + File.separator;
JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory);
downloader.downloadJars();
}
}

View File

@ -0,0 +1,38 @@
package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import org.junit.Test;
import javax.naming.ConfigurationException;
import java.util.Arrays;
import static junit.framework.TestCase.assertEquals;
public class ServerTypeHandlerTest {
@Test
public void getTypeNamesTest() throws ConfigurationException {
String[] serverTypeNames = ServerTypeHandler.getTypeNames();
String[] typeNames = new String[serverTypeNames.length];
int index = 0;
for (ServerType serverType : ServerTypeHandler.getServerTypes()) {
typeNames[index++] = serverType.getName();
}
assertEquals(Arrays.asList(typeNames), Arrays.asList(serverTypeNames));
}
@Test
public void getTypeFromNameTest() throws ConfigurationException {
ServerType targetType = null;
for (ServerType serverType : ServerTypeHandler.getServerTypes()) {
if (serverType.getName().equals("Vanilla")) {
targetType = serverType;
}
}
ServerType typeFromName = ServerTypeHandler.getByName("Vanilla");
assertEquals(targetType, typeFromName);
}
}

View File

@ -1,4 +1,4 @@
package net.knarcraft.minecraftserverlauncher;
package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import org.junit.Test;
@ -8,16 +8,16 @@ import java.io.FileNotFoundException;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween;
import static org.junit.Assert.assertEquals;
public class Tests {
public class CommonFunctionsTest {
@Test
public void saveProfile() throws FileNotFoundException { //Make sure we can write profiles to disk
public void saveProfileTest() throws FileNotFoundException {
Profile.addProfile("Test");
Profile.getCurrent().save();
}
@Test
public void stringBetweenTest() { //Make sure stringBetween is not creating exceptions
public void stringBetweenTest() {
String substring = stringBetween("fish'nchips", "f", "'");
assertEquals("ish", substring);
substring = stringBetween("something", "whale", "fish");

View File

@ -0,0 +1,111 @@
package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.naming.ConfigurationException;
import java.io.File;
import java.io.IOException;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
public class JarDownloaderTest {
private static String targetDirectory;
@BeforeClass
public static void setUp() {
targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"testjars" + File.separator;
removeDownloadedFiles();
}
@AfterClass
public static void cleanUp() {
removeDownloadedFiles();
}
@Test
public void downloadJarsTest() throws IOException {
JarDownloader downloader = new JarDownloader(new FakeGUI(), targetDirectory);
downloader.downloadJars();
}
@Test
public void spongeVanillaDownloadTest() throws IOException, ConfigurationException {
singleDownloadTest(ServerTypeHandler.getByName("SpongeVanilla"));
}
@Test
public void spigotDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Spigot"));
}
@Test
public void mcpcPlusDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("MCPCplus"));
}
@Test
public void craftbukkitDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Bukkit"));
}
@Test
public void paperDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Paper"));
}
@Test
public void bungeeDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Bungee"));
}
@Test
public void waterfallDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Waterfall"));
}
@Test
public void travertineDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Travertine"));
}
@Test
public void vanillaDownloadTest() throws ConfigurationException, IOException {
singleDownloadTest(ServerTypeHandler.getByName("Vanilla"));
}
/**
* Downloads all .jar files for a single server type and asserts it works
*
* @param serverType <p>The server type to test</p>
* @throws IOException <p>If unable to download any of the .jar files</p>
*/
private void singleDownloadTest(ServerType serverType) throws IOException {
assertNotNull(serverType);
for (String serverVersion : serverType.getVersions()) {
System.out.println("Downloading " + serverType.getName() + serverVersion + ".jar");
serverType.downloadJar(targetDirectory, serverVersion);
assertTrue(new File(targetDirectory + serverType.getName() + serverVersion + ".jar").exists());
}
}
/**
* Removes downloaded test jars
*/
private static void removeDownloadedFiles() {
File[] oldFiles = new File(targetDirectory).listFiles();
if (oldFiles == null) {
throw new IllegalArgumentException("Unable to list files in jar test directory");
}
for (File file : oldFiles) {
assertTrue(file.delete());
}
}
}