This commit is contained in:
Kristian Knarvik 2018-01-25 21:17:02 +01:00
parent 7843331605
commit fdb02a6428
5 changed files with 117 additions and 135 deletions

View File

@ -1,86 +1,31 @@
import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.server.*;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import java.io.*; /**
import java.lang.Runtime; * @Version
import java.util.ArrayList; * @Java
import java.util.Scanner; * @Requires
*/
//Java 9 required. //Java 9 required.
/** /**
* A software for managing Minecraft servers. * A software for managing Minecraft servers.
* @author Kristian Knarvik *
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/ */
public class Main { public class Main {
private static ArrayList<Server> servers = new ArrayList<>();
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
public static void main(String[] args) { public static void main(String[] args) {
setup();
}
public static void setup() {
try { try {
loadServerTypes(); ServerType.loadServerTypes();
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
} }
// TODO: Add gui // TODO: Add gui
/**
* Runs all enabled servers with their settings.
*/
public static void startServers() {
//This should update the server status to running. The output should display in a custom commandline interface.
for (Server server : servers) {
if (server.isEnabled()) {
String path = server.getPath();
String type = server.getType();
try {
server.downloadJar();
} 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");
}
}
}
}
/**
* Adds all the valid server types and versions.
*/
private static void loadServerTypes() throws ConfigurationException {
try (Scanner in = new Scanner(new File("config/servertypes.csv"))) {
while (in.hasNextLine()) {
String[] str = in.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
if (len == 7) {
serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]));
} else if (len == 3) {
serverTypes.add(new ServerType(str[0], ver, str[2]));
} else {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} }

View File

@ -1,5 +1,12 @@
package net.knarcraft.serverlauncher.server; package net.knarcraft.serverlauncher.server;
/**
* A more advanced servertype for particularly tricky jar downloads.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class AdvancedServerType extends ServerType { public class AdvancedServerType extends ServerType {
private String versionURL; private String versionURL;
private String downloadURLPart; private String downloadURLPart;

View File

@ -7,12 +7,20 @@ import java.nio.file.StandardCopyOption;
import java.nio.file.*; import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
/* Contains all necessary information to create, run and manage a Minecraft server. */
/**
* Contains all necessary information to create, run and manage a Minecraft server.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Server { public class Server {
/** /**
* Available ram sizes. For GUI dropdown * 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 static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private static ArrayList<Server> servers = new ArrayList<>();
private String name; private String name;
private String path; private String path;
@ -32,6 +40,7 @@ public class Server {
this.serverVersion = null; this.serverVersion = null;
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.pid = -1; this.pid = -1;
servers.add(this);
} }
public void addPlayer(String name) { public void addPlayer(String name) {
@ -47,20 +56,12 @@ public class Server {
} }
} }
public String getPath() {
return this.path;
}
public String getType() { public String getType() {
return this.type.getName() + this.serverVersion + ".jar"; return this.type.getName() + this.serverVersion + ".jar";
} }
public boolean isEnabled() { public static ArrayList<Server> getServers() {
return this.enabled; return servers;
}
public String maxRam() {
return this.maxRam;
} }
public void toggle() { public void toggle() {
@ -78,39 +79,50 @@ public class Server {
/** /**
* Sets the server's server version to a valid version, or ignores the request. * Sets the server's server version to a valid version, or ignores the request.
* *
* @param serverVersion New version number. * @param serverVersion Version number.
*/ */
public void setServerVersion(String serverVersion) { public void setServerVersion(String serverVersion) throws IllegalArgumentException {
String[] versions = this.type.getVersions(); String[] versions = this.type.getVersions();
for (String version : versions) { for (String version : versions) {
if (version.equals(serverVersion)) { if (version.equals(serverVersion)) {
this.serverVersion = serverVersion; this.serverVersion = serverVersion;
return;
} }
} }
throw new IllegalArgumentException("Invalid server version.");
} }
public void setMaxRam(String ram) { public void setMaxRam(String ram) {
this.maxRam = ram; this.maxRam = ram;
} }
public void updatePid(long pid) { /**
this.pid = pid; * Runs all enabled servers with their settings.
*/
public static void startServers() {
System.out.println("Starting servers.");
for (Server server : servers) {
server.run();
}
} }
/** /**
* Runs the Minecraft server. * Runs the Minecraft server.
*/ */
public void run() { private void run() {
if (this.isEnabled()) { if (this.enabled) {
try { try {
System.out.println("Downloading jar...");
this.downloadJar(); this.downloadJar();
System.out.println("File downloaded."); System.out.println("File downloaded.");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File was not found."); System.out.println("File was not found.");
return; return;
} }
Runtime rt = Runtime.getRuntime(); Runtime rt = Runtime.getRuntime();
try { try {
System.out.println("Executing command.");
Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path)); Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path));
this.pid = pr.pid(); this.pid = pr.pid();
System.out.println("Success"); System.out.println("Success");
@ -135,6 +147,7 @@ public class Server {
boolean success; boolean success;
switch (this.type.getName()) { switch (this.type.getName()) {
case "Custom": case "Custom":
//TODO: Change Custom to use path as serverdir + input.
if (!file.isFile()) { if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found."); throw new FileNotFoundException("Specified custom jar was not found.");
} }
@ -144,6 +157,7 @@ public class Server {
case "MCPCplus": case "MCPCplus":
if (!file.isFile()) { if (!file.isFile()) {
success = downloadFile(this.type.getDownloadURL() + this.type.getName() + this.serverVersion + ".jar", filePath); success = downloadFile(this.type.getDownloadURL() + this.type.getName() + this.serverVersion + ".jar", filePath);
System.out.println(this.type.getDownloadURL() + this.type.getName() + this.serverVersion + ".jar");
if (!success) { if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded."); throw new FileNotFoundException("Jar file could not be downloaded.");
} }

View File

@ -1,17 +1,29 @@
package net.knarcraft.serverlauncher.server; package net.knarcraft.serverlauncher.server;
import javax.naming.ConfigurationException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/** /**
* Has a name and contains a list of valid server versions. * Contains the bare minimum to be a functional servertype.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/ */
public class ServerType { public class ServerType {
private final String name; private final String name;
private final String[] versions; private final String[] versions;
private final String downloadURL; private final String downloadURL;
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
public ServerType(String name, String[] versions, String downloadURL) { public ServerType(String name, String[] versions, String downloadURL) {
this.name = name; this.name = name;
this.versions = versions; this.versions = versions;
this.downloadURL = downloadURL; this.downloadURL = downloadURL;
serverTypes.add(this);
} }
public String getName() { public String getName() {
@ -25,4 +37,43 @@ public class ServerType {
public String getDownloadURL() { public String getDownloadURL() {
return this.downloadURL; return this.downloadURL;
} }
public static ArrayList<ServerType> getServerTypes() {
return serverTypes;
}
/**
* Reads valid server types and version from a file, and creates their objects.
*
* @throws ConfigurationException if anything goes wrong.
*/
public static void loadServerTypes() throws ConfigurationException {
if (serverTypes.isEmpty()) {
try (Scanner in = new Scanner(new File("config/servertypes.csv"))) {
while (in.hasNextLine()) {
String[] str = in.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
if (len == 7) {
new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]);
} else if (len == 3) {
new ServerType(str[0], ver, str[2]);
} else {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Configuration file invalid.");
}
} else {
throw new ConfigurationException("Error: Configuration already loaded.");
}
}
} }

View File

@ -1,70 +1,35 @@
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.server.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
/**
* A class for testing new and existing features.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class ServerTest { public class ServerTest {
private static ArrayList<Server> servers = new ArrayList<>(); private static ArrayList<Server> servers = new ArrayList<>();
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
public static void main(String[] args) { public static void main(String[] args) {
try { try {
loadServerTypes(); ServerType.loadServerTypes();
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
Server server1 = new Server("Server1"); new Server("Server1");
Server server1 = Server.getServers().get(0);
server1.toggle(); server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType(serverTypes.get(4)); server1.setType(ServerType.getServerTypes().get(6));
//TODO: All types are inside serverTypes, but the ones of ServerType get a casting error. server1.setServerVersion("1.6.4");
server1.setServerVersion("1.12.2");
server1.setMaxRam("1G"); server1.setMaxRam("1G");
servers.add(server1); servers.add(server1);
startServers(servers); Server.startServers();
} }
/**
* Runs all enabled servers with their settings.
*/
private static void startServers(ArrayList<Server> servers) {
System.out.println("Starting servers.");
for (Server server : servers) {
server.run();
}
}
/**
* Adds all the valid server types and versions.
*/
private static void loadServerTypes() throws ConfigurationException {
try (Scanner in = new Scanner(new File("config/servertypes.csv"))) {
while (in.hasNextLine()) {
String[] str = in.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
if (len == 7) {
serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]));
} else if (len == 3) {
serverTypes.add(new ServerType(str[0], ver, str[2]));
} else {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} }