Comment and code additions

More comments are added.
More work on downloading server jars is done.
This commit is contained in:
Kristian Knarvik 2018-01-24 22:31:48 +01:00
parent 1ffed9310a
commit 227b00d801
2 changed files with 42 additions and 25 deletions

View File

@ -25,10 +25,11 @@ public class Main {
*/
private static void addServerTypes() {
//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 AdvancedServerType("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"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"release\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server."));
serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server."));
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 AdvancedServerType("Bungee", new String[]{"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", ""));
serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "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"}));
serverTypes.add(new ServerType("Custom", new String[]{""}));

View File

@ -11,11 +11,14 @@ import java.util.ArrayList;
import java.io.FileNotFoundException;
/* Contains all necessary information to create, run and manage a Minecraft server. */
public class Server {
/** Necessary urls for downloading from knarcraft.net */
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"};
/** Available ram sizes. For GUI dropdown */
private static final 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;
@ -76,12 +79,17 @@ public class Server {
* Custom files must exist, or trigger a message.
*/
public void downloadJar() throws FileNotFoundException {
if (!new File(this.path + "\\" + this.getType()).isFile()) {
File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
switch (this.type.getName()) {
case "Custom":
if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
case "Spigot":
if (!file.isFile()) {
staticJar(SPIGOTURL);
}
break;
case "Craftbukkit":
staticJar(BUKKITURL);
@ -90,10 +98,21 @@ public class Server {
staticJar(MCPCURL);
break;
case "Vanilla":
case "Snapshot":
if (this.serverVersion.equals("Latest")) {
String versionText;
try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
String newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
boolean success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
} else {
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
}
break;
case "Sponge":
@ -101,7 +120,6 @@ public class Server {
}
//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()));
@ -112,24 +130,15 @@ public class Server {
}
/**
* Reads the first line of a file from a website.
* Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read.
* @return True if successfull. False otherwise.
*/
public static boolean readFile(String path) {
//We might need to change this to look for specific lines in the file to find vanilla and snapshot versions.
//The version should probably be returned. Empty or null on failiure.
try {
private static String readFile(String path) throws IOException {
URL url = new URL(path);
Scanner s = new Scanner(url.openStream());
System.out.println(s.next());
return true;
} catch (IOException e) {
System.out.println("Error");
return false;
}
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/**
@ -141,16 +150,23 @@ public class Server {
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL("https://knarcraft.net/Api/View/Jawns/Jawns%20instructions.txt");
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
System.out.println("Error");
return false;
}
}
/**
* Finds a substring between two substrings in a string.
*
* @param string The string containing the substrings.
* @param start The substring before the wanted substring.
* @param end The substring after the wanted substring.
* @return The wanted substring.
*/
private String stringBetween(String string, String start, String end) {
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
}