86 lines
2.4 KiB
Java
Raw Normal View History

2018-01-24 12:18:06 +01:00
import net.knarcraft.serverlauncher.server.*;
2018-01-25 17:34:45 +01:00
import javax.naming.ConfigurationException;
import java.io.*;
import java.lang.Runtime;
import java.util.ArrayList;
2018-01-25 17:34:45 +01:00
import java.util.Scanner;
//Java 9 required.
2018-01-24 12:18:06 +01:00
/**
* A software for managing Minecraft servers.
* @author Kristian Knarvik
*/
public class Main {
private static ArrayList<Server> servers = new ArrayList<>();
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
public static void main(String[] args) {
2018-01-25 17:34:45 +01:00
try {
loadServerTypes();
} catch (ConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
}
// TODO: Add gui
/**
* Adds all the valid server types and versions.
*/
2018-01-25 17:34:45 +01:00
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.");
}
}
/**
* 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");
}
}
}
}
}