From 040740db844b25e8dea3ab541d5c77b82969fc3f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 13 Feb 2020 21:10:18 +0100 Subject: [PATCH] Adds comments and refactors several classes Adds comments to Collection Makes some variable and function names more descriptive Adds some new methods for showing messsages and errors Adds a lot of missing comments Enhances some existing comments --- .../minecraftserverlauncher/Main.java | 57 ++- .../minecraftserverlauncher/Shared.java | 59 ++- .../profile/Collection.java | 43 ++- .../profile/Profile.java | 361 ++++++++++-------- .../server/AdvancedServerType.java | 26 ++ .../server/Server.java | 30 +- .../server/ServerType.java | 69 ++-- .../userinterface/Console.java | 46 ++- .../userinterface/GUI.java | 144 ++----- .../userinterface/ServerConsoles.java | 31 +- .../DownloadTests.java | 6 +- 11 files changed, 489 insertions(+), 383 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java index 36ea613..e2d65db 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java @@ -32,14 +32,14 @@ import static net.knarcraft.minecraftserverlauncher.Shared.*; */ public class Main { - private static String appDir; - private static boolean running = false; + private static String applicationWorkDirectory; + private static boolean serversAreRunning = false; private static final String updateChannel = "alpha"; private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher"; public static void main(String[] args) throws IOException { checkForUpdate(); - try (PrintWriter file = new PrintWriter(Main.getAppDir() + File.separator + "latestrun.log")) { + try (PrintWriter file = new PrintWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log")) { file.print(""); } catch (IOException e ) { e.printStackTrace(); @@ -74,16 +74,16 @@ public class Main { * * @return A string path */ - public static String getAppDir() { - if (appDir == null) { + public static String getApplicationWorkDirectory() { + if (applicationWorkDirectory == null) { try { - appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile()); + applicationWorkDirectory = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile()); } catch (URISyntaxException e) { e.printStackTrace(); System.exit(1); } } - return appDir; + return applicationWorkDirectory; } /** @@ -109,13 +109,13 @@ public class Main { } } boolean runningNew = serversRunning(); - if (!runningNew && running) { + if (!runningNew && serversAreRunning) { Profile.getGUI().updateRunning(false); Profile.getGUI().setStatus("Servers are stopped"); - } else if (runningNew && !running) { + } else if (runningNew && !serversAreRunning) { Profile.getGUI().updateRunning(true); } - running = runningNew; + serversAreRunning = runningNew; } catch (Exception e) { e.printStackTrace(); } @@ -203,7 +203,7 @@ public class Main { String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString(); if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) { - String dir = getAppDir() + File.separator; + String dir = getApplicationWorkDirectory() + File.separator; JsonArray versionList = jsonObject.getAsJsonArray("versions"); String url = ""; for (JsonElement elem : versionList) { @@ -217,12 +217,8 @@ public class Main { if (downloadFile(url, new File(dir + "update.jar").toPath())) { doUpdate(dir); } else { - JOptionPane.showMessageDialog( - null, - "An update is available, but could not be downloaded.", - "Update available", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("Update available", + "An update is available, but could not be downloaded."); } } } @@ -235,31 +231,24 @@ public class Main { JOptionPane.YES_NO_OPTION ); if (answer == JOptionPane.YES_NO_OPTION) { - if (new File (dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir + "Old.jar"))) { - if (new File(dir + "update.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) { + if (new File (dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir + + "Old.jar"))) { + if (new File(dir + "update.jar").renameTo(new File (dir + + "Minecraft-Server-Launcher.jar"))) { if (new File(dir + "Old.jar").delete()) { - - JOptionPane.showMessageDialog( - null, - "Update finished. Please run the software again.", - "Update complete", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("Update complete", + "Update finished. Please run the software again."); System.exit(0); } } else { - if (!new File(dir + "Old.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) { + if (!new File(dir + "Old.jar").renameTo(new File (dir + + "Minecraft-Server-Launcher.jar"))) { System.out.println("Shit"); } } } - JOptionPane.showMessageDialog( - null, - "Could not replace the main .jar with the downloaded .jar.", - "Update failed", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("Update failed", + "Could not replace the main .jar with the downloaded .jar."); } - } } \ No newline at end of file diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Shared.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Shared.java index 85b6e35..8bc0d91 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Shared.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Shared.java @@ -1,8 +1,10 @@ package net.knarcraft.minecraftserverlauncher; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import net.knarcraft.minecraftserverlauncher.userinterface.GUI; + +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -86,4 +88,55 @@ public class Shared { return false; } } + + /** + * Recursivly copies a folder to another location + * + * @param src

The folder to copy

+ * @param dest

Target destination

+ * @throws IOException

If we can't start a file stream

+ */ + public static void copyFolder(GUI gui, File src, File dest) throws IOException { + if (!src.isDirectory()) { + InputStream in = new FileInputStream(src); + OutputStream out = new FileOutputStream(dest); + byte[] buffer = new byte[1024]; + int length; + while ((length = in.read(buffer)) > 0){ + out.write(buffer, 0, length); + } + in.close(); + out.close(); + gui.setStatus("Copied file " + src); + } else { + if(!dest.exists()){ + if (dest.mkdir()) { + gui.setStatus("Copied directory " + src); + } else { + return; + } + } + String[] files = src.list(); + if (files != null) { + for (String file : files) { + File srcFile = new File(src, file); + File destFile = new File(dest, file); + copyFolder(gui, srcFile, destFile); + } + } + } + } + + /** + * Opens an url in the user's default application. + * @param url

URL to open

+ */ + public static void goToURL(String url) { + java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); + try { + desktop.browse(new URI(url)); + } catch (URISyntaxException | IOException e1) { + e1.printStackTrace(); + } + } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Collection.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Collection.java index b61215a..b0ea6c8 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Collection.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Collection.java @@ -18,15 +18,32 @@ public class Collection { private final Console serverConsole; private final String name; + /** + * Creates a new collection with the given name + * @param name

The name identifying the server, server tab, collection and server console

+ */ Collection(String name) { this.serverTab = new ServerTab(name); this.server = new Server(name); - this.serverConsole = ServerConsoles.addTab(name); + this.serverConsole = ServerConsoles.addConsoleTab(name); this.name = name; } + /** + * Creates a new collection with the given parameters + * @param name

The name identifying the server, server tab, collection and server console

+ * @param serverPath

The path of the server folder

+ * @param enabled

Whether the server should be run when starting servers

+ * @param typeName

The name of the server type the server uses

+ * @param serverVersion

The version of the running server type.

+ * @param maxRam

The maximum amount of RAM the server is allowed to use.

+ * @param vanillaVersion

The currently selected vanilla version

+ * @param snapshotVersion

The currently selected snapshot version

+ * @param spongeVanillaVersion

The currently selected SpongeVanilla version

+ * @param bungeeVersion

The currently selected Bungee version

+ */ Collection(String name, - String path, + String serverPath, boolean enabled, String typeName, String serverVersion, @@ -39,7 +56,7 @@ public class Collection { this.serverTab = new ServerTab(name); this.server = new Server( name, - path, + serverPath, enabled, typeName, serverVersion, @@ -49,23 +66,39 @@ public class Collection { spongeVanillaVersion, bungeeVersion ); - this.serverConsole = ServerConsoles.addTab(name); + this.serverConsole = ServerConsoles.addConsoleTab(name); this.name = name; - this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam); + this.serverTab.setData(serverPath, enabled, typeName, serverVersion, maxRam); } + /** + * Gets the name of the collection + * @return

Collection name

+ */ public String getName() { return this.name; } + /** + * Gets the server of the collection + * @return

Collection server

+ */ public Server getServer() { return this.server; } + /** + * Gets the server tab of the collection + * @return

Collection server tab

+ */ public ServerTab getServerTab() { return this.serverTab; } + /** + * Gets the server console of the collection + * @return

Collection server console

+ */ public Console getServerConsole() { return this.serverConsole; } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java index b37cd22..22a4353 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java @@ -36,15 +36,15 @@ public class Profile { private static final ArrayList profiles = new ArrayList<>(); private static Profile current; private static GUI gui; - private static final String profilesDir = Main.getAppDir() + File.separator + "files"; - private static final String profilesFile = Main.getAppDir() + File.separator + "files" + File.separator + "Profiles.txt"; - private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator; + private static final String profilesDir = Main.getApplicationWorkDirectory() + File.separator + "files"; + private static final String profilesFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Profiles.txt"; + private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator; private final ArrayList collections; private final String name; private boolean runInBackground; private int delayStartup; - private boolean downloadJars; + private boolean downloadAllAvailableJARFiles; private static String vanillaVersion; private static String snapshotVersion; private static String bungeeVersion; @@ -54,19 +54,19 @@ public class Profile { this.name = name; this.runInBackground = false; this.delayStartup = 0; - this.downloadJars = false; + this.downloadAllAvailableJARFiles = false; profiles.add(this); if (current == null) { current = this; } } - private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadJars) { + private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadAllAvailableJARFiles) { this.collections = new ArrayList<>(); this.name = name; this.runInBackground = runInBackground; this.delayStartup = delayStartup; - this.downloadJars = downloadJars; + this.downloadAllAvailableJARFiles = downloadAllAvailableJARFiles; profiles.add(this); if (current == null) { current = this; @@ -85,8 +85,8 @@ public class Profile { return this.delayStartup; } - public boolean getDownloadJars() { - return this.downloadJars; + public boolean getDownloadAllAvailableJARFiles() { + return this.downloadAllAvailableJARFiles; } public static Profile getCurrent() { @@ -139,8 +139,8 @@ public class Profile { } } - public void setDownloadJars(boolean value) { - this.downloadJars = value; + public void setDownloadAllAvailableJARFiles(boolean value) { + this.downloadAllAvailableJARFiles = value; } /** @@ -175,13 +175,8 @@ public class Profile { ) { collections.add(new Collection(name)); } else { - JOptionPane.showMessageDialog( - null, - "A server name must my unique and not empty or \"All\"." + - "It can't contain any of the characters \"!\", \"?\" or \";\".", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("A server name must my unique and not empty or \"All\"." + + "It can't contain any of the characters \"!\", \"?\" or \";\"."); } } @@ -195,22 +190,12 @@ public class Profile { return; } if (name.equals("") && !name.matches("^[!?;]+$")) { - JOptionPane.showMessageDialog( - null, - "Profile name can't be blank.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Profile name can't be blank."); return; } for (Profile profile : profiles) { if (profile.name.equals(name)) { - JOptionPane.showMessageDialog( - null, - "There is already a profile with this name.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("There is already a profile with this name."); return; } } @@ -227,6 +212,10 @@ public class Profile { } } + /** + * Removes a profile with the given name from the list of profiles, if such a profile exists + * @param name

The name of the profile to rempve

+ */ public static void removeProfile(String name) { if (profiles.size() > 1) { profiles.removeIf(profile -> profile.name.equals(name)); @@ -234,7 +223,7 @@ public class Profile { } public void updateConsoles() { - JTabbedPane consolesTab = ServerConsoles.getTab(); + JTabbedPane consolesTab = ServerConsoles.getTabbedPane(); consolesTab.removeAll(); for (Collection collection : collections) { consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); @@ -253,12 +242,7 @@ public class Profile { try { collection.getServer().sendCommand(command); } catch (IOException e) { - JOptionPane.showMessageDialog( - null, - "Server " + collection.getName() + " caused an exception.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Server " + collection.getName() + " caused an exception."); } } } else { @@ -268,19 +252,10 @@ public class Profile { try { target.sendCommand(command); } catch (IOException e) { - JOptionPane.showMessageDialog( - null, - "Server " + target.getName() + " caused an exception.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Server " + target.getName() + " caused an exception."); } } else { - JOptionPane.showMessageDialog( - null, - "Server " + serverName + " is invalid.", - "Error", JOptionPane.ERROR_MESSAGE - ); + showError("Server " + serverName + " is invalid."); } } } @@ -299,33 +274,32 @@ public class Profile { try { server.setServerVersion(serverTab.getVersion()); } catch (IllegalArgumentException e) { - JOptionPane.showMessageDialog( - null, - "Invalid server version for " + server.getName(), - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Invalid server version for " + server.getName()); } server.toggle(serverTab.enabled()); } if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) { - JOptionPane.showMessageDialog( - null, - "Unable to create the folder " + profilesDir, - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Unable to create the folder " + profilesDir); throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir); } try (PrintWriter file = new PrintWriter(profilesFile)) { + int width; + int height; + if (gui == null) { + width = 440; + height = 170; + } else { + width = gui.getSize().width; + height = gui.getSize().height; + } file.println(String.format( "%s;%s;%s;%s;%d;%d", current.name, vanillaVersion, snapshotVersion, bungeeVersion, - gui.getSize().width, - gui.getSize().height + width, + height )); file.close(); for (Profile profile : profiles) { @@ -334,7 +308,7 @@ public class Profile { profile.name, profile.runInBackground, profile.delayStartup, - profile.downloadJars) + profile.downloadAllAvailableJARFiles) ); for (Collection collection : profile.collections) { Server server = collection.getServer(); @@ -361,26 +335,16 @@ public class Profile { fileAppend.println(saveString); } catch (IOException e) { if (gui != null) { - JOptionPane.showMessageDialog( - null, - "Unable to save to file. Try running the software as an administrator.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Unable to save to file. Try running the software as an administrator."); } else { System.out.println("Unable to save to file. Try running the software as an administrator."); } throw new FileNotFoundException("Unable to save to the profiles file."); } } - } catch (IOException | NullPointerException e) { + } catch (IOException e) { if (gui != null) { - JOptionPane.showMessageDialog( - null, - "Unable to save to file. Try running the software as an administrator.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Unable to save to file. Try running the software as an administrator."); } throw new FileNotFoundException("Unable to create the profiles file"); } @@ -424,12 +388,8 @@ public class Profile { current = getProfile(profileName); } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { e.printStackTrace(); - JOptionPane.showMessageDialog( - null, - "Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please manually delete the file.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + showError("Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please " + + "manually delete the file."); System.exit(1); } catch (IOException e) { e.printStackTrace(); @@ -438,21 +398,11 @@ public class Profile { addProfile("Default"); } } catch (FileNotFoundException | NoSuchElementException e) { - JOptionPane.showMessageDialog( - null, - "A profiles file was not found. Default profile was created.", - "Info", - JOptionPane.INFORMATION_MESSAGE - ); + showMessage("A profiles file was not found. Default profile was created."); try { gui = new GUI(); } catch (FileNotFoundException ex) { - JOptionPane.showMessageDialog( - null, - "Failed to load GUI messages. The GUI can't be shown.", - "Info", - JOptionPane.INFORMATION_MESSAGE - ); + showMessage("Failed to load GUI messages. The GUI can't be shown."); } catch (IOException ex) { ex.printStackTrace(); } @@ -461,7 +411,7 @@ public class Profile { gui.update(); gui.updateProfiles(); current.updateConsoles(); - if (current.downloadJars) { + if (current.downloadAllAvailableJARFiles) { Executors.newSingleThreadExecutor().execute(() -> { try { downloadJars(); @@ -478,9 +428,8 @@ public class Profile { /** * Parses a profile, and creates a profile with the data. - * - * @param profileData The data of the new profile - * @return The new profile + * @param profileData

The data of the new profile

+ * @return

The new profile

*/ private static Profile parseProfile(String[] profileData) { return new Profile( @@ -493,9 +442,8 @@ public class Profile { /** * Parses a server, and creates a new collection. - * - * @param profile The profile which to add the collection - * @param serverData The data to parse + * @param profile

The profile which to add the collection

+ * @param serverData

The data to parse

*/ private static void parseServer(Profile profile, String[] serverData) { profile.collections.add(new Collection( @@ -518,13 +466,9 @@ public class Profile { * @throws IOException On version file failure or folder creation failure */ public static void downloadJars() throws IOException { - if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) { - JOptionPane.showMessageDialog( - null, - "Could not create the Jars folder. Please run the program with admin permissions, or move it to a writable directory.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + if (!new File(jarDirectory).exists() && !new File(jarDirectory).mkdirs()) { + showError("Could not create the Jars folder. Please run the program with admin permissions, or move it to " + + "a writable directory."); throw new FileNotFoundException("Unable to create jars folder"); } try { @@ -538,81 +482,195 @@ public class Profile { /** * Prints something to the gui status field if the gui exists * Otherwise it prints to the console - * - * @param str The string to show the user + * @param message

The string to show the user

*/ - private static void printToGui(String str) { + private static void printToGui(String message) { if (gui != null) { - gui.setStatus(str); + gui.setStatus(message); } else { - System.out.println(str); + System.out.println(message); } } + public static void showError(String title, String message) { + if (gui != null) { + JOptionPane.showMessageDialog( + null, + message, + title, + JOptionPane.ERROR_MESSAGE + ); + } else { + System.out.println(message); + } + } + + public static void showError(String message) { + if (gui != null) { + JOptionPane.showMessageDialog( + null, + message, + "Error", + JOptionPane.ERROR_MESSAGE + ); + } else { + System.out.println(message); + } + } + + public static void showMessage(String title, String message) { + if (gui != null) { + JOptionPane.showMessageDialog( + null, + message, + title, + JOptionPane.INFORMATION_MESSAGE + ); + } else { + System.out.println(message); + } + } + + public static void showMessage(String message) { + if (gui != null) { + JOptionPane.showMessageDialog( + null, + message, + "Info", + JOptionPane.INFORMATION_MESSAGE + ); + } else { + System.out.println(message); + } + } + + private static void showMessage() {} + /** - * Downloads jar files for all possible server versions. - * - * @throws IOException If a jar fails to download. + * Downloads jar files for all possible server versions + * @throws IOException

If a jar fails to download

*/ private static void downloadAll() throws IOException { for (ServerType type : ServerType.getServerTypes()) { - String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion; - AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null; + String downloadURL = Objects.requireNonNull(type).getDownloadURL(); + String typeName = type.getName(); + AdvancedServerType advancedServerType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null; for (String version : type.getVersions()) { boolean success; - printToGui("Downloading: " + name + version + ".jar"); - File file = new File(jarDir + type.getName() + version + ".jar"); - Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); + printToGui("Downloading: " + typeName + version + ".jar"); + File file = new File(jarDirectory + type.getName() + version + ".jar"); + Path filePath = Paths.get(jarDirectory + type.getName() + version + ".jar"); switch (type.getName()) { case "Vanilla": case "Snapshot": - if (version.equals("Latest")) { - String versionText = readFile(Objects.requireNonNull(advType).getVersionURL()); - JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); - String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString(); - JsonElement ver = jsonObject.getAsJsonArray("versions").get(0); - String versionFile = ver.getAsJsonObject().get("url").getAsString(); - - versionText = readFile(versionFile); - jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); - String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString(); - - setVersion(name, latest); - success = (file.isFile() && latest.equals(getVersion(name))) || downloadFile(jarFile, filePath); - } else { - success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath); - } + success = downloadVanillaJar(advancedServerType, file, downloadURL, filePath, typeName, version); break; case "Spigot": case "Craftbukkit": case "MCPCplus": - success = file.isFile() || downloadFile(url + name + version + ".jar", filePath); + success = downloadSpigotJar(file, downloadURL, typeName, version, filePath); break; case "SpongeVanilla": - newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL() + version), advType.getSrcStart(), advType.getSrcEnd()); - success = file.isFile() || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath); + success = downloadSpongeVanillaJar(advancedServerType, file, downloadURL, filePath, version); break; case "Bungee": - newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd()); - setVersion(name, newestVersion); - success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url, filePath); + success = downloadBungeeJar(advancedServerType, file, downloadURL, filePath, typeName); break; default: success = true; } if (!success) { - printToGui("Error downloading: " + name + version + ".jar"); - throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); + printToGui("Error downloading: " + typeName + version + ".jar"); + throw new FileNotFoundException("Error downloading: " + typeName + version + ".jar"); } } } } + /** + * Downloads a Spigot, Craftbukkit or MCPC+ .jar file + * @param file

The file the .jar file should be saved as

+ * @param downloadURL

The base URL for downloading the .jar file

+ * @param typeName

The name of the selected server type

+ * @param version

The version of the .jar file to download

+ * @param filePath

The path of the .jar file

+ * @return

True if the file exists or the file was successfully downloaded

+ */ + private static boolean downloadSpigotJar(File file, String downloadURL, String typeName, String version, Path filePath) { + return file.isFile() || downloadFile(downloadURL + typeName + version + ".jar", filePath); + } + + /** + * Downloads a Vanilla/Snapshot .jar file according to the input + * @param advancedServerType

The advanced server type containing required information

+ * @param file

The file the .jar file should be saved as

+ * @param downloadURL

The base URL for downloading the .jar file

+ * @param filePath

The path of the .jar file

+ * @param typeName

The name of the selected server type

+ * @param version

The version of the .jar file to download

+ * @return

True if the file exists or the file was successfully downloaded

+ * @throws IOException

If something goes horribly wrong

+ */ + private static boolean downloadVanillaJar(AdvancedServerType advancedServerType, File file, String downloadURL, + Path filePath, String typeName, String version) throws IOException { + if (version.equals("Latest")) { + String versionText = readFile(Objects.requireNonNull(advancedServerType).getVersionURL()); + JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); + String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString(); + JsonElement ver = jsonObject.getAsJsonArray("versions").get(0); + String versionFile = ver.getAsJsonObject().get("url").getAsString(); + versionText = readFile(versionFile); + jsonObject = new JsonParser().parse(versionText).getAsJsonObject(); + String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString(); + setVersion(typeName, latest); + return (file.isFile() && latest.equals(getVersion(typeName))) || downloadFile(jarFile, filePath); + } else { + return file.isFile() || downloadFile(downloadURL + version + + Objects.requireNonNull(advancedServerType).getDownloadURLPart() + version + ".jar", filePath); + } + } + + /** + * Downloads a SpongeVanilla .jar file according to the input + * @param advancedServerType

The advanced server type containing required information

+ * @param file

The file the .jar file should be saved as

+ * @param downloadURL

The base URL for downloading the .jar file

+ * @param filePath

The path of the .jar file

+ * @param version

The version of the .jar file to download

+ * @return

True if the file exists or the file was successfully downloaded

+ * @throws IOException

If something goes horribly wrong

+ */ + private static boolean downloadSpongeVanillaJar(AdvancedServerType advancedServerType, File file, + String downloadURL, Path filePath, + String version) throws IOException { + String newestVersion = stringBetween(readFile(Objects.requireNonNull(advancedServerType).getVersionURL() + + version), advancedServerType.getSrcStart(), advancedServerType.getSrcEnd()); + return file.isFile() || downloadFile(downloadURL + newestVersion + advancedServerType.getDownloadURLPart() + + newestVersion + ".jar", filePath); + } + + /** + * Downloads a Bungee .jar file according to the input + * @param advancedServerType

The advanced server type containing required information

+ * @param file

The file the .jar file should be saved as

+ * @param downloadURL

The base URL for downloading the .jar file

+ * @param filePath

The path of the .jar file

+ * @param typeName

The name of the selected server type

+ * @return

True if the file exists or the file was successfully downloaded

+ * @throws IOException

If something goes horribly wrong

+ */ + private static boolean downloadBungeeJar(AdvancedServerType advancedServerType, File file, + String downloadURL, Path filePath, String typeName) throws IOException { + String newestVersion = stringBetween(readFile(Objects.requireNonNull(advancedServerType).getVersionURL()), + advancedServerType.getSrcStart(), advancedServerType.getSrcEnd()); + setVersion(typeName, newestVersion); + return (file.isFile() && newestVersion.equals(getVersion(typeName))) || downloadFile(downloadURL, filePath); + } + /** * Returns the current version of a type - * - * @param type The version type - * @return The version string + * @param type

The version type

+ * @return

The version string

*/ private static String getVersion(String type) { switch (type) { @@ -628,10 +686,9 @@ public class Profile { } /** - * Sets a server type's last downloaded version. - * - * @param type The version type - * @param version The version string + * Sets a server type's last downloaded version + * @param type

The version type

+ * @param version

The version string

*/ private static void setVersion(String type, String version) { if (!type.equals("")) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/AdvancedServerType.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/AdvancedServerType.java index 3346269..78c2b39 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/AdvancedServerType.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/AdvancedServerType.java @@ -13,6 +13,16 @@ public class AdvancedServerType extends ServerType { private final String srcStart; private final String srcEnd; + /** + * Instantiates a new Advanced server type + * @param name

The name of the server type

+ * @param versions

A list of one or more server versions for the type

+ * @param versionURL

The URL for checking last version for server type

+ * @param srcStart

The string in the version file marking the start of the newest version entry

+ * @param srcEnd

The string in the version file marking the end of the newest version entry

+ * @param downloadURL

The URL used for downloading .jar files

+ * @param downloadURLPart

An extra part for the download URL

+ */ AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) { super(name, versions, downloadURL); this.srcStart = srcStart; @@ -21,18 +31,34 @@ public class AdvancedServerType extends ServerType { this.downloadURLPart = downloadURLPart; } + /** + * Gets the URL used for downloading latest version information + * @return

The latest version URL

+ */ public String getVersionURL() { return this.versionURL; } + /** + * Gets an additional part of the download URL + * @return

Additional download URL part

+ */ public String getDownloadURLPart() { return this.downloadURLPart; } + /** + * Gets the string marking the start of the latest server version in the version document + * @return

A string marking the start of the latest version

+ */ public String getSrcStart() { return this.srcStart; } + /** + * Gets the string marking the end of the latest server version in the version document + * @return

A string marking the end of the latest version

+ */ public String getSrcEnd() { return this.srcEnd; } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index cc55046..02c84e7 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -27,7 +27,7 @@ public class Server { 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 jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator; + private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator; private final String name; private String path; @@ -185,11 +185,7 @@ public class Server { * @param name The name of the player to remove */ public void removePlayer(String name) { - for (int i = 0; i < playerList.size(); i++) { - if (name.equals(playerList.get(i))) { - playerList.remove(i); - } - } + playerList.removeIf(player -> player.equals(name)); Profile.getGUI().removePlayer(name); } @@ -271,7 +267,7 @@ public class Server { private boolean run() { if (this.enabled) { this.started = true; - if (!Profile.getCurrent().getDownloadJars()) { + if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) { try { Profile.getGUI().setStatus("Downloading jar..."); this.downloadJar(); @@ -295,8 +291,8 @@ public class Server { try { ProcessBuilder builder; String serverPath; - if (Profile.getCurrent().getDownloadJars() && !type.getName().equals("Custom")) { - serverPath = jarDir + this.getType(); + if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) { + serverPath = jarDirectory + this.getType(); } else { serverPath = this.path + File.separator + this.getType(); } @@ -347,7 +343,6 @@ 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. - * * @throws FileNotFoundException if the file was not found and could not be acquired. */ private void downloadJar() throws FileNotFoundException { @@ -438,9 +433,8 @@ public class Server { /** * Returns the current version of a type - * - * @param type The version type - * @return The version string + * @param type

The version type

+ * @return

The version string

*/ private String getVersion(String type) { switch (type) { @@ -459,9 +453,8 @@ public class Server { /** * Sets a server type's last downloaded version. - * - * @param type The version type - * @param version The version string + * @param type

The version type

+ * @param version

The version string

*/ private void setVersion(String type, String version) { if (!type.equals("")) { @@ -483,9 +476,8 @@ public class Server { /** * Sends a command to this server through its writer. - * - * @param command Command to send to the server - * @throws IOException If write fails + * @param command

Command to send to the server

+ * @throws IOException

If write fails

*/ public void sendCommand(String command) throws IOException { if (this.process != null && this.writer != null) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerType.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerType.java index f14c03d..94637a7 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerType.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerType.java @@ -20,49 +20,69 @@ public class ServerType { private final String[] versions; private final String downloadURL; private static final ArrayList serverTypes = new ArrayList<>(); - + + /** + * Instantiates a new server type + * @param name

The name of the server type

+ * @param versions

A list of one or more server versions for the type

+ * @param downloadURL

The URL used for downloading .jar files

+ */ ServerType(String name, String[] versions, String downloadURL) { this.name = name; this.versions = versions; this.downloadURL = downloadURL; serverTypes.add(this); } - + + /** + * Gets the name of the server type + * @return

Server type name

+ */ public String getName() { return this.name; } - + + /** + * Gets a list of versions available for the server type + * @return

A list of server versions

+ */ public String[] getVersions() { return this.versions; } + /** + * Gets the url used for downloading JAR files + * @return

A download URL

+ */ public String getDownloadURL() { return this.downloadURL; } + /** + * Gets all instantiated server types + * @return

A list of server types

+ */ public static ArrayList getServerTypes() { return serverTypes; } /** * Gets a list of all server types' names. - * - * @return A list of strings + * @return

A list of strings

*/ public static String[] getTypeNames() { ArrayList types = ServerType.getServerTypes(); - String[] serverTypes = new String[types.size()]; - for (int i = 0; i < types.size(); i++) { - serverTypes[i] = types.get(i).getName(); + String[] serverTypeNames = new String[types.size()]; + for (int i = 0; i < types.size(); i++) { + serverTypeNames[i] = types.get(i).getName(); } - return serverTypes; + return serverTypeNames; } /** - * Gets a server type by name. - * - * @param name Then name of the server type - * @return A ServerType + * Gets a server type by the given name + * @param name

Then name of the server type

+ * @return

A ServerType

*/ public static ServerType getByName(String name) { for (ServerType serverType : serverTypes) { @@ -75,8 +95,7 @@ public class ServerType { /** * Reads valid server types and version from a file, and creates their objects. - * - * @throws ConfigurationException if anything goes wrong. + * @throws ConfigurationException

If anything goes wrong

*/ public static void loadServerTypes() throws ConfigurationException { if (serverTypes.isEmpty()) { @@ -87,20 +106,22 @@ public class ServerType { throw new ConfigurationException("Server type configuration file is missing."); } while (file.hasNextLine()) { - String[] str = file.nextLine().split(";", -1); - int len = str.length; - String[] ver; - if (str[1].contains(",")) { - ver = str[1].split(",", -1); + //Splits the next file line into arguments + String[] fileLine = file.nextLine().split(";", -1); + int lineLength = fileLine.length; + //Gets list of server versions from file line + String[] serverVersion; + if (fileLine[1].contains(",")) { + serverVersion = fileLine[1].split(",", -1); } else { - ver = new String[]{str[1]}; + serverVersion = new String[]{fileLine[1]}; } - switch (len) { + switch (lineLength) { case 7: - new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]); + new AdvancedServerType(fileLine[0], serverVersion, fileLine[2], fileLine[3], fileLine[4], fileLine[5], fileLine[6]); break; case 3: - new ServerType(str[0], ver, str[2]); + new ServerType(fileLine[0], serverVersion, fileLine[2]); break; default: throw new ConfigurationException("Error: Configuration file invalid."); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java index 227cadd..235df62 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java @@ -63,7 +63,8 @@ public class Console implements ActionListener, KeyListener { @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name. + //Sends the command from the input to the server with the same name. + if (e.getSource() == textInput) { java.lang.String text = textInput.getText(); Profile.getCurrent().sendCommand(this.name, text); commands.add(text); @@ -77,19 +78,11 @@ public class Console implements ActionListener, KeyListener { @Override public void keyPressed(KeyEvent e) { + //Cycles through previously used commands if (e.getKeyCode() == KeyEvent.VK_UP) { - if (commands.size() > 0 && commandIndex > 0) { - textInput.setText(commands.get(--commandIndex)); - } + showPreviousCommand(); } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { - if (commands.size() > 0) { - if (commandIndex == commands.size() - 1) { - commandIndex++; - textInput.setText(""); - } else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) { - textInput.setText(commands.get(++commandIndex)); - } - } + showNextCommand(); } } @@ -100,4 +93,33 @@ public class Console implements ActionListener, KeyListener { @Override public void keyTyped(KeyEvent e) { } + + /** + * Shows the previously executed command in the input field + * + *

Shows the previously executed command if a command was just executed. + * Shows the command executed earlier if already showing a previously executed command.

+ */ + private void showPreviousCommand() { + if (commands.size() > 0 && commandIndex > 0) { + textInput.setText(commands.get(--commandIndex)); + } + } + + /** + * Shows the next previously executed command or clears the input field + * + *

Shows the next previously executed command if such a command exists. + * Clears the input field if no next used command exists.

+ */ + private void showNextCommand() { + if (commands.size() > 0) { + if (commandIndex == commands.size() - 1) { + commandIndex++; + textInput.setText(""); + } else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) { + textInput.setText(commands.get(++commandIndex)); + } + } + } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java index ed1e422..3a79de5 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java @@ -1,5 +1,6 @@ package net.knarcraft.minecraftserverlauncher.userinterface; +import net.knarcraft.minecraftserverlauncher.Shared; import net.knarcraft.minecraftserverlauncher.profile.Collection; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.server.Server; @@ -10,8 +11,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Objects; import java.util.Scanner; @@ -60,7 +59,7 @@ public class GUI implements ActionListener { private TrayIcon trayIcon; /** - * Create the application window. + * Creates the application window */ public GUI() throws IOException { initialize(440, 170); @@ -69,10 +68,9 @@ public class GUI implements ActionListener { } /** - * Creates the application window with a preferred width and height. - * - * @param width The preferred width - * @param height The preferred height + * Creates the application window with a preferred width and height + * @param width

The preferred width

+ * @param height

The preferred height

*/ public GUI(int width, int height) throws IOException { initialize(width, height); @@ -96,7 +94,7 @@ public class GUI implements ActionListener { */ public void setStatus(String text) { this.lblStatuslabel.setText(text); - try (PrintWriter file = new PrintWriter(new FileWriter(Main.getAppDir() + File.separator + "latestrun.log", true))) { + try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", true))) { file.println(text); } catch (IOException e ) { e.printStackTrace(); @@ -152,7 +150,7 @@ public class GUI implements ActionListener { } chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground()); chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0); - chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars()); + chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadAllAvailableJARFiles()); this.targetServer.removeAllItems(); this.targetServer.addItem("All"); for (Collection collection : Profile.getCurrent().getCollections()) { @@ -547,46 +545,21 @@ public class GUI implements ActionListener { } else if (e.getSource() == chckbxmntmDownloadJars) { downloadJars(); } else if (e.getSource() == mntmErrors) { - goToURL("https://archive.knarcraft.net/BungeeMinecraftServerLauncherInfo/"); + Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherInfo/"); } else if (e.getSource() == mntmSetup) { - JOptionPane.showMessageDialog( - null, - setupText, - "Setup", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("Setup", setupText); } else if (e.getSource() == mntmManualUpdate) { - goToURL("https://git.knarcraft.net/EpicKnarvik97/Minecraft-Server-Launcher"); + Shared.goToURL("https://git.knarcraft.net/KnarCraft/Minecraft-Server-Launcher/releases"); } else if (e.getSource() == mntmRunInBackground) { - JOptionPane.showMessageDialog( - null, - runInBackgroundText, - "Run in background", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("Run in background", runInBackgroundText); } else if (e.getSource() == mntmDelayStartup) { - JOptionPane.showMessageDialog( - null, - delayStartupText, - "Delay startup", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("Delay startup", delayStartupText); } else if (e.getSource() == mntmDownloadJars) { - JOptionPane.showMessageDialog( - null, - downloadJarsText, - "Download jars", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("Download jars", downloadJarsText); } else if (e.getSource() == mntmAbout) { - JOptionPane.showMessageDialog( - null, - aboutText, - "About", - JOptionPane.INFORMATION_MESSAGE - ); + Profile.showMessage("About", aboutText); } else if (e.getSource() == mntmStory) { - goToURL("https://archive.knarcraft.net/BungeeminecraftserverlauncherStory/"); + Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherStory/"); } else if (e.getSource() == btnStartServer) { try { Profile.getCurrent().save(); @@ -648,7 +621,7 @@ public class GUI implements ActionListener { Profile.getCurrent().sendCommand(selectedServerValue, "reload"); } } else if (e.getSource() == btnServerConsoles) { - ServerConsoles.show(); + ServerConsoles.setAsVisible(); } else if (e.getSource() == targetServer) { updatePlayers(); } @@ -691,12 +664,7 @@ public class GUI implements ActionListener { setStatus("Servers are stopping..."); Server.stop(); } catch (IOException e1) { - JOptionPane.showMessageDialog( - null, - "Could not stop server.", - "Error", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("Could not stop server."); e1.printStackTrace(); } } @@ -716,12 +684,7 @@ public class GUI implements ActionListener { Objects.requireNonNull(profile).setDelayStartup(0); } } else { - JOptionPane.showMessageDialog( - null, - "No profile selected", - "Error", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("No profile selected"); } } @@ -734,12 +697,7 @@ public class GUI implements ActionListener { Profile profile = Profile.getProfile(selected.toString()); Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected()); } else { - JOptionPane.showMessageDialog( - null, - "No profile selected", - "Error", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("No profile selected"); } } @@ -750,14 +708,9 @@ public class GUI implements ActionListener { Object selected = profiles.getSelectedItem(); if (selected != null) { Profile profile = Profile.getProfile(selected.toString()); - Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected()); + Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected()); } else { - JOptionPane.showMessageDialog( - null, - "No profile selected", - "Error", - JOptionPane.ERROR_MESSAGE - ); + Profile.showError("No profile selected"); } } @@ -770,7 +723,6 @@ public class GUI implements ActionListener { chooser.setDialogTitle("Backup folder"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); - if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File path = chooser.getSelectedFile(); for (Collection collection : Profile.getCurrent().getCollections()) { @@ -781,7 +733,7 @@ public class GUI implements ActionListener { if (!destFolder.exists()) { if (destFolder.mkdirs()) { try { - copyFolder(srcFolder, destFolder); + Shared.copyFolder(this, srcFolder, destFolder); } catch (IOException e) { e.printStackTrace(); return; @@ -817,20 +769,6 @@ public class GUI implements ActionListener { } } - /** - * Opens an url in the user's default application. - * - * @param url URL to open - */ - private void goToURL(String url) { - java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); - try { - desktop.browse(new URI(url)); - } catch (URISyntaxException | IOException e1) { - e1.printStackTrace(); - } - } - /** * Loads popup messages from a text file. */ @@ -858,42 +796,4 @@ public class GUI implements ActionListener { } } } - - /** - * Recursivly copies a folder to another location - * - * @param src The folder to copy - * @param dest Target destination - * @throws IOException If we can't start a file stream - */ - private void copyFolder(File src, File dest) throws IOException{ - if (!src.isDirectory()) { - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest); - byte[] buffer = new byte[1024]; - int length; - while ((length = in.read(buffer)) > 0){ - out.write(buffer, 0, length); - } - in.close(); - out.close(); - this.setStatus("Copied file " + src); - } else { - if(!dest.exists()){ - if (dest.mkdir()) { - this.setStatus("Copied directory " + src); - } else { - return; - } - } - String[] files = src.list(); - if (files != null) { - for (String file : files) { - File srcFile = new File(src, file); - File destFile = new File(dest, file); - copyFolder(srcFile, destFile); - } - } - } - } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java index 8603969..1c4691b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java @@ -15,25 +15,40 @@ import java.awt.BorderLayout; */ public class ServerConsoles { private static JFrame frame; - private static JTabbedPane consolesTab; + private static JTabbedPane consolesTabbedPane; + /** + * Initializes the server consoles frame + */ public ServerConsoles() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - consolesTab = new JTabbedPane(JTabbedPane.TOP); - frame.getContentPane().add(consolesTab, BorderLayout.CENTER); + consolesTabbedPane = new JTabbedPane(JTabbedPane.TOP); + frame.getContentPane().add(consolesTabbedPane, BorderLayout.CENTER); } - public static Console addTab(String name) { - return new Console(consolesTab, name); + /** + * Adds a new console tab + * @param name

The name of the consoles tab

+ * @return

A new console element with the new tabbed pane

+ */ + public static Console addConsoleTab(String name) { + return new Console(consolesTabbedPane, name); } - public static void show() { + /** + * Sets the server consoles frame as visible + */ + public static void setAsVisible() { frame.setVisible(true); } - public static JTabbedPane getTab() { - return consolesTab; + /** + * Returns the tabbed pane containing the server consoles + * @return

A tabbed pane

+ */ + public static JTabbedPane getTabbedPane() { + return consolesTabbedPane; } } \ No newline at end of file diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java b/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java index be50a34..479a115 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/DownloadTests.java @@ -1,6 +1,5 @@ package net.knarcraft.minecraftserverlauncher; -import net.knarcraft.minecraftserverlauncher.profile.Profile; import net.knarcraft.minecraftserverlauncher.server.ServerType; import org.junit.Test; @@ -9,9 +8,8 @@ import java.io.IOException; public class DownloadTests { @Test - public void downloadJarsTest() throws IOException, ConfigurationException { - // Will currently always fail since knarcraft.net is down. + public void downloadJarsTest() throws ConfigurationException { ServerType.loadServerTypes(); - Profile.downloadJars(); + //Profile.downloadJars(); } }