From f1eead3807c10dfbd9936b7b5a420524a700d557 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 2 Aug 2021 21:06:22 +0200 Subject: [PATCH] Adds various fixes to make the two java versions work as expected Makes it possible to load a controller without generating a GUI, for better testing Makes sure not to try and parse empty profile lines Saves controller settings in a more readable and appendable format Adds code for using the correct java version for the occasion Adds a new function for writing to files --- .../minecraftserverlauncher/Main.java | 8 +- .../profile/Profile.java | 6 +- ...ler.java => ServerLauncherController.java} | 195 ++++++++++++------ .../server/Server.java | 32 ++- .../server/ServerTypeHandler.java | 13 +- .../server/ServerVersionContainer.java | 13 +- .../server/servertypes/SpongeVanilla.java | 4 +- .../server/servertypes/Vanilla.java | 6 +- .../userinterface/Console.java | 9 +- .../userinterface/ServerConsoles.java | 18 +- .../userinterface/ServerControlTab.java | 14 +- .../userinterface/ServerLauncherGUI.java | 40 ++-- .../userinterface/ServerLauncherMenu.java | 12 +- .../userinterface/Tray.java | 16 +- .../userinterface/WebBrowser.java | 6 +- .../utility/CommonFunctions.java | 47 ++++- .../utility/JarBuilder.java | 17 +- .../utility/Updater.java | 8 +- .../server/ServerTypeHandlerTest.java | 1 - .../server/ServerVersionContainerTest.java | 9 +- .../utility/JarBuilderTest.java | 12 ++ 21 files changed, 350 insertions(+), 136 deletions(-) rename src/main/java/net/knarcraft/minecraftserverlauncher/profile/{Controller.java => ServerLauncherController.java} (70%) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java index eab5d0c..80c060f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java @@ -1,7 +1,7 @@ package net.knarcraft.minecraftserverlauncher; import net.knarcraft.minecraftserverlauncher.profile.Collection; -import net.knarcraft.minecraftserverlauncher.profile.Controller; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; @@ -28,7 +28,7 @@ import java.util.concurrent.TimeUnit; public class Main { private static final String updateChannel = "beta"; private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher"; - private static final Controller controller = Controller.getInstance(); + private static final ServerLauncherController controller = ServerLauncherController.getInstance(); private static String applicationWorkDirectory; private static boolean serversAreRunning = false; private static ServerLauncherGUI gui; @@ -42,7 +42,7 @@ public class Main { } EventQueue.invokeLater(() -> { try { - new ServerConsoles(); + ServerConsoles.instantiate(); controller.loadState(); gui = controller.getGUI(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); @@ -58,7 +58,7 @@ public class Main { * * @return

The controller used by the software

*/ - public static Controller getController() { + public static ServerLauncherController getController() { return controller; } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java index b100d03..f562faa 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java @@ -8,7 +8,7 @@ import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import javax.naming.ConfigurationException; import javax.swing.*; -import java.io.*; +import java.io.IOException; import java.util.ArrayList; /** @@ -230,7 +230,9 @@ public class Profile { */ public static Profile fromString(String profileString) throws ConfigurationException { Profile profile; - if (profileString.contains("?")) { + if (profileString.equals("")) { + return null; + } else if (profileString.contains("?")) { String[] data = profileString.split("\\?"); String[] profileData = data[0].split(";", -1); profile = parseProfile(profileData); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/ServerLauncherController.java similarity index 70% rename from src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java rename to src/main/java/net/knarcraft/minecraftserverlauncher/profile/ServerLauncherController.java index 6365ba4..6d39a20 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Controller.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/ServerLauncherController.java @@ -9,16 +9,20 @@ import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import net.knarcraft.minecraftserverlauncher.utility.JarDownloader; import javax.naming.ConfigurationException; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Scanner; import java.util.concurrent.Executors; /** * This class handles profiles, GUI creation and session restoration */ -public class Controller { +public class ServerLauncherController { private final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; private final String profilesFile = workingDirectory + File.separator + "Profiles.txt"; @@ -28,12 +32,14 @@ public class Controller { private ServerLauncherGUI serverLauncherGUI; private Profile currentProfile; private boolean downloadAllJars; - private static Controller controller; + private static ServerLauncherController controller; + private String javaCommand = "java"; + private String oldJavaCommand = "java"; /** * Instantiates a new controller */ - private Controller() { + private ServerLauncherController() { this.profileList = new ArrayList<>(); } @@ -42,9 +48,9 @@ public class Controller { * * @return

An instance of the controller

*/ - public static Controller getInstance() { + public static ServerLauncherController getInstance() { if (controller == null) { - controller = new Controller(); + controller = new ServerLauncherController(); } return controller; } @@ -85,6 +91,29 @@ public class Controller { return currentProfile.getDelayStartup(); } + /** + * Gets the command for running java + * + *

To play on the newest version of Minecraft, this needs to be JDK 16. Can be just "java" or a file path.

+ * + * @return

The command for running java

+ */ + public String getJavaCommand() { + return javaCommand; + } + + /** + * Gets the command for running older versions of java + * + *

The command used to run older minecraft server versions. JRE 8 should probably work. + * Can be just "java" or a file path.

+ * + * @return

The command for running older versions of java

+ */ + public String getOldJavaCommand() { + return oldJavaCommand; + } + @Override public String toString() { int guiWidth; @@ -96,7 +125,8 @@ public class Controller { guiWidth = serverLauncherGUI.getSize().width; guiHeight = serverLauncherGUI.getSize().height; } - return String.format("%s;%d;%d;%b", currentProfile.getName(), guiWidth, guiHeight, downloadAllJars); + return String.format("selectedProfile;%s\nguiWidth;%d\nguiHeight;%d\ndownloadAllJars;%b\njavaCommand;%s\noldJavaCommand;%s", + currentProfile.getName(), guiWidth, guiHeight, downloadAllJars, javaCommand, oldJavaCommand); } /** @@ -171,37 +201,122 @@ public class Controller { } /** - * Loads data about the controller from a save string + * Sets the download all jars option * - * @param data

The save string to load

- * @return

The currently selected profile

- * @throws IOException

If unable to create the GUI

+ * @param downloadAllJars

Whether to download all jars

*/ - private String fromString(String data) throws IOException { - String[] dataList = data.split(";"); - this.downloadAllJars = Boolean.parseBoolean(dataList[3]); - this.serverLauncherGUI = new ServerLauncherGUI(Integer.parseInt(dataList[1]), Integer.parseInt(dataList[2])); - return dataList[0]; + public void setDownloadAllJars(boolean downloadAllJars) { + this.downloadAllJars = downloadAllJars; + } + + /** + * Sets the currently selected profile + * + * @param profileName

The name of the currently selected profile

+ */ + public void setCurrentProfile(String profileName) { + this.currentProfile = getProfileByName(profileName); + } + + /** + * Saves the state of the entire application to disk + */ + public void saveState() { + saveGUIStateToServer(); + try { + CommonFunctions.createAllFolders(); + CommonFunctions.writeFile(mainFile, this.toString()); + StringBuilder builder = new StringBuilder(); + for (Profile profile : profileList) { + builder.append(profile.toString()).append("\n"); + } + CommonFunctions.writeFile(profilesFile, builder.toString()); + } catch (IOException e) { + serverLauncherGUI.showError("Unable to save data."); + e.printStackTrace(); + } } /** * Reads profiles and servers from a text file */ public void loadState() throws ConfigurationException, IOException { + loadState(false); + } + + /** + * Reads profiles and servers from a text file + * + * @param silent

If silent, no GUI will be created

+ */ + public void loadState(boolean silent) throws ConfigurationException, IOException { //Loads data regarding this controller String currentProfile = null; if (new File(mainFile).exists()) { - String controllerData = CommonFunctions.readFile(mainFile); - currentProfile = this.fromString(controllerData); - } else { + currentProfile = this.fromString(silent); + } else if (!silent) { this.serverLauncherGUI = new ServerLauncherGUI(); } + if (silent) { + return; + } //Loads all saved profiles loadProfiles(currentProfile); //Makes the GUI show the loaded data executeGUILoadingTasks(); } + /** + * Loads data about the controller from a save string + * + * @param silent

If silent, no GUI will be created

+ * @return

The currently selected profile

+ */ + private String fromString(boolean silent) { + Map loadedData = new HashMap<>(); + + try { + String currentData = CommonFunctions.readFile(mainFile); + for (String line : currentData.split("\n")) { + parseSaveLine(loadedData, line); + } + this.downloadAllJars = Boolean.parseBoolean(loadedData.get("downloadAllJars")); + + if (!silent) { + this.serverLauncherGUI = new ServerLauncherGUI(Integer.parseInt(loadedData.get("guiWidth")), + Integer.parseInt(loadedData.get("guiHeight"))); + } else { + this.serverLauncherGUI = new ServerLauncherGUI(true); + } + + if (loadedData.get("javaCommand") != null) { + this.javaCommand = loadedData.get("javaCommand"); + } + if (loadedData.get("oldJavaCommand") != null) { + this.oldJavaCommand = loadedData.get("oldJavaCommand"); + } + return loadedData.get("selectedProfile"); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Saves one config line to the provided map + * + * @param loadedData

The map to save the loaded configuration to

+ * @param line

The line to read

+ */ + private void parseSaveLine(Map loadedData, String line) { + String[] lineData = line.split(";"); + if (lineData.length != 2) { + return; + } + + loadedData.put(lineData[0], lineData[1]); + } + /** * Loads all saved profiles * @@ -213,6 +328,9 @@ public class Controller { while (in.hasNextLine()) { String profileData = in.nextLine(); Profile loadedProfile = Profile.fromString(profileData); + if (loadedProfile == null) { + continue; + } profileList.add(loadedProfile); } } catch (FileNotFoundException e) { @@ -248,45 +366,6 @@ public class Controller { } } - /** - * Sets the download all jars option - * - * @param downloadAllJars

Whether to download all jars

- */ - public void setDownloadAllJars(boolean downloadAllJars) { - this.downloadAllJars = downloadAllJars; - } - - /** - * Sets the currently selected profile - * - * @param profileName

The name of the currently selected profile

- */ - public void setCurrentProfile(String profileName) { - this.currentProfile = getProfileByName(profileName); - } - - /** - * Saves the state of the entire application to disk - */ - public void saveState() { - saveGUIStateToServer(); - try { - CommonFunctions.createAllFolders(); - PrintWriter mFile = new PrintWriter(mainFile); - mFile.println(this); - mFile.close(); - PrintWriter pFile = new PrintWriter(profilesFile); - for (Profile profile : profileList) { - pFile.println(profile.toString()); - } - pFile.close(); - } catch (FileNotFoundException e) { - serverLauncherGUI.showError("Unable to save data."); - e.printStackTrace(); - } - } - /** * Updates the server object with the current state of the GUI server tab */ diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index bc90bd2..e200baa 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -2,7 +2,7 @@ package net.knarcraft.minecraftserverlauncher.server; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.profile.Collection; -import net.knarcraft.minecraftserverlauncher.profile.Controller; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; @@ -147,6 +147,7 @@ public class Server { /** * Kills all server processes + * * @throws InterruptedException

If interrupted waiting for any of the servers to stop

*/ private static void killServers() throws InterruptedException { @@ -158,6 +159,7 @@ public class Server { /** * Kills the given server after waiting 30 seconds for it to terminate normally + * * @param server

The server to kill

* @throws InterruptedException

If interrupted waiting for the server to stop

*/ @@ -174,7 +176,7 @@ public class Server { * Runs all enabled servers with their settings */ public static void startServers() { - Controller controller = Main.getController(); + ServerLauncherController controller = Main.getController(); controller.getGUI().setStatus("Starting servers"); int serverNum = 0; for (Collection collection : controller.getCurrentProfile().getCollections()) { @@ -429,6 +431,23 @@ public class Server { } } + /** + * Gets the correct java command to use for the selected server version + * + * @return

The java version to run

+ */ + private String getJavaCommand() { + ServerLauncherController controller = ServerLauncherController.getInstance(); + if (serverVersion.toLowerCase().contains("latest")) { + return controller.getJavaCommand(); + } else if (serverVersion.contains(".") && serverVersion.split("\\.").length >= 2 && + Integer.parseInt(serverVersion.split("\\.")[1]) >= 17) { + return controller.getJavaCommand(); + } else { + return controller.getOldJavaCommand(); + } + } + /** * Starts the process running this server * @@ -443,7 +462,7 @@ public class Server { } else { serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar"; } - builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M", + builder = new ProcessBuilder(getJavaCommand(), "-Xmx" + this.maxRam, "-Xms512M", "-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath, "nogui"); builder.directory(new File(this.path)); @@ -482,7 +501,7 @@ public class Server { /** * Looks for strings implying a player has joined or left, and updates the appropriate lists * - * @param text

The text to search

+ * @param text

The text to search

*/ private void updatePlayerList(String text) { if (!getType().isProxy()) { @@ -532,8 +551,9 @@ public class Server { /** * Returns the first regex capture group found in a pattern + * * @param pattern

The regex pattern to use

- * @param text

The string to execute the pattern on

+ * @param text

The string to execute the pattern on

* @return

The first capture group if a match is found. An empty string otherwise

*/ private String getFirstRegexCaptureGroup(String pattern, String text) { @@ -569,7 +589,7 @@ public class Server { * @return

True if nothing went wrong

*/ private boolean initializeJarDownload() { - Controller controller = Main.getController(); + ServerLauncherController controller = Main.getController(); if (!controller.getDownloadAllJars()) { try { controller.getGUI().setStatus("Downloading jar..."); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java index 6196d65..b62f43b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandler.java @@ -1,6 +1,17 @@ package net.knarcraft.minecraftserverlauncher.server; -import net.knarcraft.minecraftserverlauncher.server.servertypes.*; +import net.knarcraft.minecraftserverlauncher.server.servertypes.BungeeCord; +import net.knarcraft.minecraftserverlauncher.server.servertypes.CraftBukkit; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Custom; +import net.knarcraft.minecraftserverlauncher.server.servertypes.MCPCPlus; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Paper; +import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Spigot; +import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeForge; +import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeVanilla; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Travertine; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Vanilla; +import net.knarcraft.minecraftserverlauncher.server.servertypes.Waterfall; import javax.naming.ConfigurationException; import java.io.FileNotFoundException; diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java index 5380ea5..87b133b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java @@ -3,7 +3,10 @@ package net.knarcraft.minecraftserverlauncher.server; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; @@ -122,8 +125,8 @@ public class ServerVersionContainer { if (!new File(versionFile).exists()) { return; } - try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile)))) { - String currentData = CommonFunctions.readBufferedReader(reader); + try { + String currentData = CommonFunctions.readFile(versionFile); for (String line : currentData.split("\n")) { parseSaveLine(line); } @@ -178,9 +181,9 @@ public class ServerVersionContainer { * Reads versions from a text string and updates the version map * * @param targetMap

The map to update

- * @param data

The data string to parse

+ * @param data

The data string to parse

*/ - private void parseVersionsToMap(Map targetMap, String data) { + private void parseVersionsToMap(Map targetMap, String data) { String[] versions = data.split(","); for (String version : versions) { String[] versionData = version.split("!"); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java index 4d19d94..e9d77e4 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/SpongeVanilla.java @@ -20,8 +20,8 @@ public class SpongeVanilla extends AbstractServerType { private final String srcStart; private final String srcEnd; private final String downloadURLPart; - Function oldVersionFunction; - BiConsumer versionUpdateFunction; + Function oldVersionFunction; + BiConsumer versionUpdateFunction; final ServerVersionContainer serverVersionContainer; /** 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 f722e42..6c5ede4 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/servertypes/Vanilla.java @@ -54,9 +54,9 @@ public class Vanilla extends AbstractServerType { /** * Downloads the latest .jar file found if necessary * - * @param filePath

The path of the jar file to download

- * @param releaseType

The release type used for downloading

- * @param lastVersion

The last server version found

+ * @param filePath

The path of the jar file to download

+ * @param releaseType

The release type used for downloading

+ * @param lastVersion

The last server version found

* @param versionContainer

The version container to use

* @return

True if the jar exists and is the latest version or was downloaded

* @throws IOException

If the .jar cannot be downloaded

diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java index 4eaf35f..c3f639a 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java @@ -5,7 +5,11 @@ import net.knarcraft.minecraftserverlauncher.Main; import javax.swing.*; import javax.swing.text.DefaultCaret; import java.awt.*; -import java.awt.event.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.util.ArrayList; import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE; @@ -31,7 +35,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { /** * Instantiates a new console * - * @param tab

The tabbed pane used for displaying the console

+ * @param tab

The tabbed pane used for displaying the console

* @param name

The name of the console tab

*/ Console(JTabbedPane tab, String name) { @@ -77,6 +81,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { /** * Truncates the first 50 lines if the console output has reached the max limit + * * @param outputLines

The currently readable lines in the console output field

*/ private void truncateConsole(int outputLines) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java index e81837a..7f9f0c8 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerConsoles.java @@ -17,15 +17,21 @@ public class ServerConsoles { private static JFrame frame; private static JTabbedPane consolesTabbedPane; + private ServerConsoles() { + + } + /** * Initializes the server consoles frame */ - public ServerConsoles() { - frame = new JFrame(); - frame.setBounds(100, 100, 450, 300); - frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - consolesTabbedPane = new JTabbedPane(JTabbedPane.TOP); - frame.getContentPane().add(consolesTabbedPane, BorderLayout.CENTER); + public static void instantiate() { + if (frame == null || consolesTabbedPane == null) { + frame = new JFrame(); + frame.setBounds(100, 100, 450, 300); + frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + consolesTabbedPane = new JTabbedPane(JTabbedPane.TOP); + frame.getContentPane().add(consolesTabbedPane, BorderLayout.CENTER); + } } /** diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerControlTab.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerControlTab.java index cae7a4e..e7f1bcf 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerControlTab.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerControlTab.java @@ -1,7 +1,7 @@ package net.knarcraft.minecraftserverlauncher.userinterface; import net.knarcraft.minecraftserverlauncher.profile.Collection; -import net.knarcraft.minecraftserverlauncher.profile.Controller; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import javax.swing.*; import java.awt.event.ActionEvent; @@ -25,13 +25,13 @@ public class ServerControlTab implements ActionListener { private JButton showConsolesButton; private JTextField customCommandTextField; - private Controller controller = Controller.getInstance(); + private final ServerLauncherController controller = ServerLauncherController.getInstance(); private final ArrayList globalPlayers; /** * Instantiates a new server control tab * - * @param mainFrame

The main frame of the GUI

+ * @param mainFrame

The main frame of the GUI

* @param controlServers

The JPanel to attach the server controls to

*/ public ServerControlTab(JFrame mainFrame, JPanel controlServers) { @@ -42,7 +42,7 @@ public class ServerControlTab implements ActionListener { /** * Initializes GUI elements for the server control tab * - * @param mainFrame

The main frame of the GUI

+ * @param mainFrame

The main frame of the GUI

* @param controlServers

The JPanel to attach the server controls to

*/ private void initialize(JFrame mainFrame, JPanel controlServers) { @@ -149,7 +149,7 @@ public class ServerControlTab implements ActionListener { public void update() { this.targetServerCombo.removeAllItems(); this.targetServerCombo.addItem("All"); - for (Collection collection : Controller.getInstance().getCurrentProfile().getCollections()) { + for (Collection collection : ServerLauncherController.getInstance().getCurrentProfile().getCollections()) { this.targetServerCombo.addItem(collection.getName()); } } @@ -232,7 +232,7 @@ public class ServerControlTab implements ActionListener { /** * Handles command buttons acting on a specific server * - * @param actionSource

The object being interacted with

+ * @param actionSource

The object being interacted with

* @param selectedServerValue

The server currently selected

*/ private void handleServerCommands(Object actionSource, String selectedServerValue) { @@ -252,7 +252,7 @@ public class ServerControlTab implements ActionListener { /** * Handles command buttons which act on a player * - * @param actionSource

The clicked object

+ * @param actionSource

The clicked object

* @param selectedServerValue

The server currently selected

* @param selectedPlayerValue

The player currently selected

*/ diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java index d83f8bb..f77438f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java @@ -2,7 +2,7 @@ package net.knarcraft.minecraftserverlauncher.userinterface; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.profile.Collection; -import net.knarcraft.minecraftserverlauncher.profile.Controller; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; @@ -12,15 +12,16 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; import java.util.concurrent.Executors; -import static java.awt.Frame.NORMAL; import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsStream; @@ -35,7 +36,7 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getR public class ServerLauncherGUI extends MessageHandler implements ActionListener, GUI { private final JLabel lblStatuslabel = new JLabel("Servers are stopped"); - private Controller controller; + private final ServerLauncherController controller; private Map textStrings; private Tray applicationTray; @@ -64,6 +65,20 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, this.controller = Main.getController(); } + /** + * Creates the application window + * + * @param silent

Whether to make the GUI silent (hidden, for testing)

+ */ + public ServerLauncherGUI(boolean silent) throws IOException { + super(silent); + if (!silent) { + initialize(440, 170); + } + loadMessages(); + this.controller = Main.getController(); + } + /** * Creates the application window with a preferred width and height * @@ -146,7 +161,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, * Updates ServerLauncherGUI according to current profile settings */ public void updateWithSavedProfileData() { - Controller controller = Main.getController(); + ServerLauncherController controller = Main.getController(); serversPane.removeAll(); serverLauncherMenu.update(); serverControlTab.update(); @@ -158,7 +173,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, /** * Initializes the server launcher GUI * - * @param width

The width of the GUI

+ * @param width

The width of the GUI

* @param height

The height of the GUI

* @throws IOException

If unable to load the GUI icon

*/ @@ -169,7 +184,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e - ) { + ) { e.printStackTrace(); } @@ -311,7 +326,6 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, } - /** * Handles buttons and the combo on the main tab * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java index 3b96c99..a39f904 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java @@ -1,7 +1,7 @@ package net.knarcraft.minecraftserverlauncher.userinterface; -import net.knarcraft.minecraftserverlauncher.profile.Controller; import net.knarcraft.minecraftserverlauncher.profile.Profile; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import javax.swing.*; @@ -14,7 +14,7 @@ import java.util.Objects; */ public class ServerLauncherMenu implements ActionListener { - private JMenuBar menuBar; + private final JMenuBar menuBar; //Options private JCheckBoxMenuItem runInBackgroundCheckBoxMenuItem; private JCheckBoxMenuItem delayStartupCheckBoxMenuItem; @@ -31,17 +31,17 @@ public class ServerLauncherMenu implements ActionListener { private JMenuItem aboutMenuItem; private JMenuItem storyMenuItem; - private Controller controller; - private ServerLauncherGUI serverLauncherGUI; + private final ServerLauncherController controller; + private final ServerLauncherGUI serverLauncherGUI; /** * Initializes a new server launcher menu * - * @param menuBar

The menu bar to attach items to

+ * @param menuBar

The menu bar to attach items to

* @param serverLauncherGUI

The server launcher GUI to use

*/ public ServerLauncherMenu(JMenuBar menuBar, ServerLauncherGUI serverLauncherGUI) { - this.controller = Controller.getInstance(); + this.controller = ServerLauncherController.getInstance(); this.menuBar = menuBar; this.serverLauncherGUI = serverLauncherGUI; initialize(); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Tray.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Tray.java index 81f6611..7f274b4 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Tray.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Tray.java @@ -1,13 +1,17 @@ package net.knarcraft.minecraftserverlauncher.userinterface; import net.knarcraft.minecraftserverlauncher.Main; -import net.knarcraft.minecraftserverlauncher.profile.Controller; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; -import java.awt.event.*; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; @@ -20,14 +24,14 @@ public class Tray { private SystemTray tray; private TrayIcon trayIcon; - private Controller controller; - private ServerLauncherGUI serverLauncherGUI; - private JFrame mainFrame; + private final ServerLauncherController controller; + private final ServerLauncherGUI serverLauncherGUI; + private final JFrame mainFrame; /** * Instantiates a new tray * - * @param mainFrame

The main frame of the GUI

+ * @param mainFrame

The main frame of the GUI

* @param serverLauncherGUI

The server launcher GUI to use

*/ public Tray(JFrame mainFrame, ServerLauncherGUI serverLauncherGUI) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java index c4d53ee..dd91e80 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java @@ -12,7 +12,8 @@ public class WebBrowser { private static JFrame browserFrame; private static JTextPane editorPane; - private WebBrowser() {} + private WebBrowser() { + } /** * Instantiates a new web browser @@ -44,7 +45,8 @@ public class WebBrowser { editorPane.setContentType("text/html"); editorPane.addHyperlinkListener(hyperlinkEvent -> { if (hyperlinkEvent.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) { - displayPage(hyperlinkEvent.getURL().toString()); } + displayPage(hyperlinkEvent.getURL().toString()); + } }); } catch (IOException e) { editorPane.setContentType("text/html"); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index c16edb4..3cfb2e3 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -6,7 +6,17 @@ import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import net.knarcraft.minecraftserverlauncher.userinterface.WebBrowser; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +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.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -107,6 +117,17 @@ public final class CommonFunctions { return new Scanner(url.openStream()).useDelimiter("\\Z").next(); } + /** + * Gets a buffered reader for reading a given file + * + * @param path

The path of the file to read

+ * @return

A buffered reader for reading the file

+ * @throws FileNotFoundException

If the file does not exist

+ */ + public static BufferedReader getFileReader(String path) throws FileNotFoundException { + return new BufferedReader(new InputStreamReader(new FileInputStream(path))); + } + /** * Reads a file from disk * @@ -115,7 +136,28 @@ public final class CommonFunctions { * @throws IOException

If unable to find or read the file

*/ public static String readFile(String path) throws IOException { - return CommonFunctions.readBufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(path)))); + return CommonFunctions.readBufferedReader(getFileReader(path)); + } + + public static void writeFile(String path, String text) throws IOException { + writeFile(path, text, true); + } + + /** + * Writes text to a file + * + * @param path

The path of the file to write to

+ * @param text

The text to write

+ * @param addTrailingNewline

Whether to add a new line at the end of the file

+ * @throws IOException

If unable to write to the file

+ */ + public static void writeFile(String path, String text, Boolean addTrailingNewline) throws IOException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path))); + writer.write(text); + if (addTrailingNewline) { + writer.newLine(); + } + writer.close(); } /** @@ -262,6 +304,7 @@ public final class CommonFunctions { /** * Removes all files within a folder + * * @param target

The folder to delete from

*/ static void removeFilesRecursively(File target) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index 61e0c36..81ae888 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -1,6 +1,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import net.knarcraft.minecraftserverlauncher.userinterface.GUI; @@ -21,18 +22,21 @@ public class JarBuilder { private final String jarDirectory; private final ServerVersionContainer versionContainer; private final GUI gui; + private final String javaCommand; /** * Instantiates a new jar builder + * * @param buildDirectory

The directory containing BuildTool files

- * @param jarDirectory

The directory containing downloaded .jar files

- * @param gui

The GUI to write messages to

+ * @param jarDirectory

The directory containing downloaded .jar files

+ * @param gui

The GUI to write messages to

*/ public JarBuilder(String buildDirectory, String jarDirectory, GUI gui) { this.buildDirectory = buildDirectory; this.jarDirectory = jarDirectory; this.gui = gui; this.versionContainer = ServerVersionContainer.getInstance(); + this.javaCommand = ServerLauncherController.getInstance().getJavaCommand(); } /** @@ -40,7 +44,7 @@ public class JarBuilder { */ public void buildSpigotJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev", + ProcessBuilder processBuilder = new ProcessBuilder(javaCommand, "-jar", "BuildTools.jar", "--rev", "latest", "--output-dir", jarDirectory); if (executeBuildProcess(processBuilder) && moveBuiltJar("spigot-", "SpigotLatest.jar")) { gui.setStatus("Finished moving spigot.jar"); @@ -52,7 +56,7 @@ public class JarBuilder { */ public void buildBukkitJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile", + ProcessBuilder processBuilder = new ProcessBuilder(javaCommand, "-jar", "BuildTools.jar", "--compile", "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); if (executeBuildProcess(processBuilder) && moveBuiltJar("craftbukkit-", "BukkitLatest.jar")) { gui.setStatus("Finished moving craftbukkit.jar"); @@ -84,8 +88,9 @@ public class JarBuilder { /** * Moves a built .jar file to its target file + * * @param searchString

The start of the name of the built file used to find it

- * @param newName

The new name of the built file

+ * @param newName

The new name of the built file

* @return

True if the .jar file was moved successfully

*/ private boolean moveBuiltJar(String searchString, String newName) { @@ -108,6 +113,7 @@ public class JarBuilder { /** * Starts the build process and initializes + * * @param processBuilder

The process builder to execute

* @return

True if the process exited successfully

*/ @@ -141,6 +147,7 @@ public class JarBuilder { /** * Gets the latest build tools version available + * * @return

The latest build tools version available

* @throws IOException

If unable to read the version file

*/ diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java index 7fdef10..323c40b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java @@ -5,9 +5,12 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import javax.swing.*; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Paths; import java.util.Scanner; @@ -81,8 +84,9 @@ public final class Updater { * @throws IOException

If unable to run the updater

*/ private static void runUpdater(String url) throws IOException { + String javaCommand = ServerLauncherController.getInstance().getJavaCommand(); ProcessBuilder builder; - builder = new ProcessBuilder("java", "-jar", "Updater.jar", url, "yes", targetFile, "5", "nogui"); + builder = new ProcessBuilder(javaCommand, "-jar", "Updater.jar", url, "yes", targetFile, "5", "nogui"); builder.directory(new File(Main.getApplicationWorkDirectory())); builder.redirectErrorStream(true); builder.start(); diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java index 2194356..f2f121a 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerTypeHandlerTest.java @@ -4,7 +4,6 @@ import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; import org.junit.jupiter.api.Test; import javax.naming.ConfigurationException; - import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java index 7ef21f4..4ee859c 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java @@ -5,7 +5,10 @@ import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.createAllFolders; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,14 +38,14 @@ public class ServerVersionContainerTest { serverVersionContainer.reset(); System.out.println(serverVersionContainer.toString()); assertEquals("vanillaVersion;null\nsnapshotVersion;null\nbungeeVersion;null\nwaterfallVersions;\n" + - "travertineVersions;\nspongeVanillaVersions;\nspongeForgeVersions;", + "travertineVersions;\nspongeVanillaVersions;\nspongeForgeVersions;\ndownloadedBuildToolsVersion;null", serverVersionContainer.toString()); } @Test public void saveStateTest() throws IOException { serverVersionContainer.saveState(); - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile))); + BufferedReader reader = CommonFunctions.getFileReader(versionFile); String savedData = CommonFunctions.readBufferedReader(reader); reader.close(); assertEquals(serverVersionContainer.toString(), savedData); diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java index f0a7488..29c957c 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java @@ -1,14 +1,17 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import javax.naming.ConfigurationException; import java.io.File; import java.io.IOException; @@ -22,6 +25,15 @@ public class JarBuilderTest { private static String targetDirectory; private static String jarDirectory; + @BeforeAll + public static void preSetUp() { + try { + ServerLauncherController.getInstance().loadState(true); + } catch (ConfigurationException | IOException e) { + e.printStackTrace(); + } + } + @BeforeEach public void setUp() { targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +