Adds Advanced server type

The advanced server type allows vanilla, snapshot and sponge have
necessary methods and variables while keeping the others clean.
This commit is contained in:
Kristian Knarvik 2018-01-24 21:33:14 +01:00
parent 207427506e
commit 1ffed9310a
4 changed files with 82 additions and 14 deletions

View File

@ -27,7 +27,7 @@ public class Main {
//This could be changed to read from a list which is downloaded to the user's computer.
serverTypes.add(new ServerType("Vanilla", new String[]{"Latest", "1.12", "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"}));
serverTypes.add(new ServerType("Snapshot", new String[]{"Latest"}));
serverTypes.add(new ServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}));
serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-"));
serverTypes.add(new ServerType("Spigot", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}));
serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}));
serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}));

View File

@ -0,0 +1,38 @@
package net.knarcraft.serverlauncher.server;
public class AdvancedServerType extends ServerType {
private String versionURL;
private String downloadURL;
private String downloadURLPart;
private String srcStart;
private String srcEnd;
public AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) {
super(name, versions);
this.srcStart = srcStart;
this.srcEnd = srcEnd;
this.versionURL = versionURL;
this.downloadURL = downloadURL;
this.downloadURLPart = downloadURLPart;
}
public String getVersionURL() {
return this.versionURL;
}
public String getDownloadURL() {
return this.downloadURL;
}
public String getDownloadURLPart() {
return this.downloadURLPart;
}
public String getSrcStart() {
return this.srcStart;
}
public String getSrcEnd() {
return this.srcEnd;
}
}

View File

@ -11,16 +11,16 @@ import java.util.ArrayList;
import java.io.FileNotFoundException;
/* Contains all necessary information to create, run and manage a Minecraft server. */
public class Server {
private final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from.
private final String BUKKITURL = BASEURL + "/Bukkit/";
private final String MCPCURL = BASEURL + "/MCPC+/";
private final String SPIGOT = BASEURL + "/Spigot/";
private static final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from.
private static final String BUKKITURL = BASEURL + "/Bukkit/";
private static final String MCPCURL = BASEURL + "/MCPC+/";
private static final String SPIGOTURL = BASEURL + "/Spigot/";
private static String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private String name;
private String path;
private boolean enabled;
private ArrayList<String> playerList;
private ServerType type;
private AdvancedServerType type;
private String serverVersion;
private String maxRam;
private long pid;
@ -76,12 +76,41 @@ public class Server {
* Custom files must exist, or trigger a message.
*/
public void downloadJar() throws FileNotFoundException {
if (!new File(this.path + "\\" + this.type.getName() + this.serverVersion).isFile()) {
//TODO: Download a jar file based on version and type. Return true if the file was downloaded and ready to run.
throw new FileNotFoundException("");
if (!new File(this.path + "\\" + this.getType()).isFile()) {
switch (this.type.getName()) {
case "Custom":
throw new FileNotFoundException("Specified custom jar was not found.");
case "Spigot":
staticJar(SPIGOTURL);
break;
case "Craftbukkit":
staticJar(BUKKITURL);
break;
case "MCPCplus":
staticJar(MCPCURL);
break;
case "Vanilla":
if (this.serverVersion.equals("Latest")) {
} else {
}
break;
case "Sponge":
staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType());
}
//TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched.
}
}
private boolean staticJar(String url) {
return downloadFile(url + this.getType(), Paths.get(this.path + this.getType()));
}
private boolean staticJar(String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart, String outfile) {
return downloadFile(versionURL + this.getType(), Paths.get(this.path + this.getType()));
}
/**
* Reads the first line of a file from a website.
* This is used to find the newest version of the software.
@ -110,7 +139,7 @@ public class Server {
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
public static boolean downloadFile(String path, Path outfile) {
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL("https://knarcraft.net/Api/View/Jawns/Jawns%20instructions.txt");
InputStream in = url.openStream();
@ -121,4 +150,8 @@ public class Server {
return false;
}
}
private String stringBetween(String string, String start, String end) {
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
}
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.serverlauncher.server;
import java.net.MalformedURLException;
import java.net.URL;
/**
@ -8,10 +9,6 @@ import java.net.URL;
public class ServerType {
private String name;
private String[] versions;
private URL versionURL;
private URL downloadURL;
private String srcStart;
private String srcEnd;
public ServerType(String name, String[] versions) {
this.name = name;