Cleanup
This commit is contained in:
parent
7843331605
commit
fdb02a6428
@ -1,86 +1,31 @@
|
||||
import net.knarcraft.serverlauncher.server.*;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.*;
|
||||
import java.lang.Runtime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
/**
|
||||
* @Version
|
||||
* @Java
|
||||
* @Requires
|
||||
*/
|
||||
//Java 9 required.
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
private static ArrayList<Server> servers = new ArrayList<>();
|
||||
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
setup();
|
||||
}
|
||||
|
||||
public static void setup() {
|
||||
try {
|
||||
loadServerTypes();
|
||||
ServerType.loadServerTypes();
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
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 {
|
||||
private String versionURL;
|
||||
private String downloadURLPart;
|
||||
|
@ -7,12 +7,20 @@ import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.*;
|
||||
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 {
|
||||
/**
|
||||
* 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 ArrayList<Server> servers = new ArrayList<>();
|
||||
|
||||
private String name;
|
||||
private String path;
|
||||
@ -32,6 +40,7 @@ public class Server {
|
||||
this.serverVersion = null;
|
||||
this.maxRam = ramList[0];
|
||||
this.pid = -1;
|
||||
servers.add(this);
|
||||
}
|
||||
|
||||
public void addPlayer(String name) {
|
||||
@ -47,20 +56,12 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
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 static ArrayList<Server> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
@ -78,39 +79,50 @@ public class Server {
|
||||
/**
|
||||
* 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();
|
||||
for (String version : versions) {
|
||||
if (version.equals(serverVersion)) {
|
||||
this.serverVersion = serverVersion;
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid server version.");
|
||||
}
|
||||
|
||||
public void setMaxRam(String 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.
|
||||
*/
|
||||
public void run() {
|
||||
if (this.isEnabled()) {
|
||||
private void run() {
|
||||
if (this.enabled) {
|
||||
try {
|
||||
System.out.println("Downloading jar...");
|
||||
this.downloadJar();
|
||||
System.out.println("File downloaded.");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("File was not found.");
|
||||
return;
|
||||
}
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
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));
|
||||
this.pid = pr.pid();
|
||||
System.out.println("Success");
|
||||
@ -135,6 +147,7 @@ public class Server {
|
||||
boolean success;
|
||||
switch (this.type.getName()) {
|
||||
case "Custom":
|
||||
//TODO: Change Custom to use path as serverdir + input.
|
||||
if (!file.isFile()) {
|
||||
throw new FileNotFoundException("Specified custom jar was not found.");
|
||||
}
|
||||
@ -144,6 +157,7 @@ public class Server {
|
||||
case "MCPCplus":
|
||||
if (!file.isFile()) {
|
||||
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) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
|
@ -1,17 +1,29 @@
|
||||
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 {
|
||||
private final String name;
|
||||
private final String[] versions;
|
||||
private final String downloadURL;
|
||||
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
|
||||
|
||||
public ServerType(String name, String[] versions, String downloadURL) {
|
||||
this.name = name;
|
||||
this.versions = versions;
|
||||
this.downloadURL = downloadURL;
|
||||
serverTypes.add(this);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -25,4 +37,43 @@ public class ServerType {
|
||||
public String getDownloadURL() {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +1,35 @@
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import net.knarcraft.serverlauncher.server.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
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 {
|
||||
private static ArrayList<Server> servers = new ArrayList<>();
|
||||
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
loadServerTypes();
|
||||
ServerType.loadServerTypes();
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
Server server1 = new Server("Server1");
|
||||
new Server("Server1");
|
||||
Server server1 = Server.getServers().get(0);
|
||||
server1.toggle();
|
||||
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
|
||||
server1.setType(serverTypes.get(4));
|
||||
//TODO: All types are inside serverTypes, but the ones of ServerType get a casting error.
|
||||
server1.setServerVersion("1.12.2");
|
||||
server1.setType(ServerType.getServerTypes().get(6));
|
||||
server1.setServerVersion("1.6.4");
|
||||
server1.setMaxRam("1G");
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user