import net.knarcraft.serverlauncher.server.*; import javax.naming.ConfigurationException; import java.io.*; import java.lang.Runtime; import java.util.ArrayList; import java.util.Scanner; //Java 9 required. /** * A software for managing Minecraft servers. * @author Kristian Knarvik */ public class Main { private static ArrayList servers = new ArrayList<>(); private static ArrayList serverTypes = new ArrayList<>(); public static void main(String[] args) { try { loadServerTypes(); } catch (ConfigurationException e) { e.printStackTrace(); System.exit(1); } } // TODO: Add gui /** * 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."); } } /** * 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"); } } } } }