diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java index f598bf4..fab5f68 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java @@ -83,7 +83,7 @@ public class Main { Server server = collection.getServer(); if (server.isEnabled() && server.getProcess() != null) { try { - String readText = server.read(); + String readText = server.readFromServer(); if (!readText.equals("")) { collection.getServerConsole().output(readText); updatePlayerList(readText, server); @@ -117,7 +117,8 @@ public class Main { private static boolean serversRunning() { int num = 0; for (Collection collection : Profile.getCurrent().getCollections()) { - if (collection.getServer().isStarted() || (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) { + if (collection.getServer().isStarted() || + (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) { num++; } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index 0a5d638..f503693 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -48,6 +48,11 @@ public class Server { private final String bungeeVersion; private boolean started; + /** + * Initializes a new server with default values + * + * @param name
The name of the server
+ */ public Server(String name) { this.name = name; this.path = ""; @@ -65,6 +70,20 @@ public class Server { this.bungeeVersion = ""; } + /** + * Initializes a server with the given values + * + * @param nameThe name of the server
+ * @param pathThe file path of the folder containing the server files
+ * @param enabledWhether the server is enabled to start the next time servers are started
+ * @param typeNameThe name of the server type currently in use on the server
+ * @param serverVersionThe currently selected server version for the given server type
+ * @param maxRamThe maximum amount of ram the server is allowed to use
+ * @param vanillaVersionThe version of the "latest" downloaded vanilla version
+ * @param snapshotVersionThe version of the "latest" downloaded snapshot version
+ * @param spongeVanillaVersionThe version of the "latest" SpongeVanilla jar downloaded
+ * @param bungeeVersionThe version of the "latest" bungee jar downloaded
+ */ public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam, String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) { this.name = name; @@ -80,18 +99,38 @@ public class Server { this.playerList = new ArrayList<>(); } + /** + * Gets the name of the server + * + * @returnThe name of the server
+ */ public String getName() { return this.name; } + /** + * Whether the server has been started + * + * @returnTrue if the server has been started. False otherwise
+ */ public boolean isStarted() { return started; } + /** + * Gets the name of the server type used by this server + * + * @returnThe name of the server type used by this server
+ */ public String getTypeName() { return this.type.getName(); } + /** + * Gets the version used given server type used + * + * @returnThe server version given server type
+ */ public String getServerVersion() { return this.serverVersion; } @@ -255,76 +294,39 @@ public class Server { } /** - * Runs the Minecraft server. + * Runs a Minecraft server + * + * @returnTrue if nothing went wrong
*/ private boolean run() { - if (this.enabled) { - this.started = true; - if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) { - try { - Profile.getGUI().setStatus("Downloading jar..."); - this.downloadJar(); - Profile.getGUI().setStatus("File downloaded"); - } catch (IOException e) { - System.out.println(e.getMessage()); - Profile.getGUI().setStatus("Error: Jar file not found"); - e.printStackTrace(); - this.started = false; - return false; - } - } - try { - Profile.getGUI().setStatus("Delaying startup"); - TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup()); - } catch (InterruptedException e) { - e.printStackTrace(); - this.started = false; - return false; - } - try { - ProcessBuilder builder; - String serverPath; - if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) { - serverPath = jarDirectory + this.getType(); - } else { - serverPath = this.path + File.separator + this.getType(); - } - builder = new ProcessBuilder( - "java", - "-Xmx" + this.maxRam, - "-Xms512M", - "-Djline.terminal=jline.UnsupportedTerminal", - "-Dcom.mojang.eula.agree=true", - "-jar", - serverPath, - "nogui" - ); - builder.directory(new File(this.path)); - builder.redirectErrorStream(true); - this.process = builder.start(); - this.writer = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream())); - this.reader = new BufferedReader(new InputStreamReader(this.process.getInputStream())); - Profile.getGUI().setStatus("Servers are running"); - this.started = true; - return true; - } catch (IOException e) { - Profile.getGUI().setStatus("Could not start server"); - this.started = false; - return false; - } - } else { + if (!this.enabled) { this.started = false; return true; } + this.started = true; + if (!initializeJarDownload() || !delayStartup()) { + return false; + } + + try { + startServerProcess(); + Profile.getGUI().setStatus("Servers are running"); + this.started = true; + return true; + } catch (IOException e) { + Profile.getGUI().setStatus("Could not start server"); + this.started = false; + return false; + } } /** - * Reads all available output from the server process. + * Reads all available output from the server process * - * @return The server output - * @throws IOException If reading from the reader fails + * @returnThe server output
+ * @throws IOExceptionIf reading from the reader fails
*/ - public String read() throws IOException { + public String readFromServer() throws IOException { String line; StringBuilder text = new StringBuilder(); while (reader.ready() && (line = reader.readLine()) != null) { @@ -334,10 +336,77 @@ public class Server { } /** - * Downloads necessary .jar file for the server. - * This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves. + * Starts the process running this server * - * @throws FileNotFoundException if the file was not found and could not be acquired. + * @throws IOExceptionIf the process cannot be started
+ */ + private void startServerProcess() throws IOException { + ProcessBuilder builder; + String serverPath; + String serverFile; + if (type.getName().equals("Custom")) { + serverFile = serverVersion; + } else { + serverFile = this.type.getName() + serverVersion + ".jar"; + } + if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) { + serverPath = jarDirectory + serverFile; + } else { + serverPath = this.path + File.separator + serverFile; + } + builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M", + "-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath, + "nogui"); + builder.directory(new File(this.path)); + builder.redirectErrorStream(true); + this.process = builder.start(); + this.writer = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream())); + this.reader = new BufferedReader(new InputStreamReader(this.process.getInputStream())); + } + + /** + * Delays the server's startup for the given amount of time + * + * @returnTrue if the delay was successful
+ */ + private boolean delayStartup() { + try { + Profile.getGUI().setStatus("Delaying startup"); + TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup()); + return true; + } catch (InterruptedException e) { + e.printStackTrace(); + this.started = false; + return false; + } + } + + /** + * Starts downloading the necessary .jar file + * + * @returnTrue if nothing went wrong
+ */ + private boolean initializeJarDownload() { + if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) { + try { + Profile.getGUI().setStatus("Downloading jar..."); + this.downloadJar(); + Profile.getGUI().setStatus("File downloaded"); + } catch (IOException e) { + System.out.println(e.getMessage()); + Profile.getGUI().setStatus("Error: Jar file not found"); + e.printStackTrace(); + this.started = false; + return false; + } + } + return true; + } + + /** + * Downloads necessary .jar file for the server. + * + * @throws FileNotFoundExceptionIf the file was not found and could not be acquired
*/ private void downloadJar() throws IOException { String path = this.path + File.separator; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java index 67912d4..8668d66 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -54,7 +54,7 @@ public class Vanilla extends AbstractServerType { * Gets the URL to the .jar file for the newest version * * @returnAn array containing the latest version and a link to its file
- * @throws IOExceptionIf the remote resource cannot be read
+ * @throws IOExceptionIf the remote resource cannot be readFromServer
*/ private String[] getLatestFile() throws IOException { String versionText = readFile(versionURL); @@ -70,7 +70,7 @@ public class Vanilla extends AbstractServerType { * * @param versionURLThe URL to the version document describing the .jar file
* @returnThe URL necessary do download the .jar file
- * @throws IOExceptionIf the remote resource cannot be read
+ * @throws IOExceptionIf the remote resource cannot be readFromServer
*/ private String getVanillaDownloadURL(String versionURL) throws IOException { String versionText = readFile(versionURL); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index c5ccb5a..6674e1c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -2,7 +2,13 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -14,16 +20,16 @@ import java.util.Scanner; /** * A holding class for methods shared between classes. * - * @author Kristian KnarvikThe name of the resource you want to read
+ * @param resourceNameThe name of the resource you want to readFromServer
* @returnAn input stream which can be used to access the resource
*/ public static InputStream getResourceAsStream(String resourceName) { @@ -34,8 +40,8 @@ public class CommonFunctions { /** * Gets a resource as a Scanner * - * @param resourceNameThe name of the resource you want to read
- * @returnA scanner which can be used to read contents of the resource
+ * @param resourceNameThe name of the resource you want to readFromServer
+ * @returnA scanner which can be used to readFromServer contents of the resource
* @throws FileNotFoundExceptionIf the resource is not found
*/ public static Scanner getResourceAsScanner(String resourceName) throws FileNotFoundException { @@ -50,8 +56,8 @@ public class CommonFunctions { * Finds a substring between two substrings in a string * * @param stringThe string containing the substrings
- * @param startThe substring before the wanted substring
- * @param endThe substring after the wanted substring
+ * @param startThe substring before the wanted substring
+ * @param endThe substring after the wanted substring
* @returnThe wanted substring
*/ public static String stringBetween(String string, String start, String end) { @@ -67,7 +73,7 @@ public class CommonFunctions { * *This is used to find the newest version of jars and the software.
* - * @param pathThe full url of the file to read
+ * @param pathThe full url of the file to readFromServer
* @returnTrue if successful. False otherwise
*/ public static String readFile(String path) throws IOException { @@ -78,7 +84,7 @@ public class CommonFunctions { /** * Downloads a file from a website and replaces the target file * - * @param pathThe full url of the file to download
+ * @param pathThe full url of the file to download
* @param outfileThe file to save to
* @returnTrue if successful. False otherwise
*/ @@ -96,7 +102,7 @@ public class CommonFunctions { /** * Recursively copies a folder to another location * - * @param sourceThe folder to copy
+ * @param sourceThe folder to copy
* @param destinationTarget destination
* @throws IOExceptionIf we can't start a file stream
*/ @@ -124,8 +130,8 @@ public class CommonFunctions { * Copies a file from one location to another * * @param serverLauncherGuiThe serverLauncherGui to use for alerting the user
- * @param sourceThe file to copy
- * @param destinationThe location of the copied file
+ * @param sourceThe file to copy
+ * @param destinationThe location of the copied file
* @throws IOExceptionIf reading or writing fails
*/ private static void copyFile(ServerLauncherGUI serverLauncherGui, File source, File destination) throws IOException { @@ -144,6 +150,7 @@ public class CommonFunctions { /** * Opens an url in the user's default application. + * * @param urlThe URL to open
*/ public static void goToURL(String url) {