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 { * * @returnAn 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.
+ * + * @returnThe 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.
+ * + * @returnThe 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 dataThe save string to load
- * @returnThe currently selected profile
- * @throws IOExceptionIf unable to create the GUI
+ * @param downloadAllJarsWhether 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 profileNameThe 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 silentIf 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 silentIf silent, no GUI will be created
+ * @returnThe currently selected profile
+ */ + private String fromString(boolean silent) { + MapThe map to save the loaded configuration to
+ * @param lineThe line to read
+ */ + private void parseSaveLine(MapWhether to download all jars
- */ - public void setDownloadAllJars(boolean downloadAllJars) { - this.downloadAllJars = downloadAllJars; - } - - /** - * Sets the currently selected profile - * - * @param profileNameThe 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 InterruptedExceptionIf 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 serverThe server to kill
* @throws InterruptedExceptionIf 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 + * + * @returnThe 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 textThe text to search
+ * @param textThe 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 patternThe regex pattern to use
- * @param textThe string to execute the pattern on
+ * @param textThe string to execute the pattern on
* @returnThe 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 { * @returnTrue 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 targetMapThe map to update
- * @param dataThe data string to parse
+ * @param dataThe data string to parse
*/ - private void parseVersionsToMap(MapThe path of the jar file to download
- * @param releaseTypeThe release type used for downloading
- * @param lastVersionThe last server version found
+ * @param filePathThe path of the jar file to download
+ * @param releaseTypeThe release type used for downloading
+ * @param lastVersionThe last server version found
* @param versionContainerThe version container to use
* @returnTrue if the jar exists and is the latest version or was downloaded
* @throws IOExceptionIf 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 tabThe tabbed pane used for displaying the console
+ * @param tabThe tabbed pane used for displaying the console
* @param nameThe 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 outputLinesThe 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 ArrayListThe main frame of the GUI
+ * @param mainFrameThe main frame of the GUI
* @param controlServersThe 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 mainFrameThe main frame of the GUI
+ * @param mainFrameThe main frame of the GUI
* @param controlServersThe 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 actionSourceThe object being interacted with
+ * @param actionSourceThe object being interacted with
* @param selectedServerValueThe 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 actionSourceThe clicked object
+ * @param actionSourceThe clicked object
* @param selectedServerValueThe server currently selected
* @param selectedPlayerValueThe 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 MapWhether 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 widthThe width of the GUI
+ * @param widthThe width of the GUI
* @param heightThe height of the GUI
* @throws IOExceptionIf 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 menuBarThe menu bar to attach items to
+ * @param menuBarThe menu bar to attach items to
* @param serverLauncherGUIThe 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 mainFrameThe main frame of the GUI
+ * @param mainFrameThe main frame of the GUI
* @param serverLauncherGUIThe 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 pathThe path of the file to read
+ * @returnA buffered reader for reading the file
+ * @throws FileNotFoundExceptionIf 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 IOExceptionIf 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 pathThe path of the file to write to
+ * @param textThe text to write
+ * @param addTrailingNewlineWhether to add a new line at the end of the file
+ * @throws IOExceptionIf 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 targetThe 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 buildDirectoryThe directory containing BuildTool files
- * @param jarDirectoryThe directory containing downloaded .jar files
- * @param guiThe GUI to write messages to
+ * @param jarDirectoryThe directory containing downloaded .jar files
+ * @param guiThe 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 searchStringThe start of the name of the built file used to find it
- * @param newNameThe new name of the built file
+ * @param newNameThe new name of the built file
* @returnTrue 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 processBuilderThe process builder to execute
* @returnTrue if the process exited successfully
*/ @@ -141,6 +147,7 @@ public class JarBuilder { /** * Gets the latest build tools version available + * * @returnThe latest build tools version available
* @throws IOExceptionIf 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 IOExceptionIf 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 +