Comment and code additions

More comments are added.
More work on downloading server jars is done.
Server test added.
This commit is contained in:
Kristian Knarvik 2018-01-25 10:53:51 +01:00
parent 227b00d801
commit b6685edc6f
5 changed files with 258 additions and 149 deletions

View File

@ -29,10 +29,10 @@ public class Main {
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 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[]{""}));
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"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/"));
serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/"));
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"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/"));
serverTypes.add(new ServerType("Custom", new String[]{""}, ""));
}
/**

View File

@ -2,17 +2,15 @@ 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);
super(name, versions, downloadURL);
this.srcStart = srcStart;
this.srcEnd = srcEnd;
this.versionURL = versionURL;
this.downloadURL = downloadURL;
this.downloadURLPart = downloadURLPart;
}
@ -20,10 +18,6 @@ public class AdvancedServerType extends ServerType {
return this.versionURL;
}
public String getDownloadURL() {
return this.downloadURL;
}
public String getDownloadURLPart() {
return this.downloadURLPart;
}

View File

@ -9,165 +9,205 @@ import java.nio.file.StandardCopyOption;
import java.nio.file.*;
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/";
/** 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"};
/**
* 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;
private ArrayList<String> playerList;
private AdvancedServerType type;
private String serverVersion;
private String maxRam;
private long pid;
public Server(String name) {
this.name = name;
this.path = "";
this.enabled = false;
this.playerList = new ArrayList<>();
this.type = null;
this.serverVersion = null;
this.maxRam = ramList[0];
this.pid = -1;
}
public void addPlayer(String name) {
this.playerList.add(name);
}
public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) {
if (name.equals(playerList.get(i))) {
playerList.remove(i);
return;
}
}
}
public String getPath() {
return this.path;
}
public String getType() {
return this.type.getName() + this.serverVersion + ".jar";
}
public boolean isEnabled() {
return this.enabled;
}
public String maxRam() {
return this.maxRam;
}
public void updatePid(long pid) {
this.pid = pid;
}
private String name;
private String path;
private boolean enabled;
private ArrayList<String> playerList;
private AdvancedServerType type;
private String serverVersion;
private String maxRam;
private long pid;
public Server(String name) {
this.name = name;
this.path = "";
this.enabled = false;
this.playerList = new ArrayList<>();
this.type = null;
this.serverVersion = null;
this.maxRam = ramList[0];
this.pid = -1;
}
public void addPlayer(String name) {
this.playerList.add(name);
}
public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) {
if (name.equals(playerList.get(i))) {
playerList.remove(i);
return;
}
}
}
public String getPath() {
return this.path;
}
public String getType() {
return this.type.getName() + this.serverVersion + ".jar";
}
public boolean isEnabled() {
return this.enabled;
}
public String maxRam() {
return this.maxRam;
}
public void toggle() {
this.enabled = !this.enabled;
}
public void setPath(String path) {
this.path = path;
}
public void setType(AdvancedServerType type) {
this.type = type;
}
public void setServerVersion(String serverVersion) {
this.serverVersion = serverVersion;
}
public void setMaxRam(String ram) {
this.maxRam = ram;
}
public void updatePid(long pid) {
this.pid = pid;
}
/**
* Downloads necessary .jar file for the server.
* All server versions are downloaded if missing.
* Newest server versions (snapshot, vanilla and bungee) need to be checked against an online json file and downloaded if outdated.
* Custom files must exist, or trigger a message.
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
*
* @throws FileNotFoundException if the file was not found and could not be acquired.
*/
public void downloadJar() throws FileNotFoundException {
File file = new File(this.path + "\\" + this.getType());
public void downloadJar() throws FileNotFoundException {
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.");
String versionText;
String newestVersion;
boolean success;
switch (this.type.getName()) {
case "Custom":
if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
break;
case "Spigot":
case "Craftbukkit":
case "MCPCplus":
if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + type.getName() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
case "Spigot":
if (!file.isFile()) {
staticJar(SPIGOTURL);
}
break;
case "Vanilla":
case "Snapshot":
if (this.serverVersion.equals("Latest")) {
try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
break;
case "Craftbukkit":
staticJar(BUKKITURL);
break;
case "MCPCplus":
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);
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
} else {
if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".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":
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.
}
break;
case "Sponge":
try {
versionText = readFile(this.type.getVersionURL() + this.serverVersion);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
break;
case "Bungee":
/*try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());*/
success = downloadFile(type.getDownloadURL(), filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
}
private boolean staticJar(String url) {
return downloadFile(url + this.getType(), Paths.get(this.path + this.getType()));
/**
* 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.
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
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()));
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
/**
* 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.
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
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.
* @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) {
private String stringBetween(String string, String start, String end) {
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
}
}

View File

@ -7,12 +7,14 @@ import java.net.URL;
* Has a name and contains a list of valid server versions.
*/
public class ServerType {
private String name;
private String[] versions;
private final String name;
private final String[] versions;
private final String downloadURL;
public ServerType(String name, String[] versions) {
public ServerType(String name, String[] versions, String downloadURL) {
this.name = name;
this.versions = versions;
this.downloadURL = downloadURL;
}
public String getName() {
@ -22,4 +24,8 @@ public class ServerType {
public String[] getVersions() {
return this.versions;
}
public String getDownloadURL() {
return this.downloadURL;
}
}

69
test/ServerTest.java Normal file
View File

@ -0,0 +1,69 @@
import net.knarcraft.serverlauncher.server.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class ServerTest {
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
private static ArrayList<Server> servers = new ArrayList<>();
public static void main(String[] args) {
addServerTypes();
Server server1 = new Server("Server1");
server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType((AdvancedServerType) serverTypes.get(0));
server1.setServerVersion("Latest");
server1.setMaxRam("1G");
servers.add(server1);
startServers(servers);
}
/**
* Adds all the valid server types and versions.
*/
private static void addServerTypes() {
//This could be changed to read from a list which is downloaded to the user's computer.
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 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"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/"));
serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/"));
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"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/"));
serverTypes.add(new ServerType("Custom", new String[]{""}, ""));
}
/**
* Runs all enabled servers with their settings.
*/
public static void startServers(ArrayList<Server> servers) {
System.out.println("Starting servers.");
for (Server server : servers) {
if (server.isEnabled()) {
String path = server.getPath();
String type = server.getType();
try {
server.downloadJar();
System.out.println("File downloaded.");
} catch (FileNotFoundException e) {
System.out.println("File was not found.");
return;
}
String ram = server.maxRam();
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("java -Xmx" + ram + " -Xms512M -jar " + "\"" + path + "\\" + type + "\" nogui");
long pid = pr.pid();
server.updatePid(pid);
System.out.println("Success");
} catch (IOException e) {
System.out.println("Error");
}
} else {
System.out.println("Server disabled");
}
}
}
}