diff --git a/src/net/knarcraft/serverlauncher/Main.java b/src/net/knarcraft/serverlauncher/Main.java index 37708f7..0669cef 100644 --- a/src/net/knarcraft/serverlauncher/Main.java +++ b/src/net/knarcraft/serverlauncher/Main.java @@ -83,7 +83,7 @@ public class Main { */ private static void updatePlayerList(String text, Collection collection) { Server server = collection.getServer(); - if (!server.typeName().equals("Bungee")) { + if (!server.getTypeName().equals("Bungee")) { String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game"); if (!joinedPlayer.equals("")) { if (!server.hasPlayer(joinedPlayer)) { diff --git a/src/net/knarcraft/serverlauncher/profile/Collection.java b/src/net/knarcraft/serverlauncher/profile/Collection.java index 734901f..097343d 100644 --- a/src/net/knarcraft/serverlauncher/profile/Collection.java +++ b/src/net/knarcraft/serverlauncher/profile/Collection.java @@ -22,9 +22,30 @@ public class Collection { this.name = name; } - Collection(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam, String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) { + Collection(String name, + String path, + boolean enabled, + String typeName, + String serverVersion, + String maxRam, + String vanillaVersion, + String snapshotVersion, + String spongeVanillaVersion, + String bungeeVersion + ) { this.serverTab = new ServerTab(name); - this.server = new Server(name, path, enabled, typeName, serverVersion, maxRam, vanillaVersion, snapshotVersion, spongeVanillaVersion, bungeeVersion); + this.server = new Server( + name, + path, + enabled, + typeName, + serverVersion, + maxRam, + vanillaVersion, + snapshotVersion, + spongeVanillaVersion, + bungeeVersion + ); this.serverConsole = ServerConsoles.addTab(name); this.name = name; this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam); diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index 4532a4e..f29d9c6 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -52,104 +52,6 @@ public class Profile { } } - public void addCollection(String name) { - if (name == null) { //If a user cancels or crosses out window - return; - } - if (!collectionExists(name) && !name.equals("") && !name.equals("All")) { - collections.add(new Collection(name)); - } else { - JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE); - } - } - - public void removeCollection(String name) { - for (int i = 0; i < collections.size(); i++) { - if (collections.get(i).getName().equals(name)) { - this.collections.remove(i); - Main.gui().updateSelectServers(); - break; - } - } - } - - public Collection getCollection(String name) { - for (Collection collection : this.collections) { - if (collection.getName().equals(name)) { - return collection; - } - } - return null; - } - - private boolean collectionExists(String name) { - for (Collection collection : this.collections) { - if (collection.getName().equals(name)) { - return true; - } - } - return false; - } - - public ArrayList getCollections() { - return this.collections; - } - - public static void addProfile(String name) { - if (name == null) { //If a user cancels or crosses out window - return; - } - if (name.equals("")) { - JOptionPane.showMessageDialog(null, "Profile name can't be blank.", "Error", JOptionPane.ERROR_MESSAGE); - 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); - return; - } - } - new Profile(name); - Main.gui().addProfile(name); - } - - public static Profile getProfile(String name) { - for (Profile profile : profiles) { - if (profile.name.equals(name)) { - return profile; - } - } - return null; - } - - public static void deleteProfile(String name) { - if (profiles.size() > 1) { - for (int i = 0; i < profiles.size(); i++) { - if (profiles.get(i).name.equals((name))) { - profiles.remove(i); - Main.gui().removeProfile(i); - } - } - } - } - - public static Profile getCurrent() { - return current; - } - - public static void setCurrent(String name) { - for (Profile profile : profiles) { - if (profile.name.equals(name)) { - current = profile; - break; - } - } - } - - public void setRunInBackground(boolean value) { - this.runInBackground = value; - } - public boolean getRunInBackground() { return this.runInBackground; } @@ -162,6 +64,42 @@ public class Profile { return this.downloadJars; } + public static Profile getCurrent() { + return current; + } + + public ArrayList getCollections() { + return this.collections; + } + + /** + * Gets a Collection object by name. + * + * @param name The name of the collection. + * @return A collection object. + */ + public Collection getCollection(String name) { + for (Collection collection : this.collections) { + if (collection.getName().equals(name)) { + return collection; + } + } + return null; + } + + public static Profile getProfile(String name) { + for (Profile profile : profiles) { + if (profile.name.equals(name)) { + return profile; + } + } + return null; + } + + public void setRunInBackground(boolean value) { + this.runInBackground = value; + } + public void setDelayStartup(int value) { if (value >= 0) { this.delayStartup = value; @@ -172,6 +110,97 @@ public class Profile { this.downloadJars = value; } + public static void setCurrent(String name) { + for (Profile profile : profiles) { + if (profile.name.equals(name)) { + current = profile; + break; + } + } + } + + /** + * Adds a collection to the profile if the name is valid. + * + * @param name The name of the collection and its elements. + */ + public void addCollection(String name) { + if (name == null) { //If a user cancels or crosses out window + return; + } + if (getCollection(name) == null && + !name.equals("") && + !name.equals("All") && + !name.contains("!") && + !name.contains("?") && + !name.contains(";") + ) { + 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 + ); + } + } + + /** + * Adds a profile if the name is valid and unique. + * + * @param name The name of the new profile. + */ + public static void addProfile(String name) { + if (name == null) { //If a user cancels or crosses out window + return; + } + if (name.equals("") && !name.contains("!") && !name.contains("?") && !name.contains(";")) { + JOptionPane.showMessageDialog( + null, + "Profile name can't be blank.", + "Error", + JOptionPane.ERROR_MESSAGE + ); + 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 + ); + return; + } + } + new Profile(name); + Main.gui().addProfile(name); + } + + public void removeCollection(String name) { + for (int i = 0; i < collections.size(); i++) { + if (collections.get(i).getName().equals(name)) { + this.collections.remove(i); + Main.gui().update(); + break; + } + } + } + + public static void removeProfile(String name) { + if (profiles.size() > 1) { + for (int i = 0; i < profiles.size(); i++) { + if (profiles.get(i).name.equals((name))) { + profiles.remove(i); + Main.gui().removeProfile(i); + } + } + } + } + /** * Sends a command to a server, or all servers * @@ -184,7 +213,12 @@ public class Profile { try { collection.getServer().sendCommand(command); } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Server " + collection.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Server " + collection.getName() + " caused an exception.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } } else { @@ -194,10 +228,19 @@ public class Profile { try { target.sendCommand(command); } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Server " + target.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Server " + target.getName() + " caused an exception.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } else { - JOptionPane.showMessageDialog(null, "Server " + serverName + " is invalid.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Server " + serverName + " is invalid.", + "Error", JOptionPane.ERROR_MESSAGE + ); } } } @@ -216,27 +259,64 @@ public class Profile { try { server.setServerVersion(serverTab.getVersion()); } catch (IllegalArgumentException e) { - JOptionPane.showMessageDialog(null, "Invalid server version for " + server.getName(), "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Invalid server version for " + server.getName(), + "Error", + JOptionPane.ERROR_MESSAGE + ); } server.toggle(serverTab.enabled()); } try (PrintWriter file = new PrintWriter("files/Profiles.txt")) { - file.print(""); + file.println(current.name); + file.close(); for (Profile profile : profiles) { - StringBuilder saveString = new StringBuilder(String.format("%s;%b;%d;%b?", profile.name, profile.runInBackground, profile.delayStartup, profile.downloadJars)); + StringBuilder saveString = new StringBuilder(String.format( + "%s;%b;%d;%b?", + profile.name, + profile.runInBackground, + profile.delayStartup, + profile.downloadJars) + ); for (Collection collection : profile.collections) { Server server = collection.getServer(); - saveString.append(String.format("%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!", server.getName(), server.getPath(), server.isEnabled(), server.typeName(), server.getServerVersion(), server.getMaxRam(), server.getVanillaVersion(), server.getSnapshotVersion(), server.getSpongeVanillaVersion(), server.getBungeeVersion())); + saveString.append(String.format( + "%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!", + server.getName(), + server.getPath(), + server.isEnabled(), + server.getTypeName(), + server.getServerVersion(), + server.getMaxRam(), + server.getVanillaVersion(), + server.getSnapshotVersion(), + server.getSpongeVanillaVersion(), + server.getBungeeVersion()) + ); } saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1)); - try (PrintWriter fileAppend = new PrintWriter(new FileWriter("files/Profiles.txt", true))) { + try (PrintWriter fileAppend = new PrintWriter(new FileWriter( + "files/Profiles.txt", + true + ))) { fileAppend.println(saveString); } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Unable to save to file. Try running the software as an administrator.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Unable to save to file. Try running the software as an administrator.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } @@ -245,39 +325,52 @@ public class Profile { */ public static void load() { try (Scanner in = new Scanner(new File("files/Profiles.txt"))) { + String profileName = in.nextLine(); try { while (in.hasNextLine()) { String line = in.nextLine(); if (line.contains("?")) { String[] data = line.split("\\?"); String[] profileData = data[0].split(";", -1); - Profile profile = new Profile(profileData[0], Boolean.parseBoolean(profileData[1]), Integer.parseInt(profileData[2]), Boolean.parseBoolean(profileData[3])); + Profile profile = parseProfile(profileData); Main.gui().addProfile(profileData[0]); if (data[1].contains("!")) { String[] servers = data[1].split("!", -1); for (String server : servers) { String[] serverData = server.split(";", -1); - profile.collections.add(new Collection(serverData[0], serverData[1], Boolean.parseBoolean(serverData[2]), serverData[3], serverData[4], serverData[5], serverData[6], serverData[7], serverData[8], serverData[9])); + parseServer(profile, serverData); } } else { String[] serverData = data[1].split(";", -1); - profile.collections.add(new Collection(serverData[0], serverData[1], Boolean.parseBoolean(serverData[2]), serverData[3], serverData[4], serverData[5], serverData[6], serverData[7], serverData[8], serverData[9])); + parseServer(profile, serverData); } } else { String[] profileData = line.split(";", -1); - new Profile(profileData[0], Boolean.parseBoolean(profileData[1]), Integer.parseInt(profileData[2]), Boolean.parseBoolean(profileData[3])); + parseProfile(profileData); Main.gui().addProfile(profileData[0]); } } + current = getProfile(profileName); } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { - 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); + 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 + ); System.exit(1); } if (profiles.size() == 0) { addProfile("Default"); } } catch (FileNotFoundException e) { - JOptionPane.showMessageDialog(null, "A profiles file was not found. Default profile was created.", "Info", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + "A profiles file was not found. Default profile was created.", + "Info", + JOptionPane.INFORMATION_MESSAGE + ); addProfile("Default"); } Main.gui().update(); @@ -286,4 +379,28 @@ public class Profile { Executors.newSingleThreadExecutor().execute(Server::startServers); } } + + private static Profile parseProfile(String[] profileData) { + return new Profile( + profileData[0], + Boolean.parseBoolean(profileData[1]), + Integer.parseInt(profileData[2]), + Boolean.parseBoolean(profileData[3]) + ); + } + + private static void parseServer(Profile profile, String[] serverData) { + profile.collections.add(new Collection( + serverData[0], + serverData[1], + Boolean.parseBoolean(serverData[2]), + serverData[3], + serverData[4], + serverData[5], + serverData[6], + serverData[7], + serverData[8], + serverData[9]) + ); + } } diff --git a/src/net/knarcraft/serverlauncher/server/AdvancedServerType.java b/src/net/knarcraft/serverlauncher/server/AdvancedServerType.java index a9a157a..300c9d9 100644 --- a/src/net/knarcraft/serverlauncher/server/AdvancedServerType.java +++ b/src/net/knarcraft/serverlauncher/server/AdvancedServerType.java @@ -13,7 +13,7 @@ class AdvancedServerType extends ServerType { private final String srcStart; private final String srcEnd; - public AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) { + AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) { super(name, versions, downloadURL); this.srcStart = srcStart; this.srcEnd = srcEnd; diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 7cee6f6..dadec2c 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -80,6 +80,34 @@ public class Server { this.playerList = new ArrayList<>(); } + public String getName() { + return this.name; + } + + public String getTypeName() { + return this.type.getName(); + } + + public String getServerVersion() { + return this.serverVersion; + } + + public String getPath() { + return this.path; + } + + public Process getProcess() { + return this.process; + } + + public String getMaxRam() { + return this.maxRam; + } + + public static String[] getRamList() { + return ramList; + } + public String getVanillaVersion() { return this.vanillaVersion; } @@ -96,6 +124,38 @@ public class Server { return this.bungeeVersion; } + public ArrayList getPlayers() { + return this.playerList; + } + + /** + * @return A representation of the name of a jarfile. + */ + private String getType() { + if (this.type.getName().equals("Custom")) { + return this.serverVersion; + } else { + return this.type.getName() + this.serverVersion + ".jar"; + } + } + + public boolean isEnabled() { + return this.enabled; + } + + public void toggle(boolean value) { + this.enabled = value; + } + + public boolean hasPlayer(String name) { + for (String player : this.playerList) { + if (player.equals(name)) { + return true; + } + } + return false; + } + public void addPlayer(String name) { this.playerList.add(name); Main.gui().addPlayer(name); @@ -110,46 +170,6 @@ public class Server { Main.gui().removePlayer(name); } - public ArrayList getPlayers() { - return this.playerList; - } - - public boolean hasPlayer(String name) { - for (String player : this.playerList) { - if (player.equals(name)) { - return true; - } - } - return false; - } - - /** - * @return A representation of the name of a jarfile. - */ - private String getType() { - if (this.type.getName().equals("Custom")) { - return this.serverVersion; - } else { - return this.type.getName() + this.serverVersion + ".jar"; - } - } - - public String getName() { - return this.name; - } - - public String getPath() { - return this.path; - } - - public Process getProcess() { - return this.process; - } - - public void toggle(boolean value) { - this.enabled = value; - } - public void setPath(String path) { this.path = path; } @@ -158,44 +178,10 @@ public class Server { this.type = type; } - public static String[] getRamList() { - return ramList; - } - - public boolean isEnabled() { - return this.enabled; - } - public void setMaxRam(String ram) { this.maxRam = ram; } - public static void stop() throws IOException { - for (Collection collection : Profile.getCurrent().getCollections()) { - if (collection.getServer().writer != null) { - if (collection.getServer().type.getName().equals("Bungee")) { - collection.getServer().writer.write("end\n"); - } else { - collection.getServer().writer.write("stop\n"); - } - collection.getServer().writer.flush(); - collection.getServer().writer = null; - } - } - } - - public String typeName() { - return this.type.getName(); - } - - public String getServerVersion() { - return this.serverVersion; - } - - public String getMaxRam() { - return this.maxRam; - } - /** * Sets the server's server version to a valid version, or ignores the request. * @@ -216,6 +202,25 @@ public class Server { } } + /** + * Tries to stop all enabled servers. + * + * @throws IOException If a writer's process is already closed. + */ + public static void stop() throws IOException { + for (Collection collection : Profile.getCurrent().getCollections()) { + if (collection.getServer().writer != null) { + if (collection.getServer().type.getName().equals("Bungee")) { + collection.getServer().writer.write("end\n"); + } else { + collection.getServer().writer.write("stop\n"); + } + collection.getServer().writer.flush(); + collection.getServer().writer = null; + } + } + } + /** * Runs all enabled servers with their settings. */ diff --git a/src/net/knarcraft/serverlauncher/userinterface/Console.java b/src/net/knarcraft/serverlauncher/userinterface/Console.java index 1e6f026..9be1f61 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/Console.java +++ b/src/net/knarcraft/serverlauncher/userinterface/Console.java @@ -42,14 +42,14 @@ public class Console implements ActionListener { textOutput.setLineWrap(true); } - public void output(String text) { - this.textOutput.setText(this.textOutput.getText() + "\n" + text); - } - public JPanel getPanel() { return this.panel; } + public void output(String text) { + this.textOutput.setText(this.textOutput.getText() + "\n" + text); + } + @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name. diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index 0b59e23..ff219a6 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -62,9 +62,17 @@ public class GUI implements ActionListener { * Create the application window. */ public GUI() { - initialize(); - loadMessages(); - this.globalPlayers = new ArrayList<>(); + initialize(); + loadMessages(); + this.globalPlayers = new ArrayList<>(); + } + + public JTabbedPane getPane() { + return this.serversPane; + } + + public void setStatus(String text) { + this.lblStatuslabel.setText(text); } public void addPlayer(String name) { @@ -81,42 +89,6 @@ public class GUI implements ActionListener { this.updatePlayers(); } - /** - * Updates the list of players currently online on the selected server, - */ - private void updatePlayers() { - String selectedServerValue; - Object selectedServer = targetServer.getSelectedItem(); - if (selectedServer != null) { - targetPlayer.removeAllItems(); - selectedServerValue = selectedServer.toString(); - if (selectedServerValue.equals("All")) { - for (String player : this.globalPlayers) targetPlayer.addItem(player); - } else { - for (String player : Profile.getCurrent().getCollection(selectedServerValue).getServer().getPlayers()) - targetPlayer.addItem(player); - } - } - } - - public void update() { - serversPane.removeAll(); - for (Collection collection : Profile.getCurrent().getCollections()) { - serversPane.addTab(collection.getName(), collection.getServerTab().getPanel()); - } - chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground()); - chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0); - chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars()); - } - - public void setStatus(String text) { - this.lblStatuslabel.setText(text); - } - - public JTabbedPane getPane() { - return this.serversPane; - } - public void addProfile(String name) { this.profiles.addItem(name); } @@ -125,10 +97,35 @@ public class GUI implements ActionListener { this.profiles.removeItemAt(index); } + /** + * Updates GUI according to current settings. + */ + public void update() { + serversPane.removeAll(); + for (Collection collection : Profile.getCurrent().getCollections()) { + serversPane.addTab(collection.getName(), collection.getServerTab().getPanel()); + } + chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground()); + chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0); + chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars()); + this.targetServer.removeAllItems(); + this.targetServer.addItem("All"); + for (Collection collection : Profile.getCurrent().getCollections()) { + this.targetServer.addItem(collection.getName()); + } + } + + /** + * Creates the GUI, + */ private void initialize() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) { + } catch (ClassNotFoundException | + UnsupportedLookAndFeelException | + InstantiationException | + IllegalAccessException e + ) { e.printStackTrace(); } @@ -392,15 +389,9 @@ public class GUI implements ActionListener { tray(); } - public void hide() { - frame.setVisible(false); - try { - tray.add(trayIcon); - } catch (AWTException e) { - e.printStackTrace(); - } - } - + /** + * Prepares the system tray if available. + */ private void tray() { if (SystemTray.isSupported()) { tray = SystemTray.getSystemTray(); @@ -409,7 +400,7 @@ public class GUI implements ActionListener { trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup); trayIcon.setImageAutoSize(true); ActionListener exitListener= e -> { - save(); + Profile.getCurrent().save(); System.exit(0); }; @@ -441,7 +432,7 @@ public class GUI implements ActionListener { e1.printStackTrace(); } } else { - save(); + Profile.getCurrent().save(); stop(); System.exit(0); } @@ -462,7 +453,7 @@ public class GUI implements ActionListener { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { - save(); + Profile.getCurrent().save(); stop(); System.exit(0); } @@ -470,11 +461,15 @@ public class GUI implements ActionListener { } } - public void updateSelectServers() { - this.targetServer.removeAllItems(); - this.targetServer.addItem("All"); - for (Collection collection : Profile.getCurrent().getCollections()) { - this.targetServer.addItem(collection.getName()); + /** + * Hides the gui to the tray, + */ + public void hide() { + frame.setVisible(false); + try { + tray.add(trayIcon); + } catch (AWTException e) { + e.printStackTrace(); } } @@ -498,21 +493,46 @@ public class GUI implements ActionListener { } else if (e.getSource() == mntmErrors) { goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Info/"); } else if (e.getSource() == mntmSetup) { - JOptionPane.showMessageDialog(null, setupText, "Setup", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + setupText, + "Setup", + JOptionPane.INFORMATION_MESSAGE + ); } else if (e.getSource() == mntmManualUpdate) { goToURL("https://knarcraft.net/Downloads/Bungeeminecraftserverlauncher/"); } else if (e.getSource() == mntmRunInBackground) { - JOptionPane.showMessageDialog(null, runInBackgroundText, "Run in background", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + runInBackgroundText, + "Run in background", + JOptionPane.INFORMATION_MESSAGE + ); } else if (e.getSource() == mntmDelayStartup) { - JOptionPane.showMessageDialog(null, delayStartupText, "Delay startup", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + delayStartupText, + "Delay startup", + JOptionPane.INFORMATION_MESSAGE + ); } else if (e.getSource() == mntmDownloadJars) { - JOptionPane.showMessageDialog(null, downloadJarsText, "Download jars", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + downloadJarsText, + "Download jars", + JOptionPane.INFORMATION_MESSAGE + ); } else if (e.getSource() == mntmAbout) { - JOptionPane.showMessageDialog(null, aboutText, "About", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog( + null, + aboutText, + "About", + JOptionPane.INFORMATION_MESSAGE + ); } else if (e.getSource() == mntmStory) { goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); } else if (e.getSource() == btnStartServer) { - this.save(); + Profile.getCurrent().save(); Executors.newSingleThreadExecutor().execute(Server::startServers); } else if (e.getSource() == btnStopServer) { stop(); @@ -528,7 +548,7 @@ public class GUI implements ActionListener { } else if (e.getSource() == delProfile) { Object selected = profiles.getSelectedItem(); if (selected != null) { - Profile.deleteProfile(selected.toString()); + Profile.removeProfile(selected.toString()); } } else if (e.getSource() == profiles) { changeProfile(); @@ -566,8 +586,11 @@ public class GUI implements ActionListener { } } + /** + * Saves the previous profile and loads data from the new profile. + */ private void changeProfile() { - save(); + Profile.getCurrent().save(); Object current = profiles.getSelectedItem(); if (current != null) { Profile.setCurrent(current.toString()); @@ -576,13 +599,6 @@ public class GUI implements ActionListener { ServerConsoles.update(); } - /** - * Reads all combo boxes, updates variables (and saves to disk). - */ - private void save() { - Profile.getCurrent().save(); - } - /** * Stops all servers */ @@ -592,7 +608,12 @@ public class GUI implements ActionListener { Server.stop(); setStatus("Servers are stopped"); } catch (IOException e1) { - JOptionPane.showMessageDialog(null, "Could not stop server.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "Could not stop server.", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } @@ -604,12 +625,19 @@ public class GUI implements ActionListener { if (selected != null) { Profile profile = Profile.getProfile(selected.toString()); if (chckbxmntmDelayStartup.isSelected()) { - Objects.requireNonNull(profile).setDelayStartup(Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "))); + Objects.requireNonNull(profile).setDelayStartup( + Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: ")) + ); } else { Objects.requireNonNull(profile).setDelayStartup(0); } } else { - JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "No profile selected", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } @@ -622,7 +650,12 @@ 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); + JOptionPane.showMessageDialog( + null, + "No profile selected", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } @@ -635,7 +668,12 @@ public class GUI implements ActionListener { Profile profile = Profile.getProfile(selected.toString()); Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected()); } else { - JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog( + null, + "No profile selected", + "Error", + JOptionPane.ERROR_MESSAGE + ); } } @@ -673,6 +711,25 @@ public class GUI implements ActionListener { } } + /** + * Updates the list of players currently online on the selected server, + */ + private void updatePlayers() { + String selectedServerValue; + Object selectedServer = targetServer.getSelectedItem(); + if (selectedServer != null) { + targetPlayer.removeAllItems(); + selectedServerValue = selectedServer.toString(); + if (selectedServerValue.equals("All")) { + for (String player : this.globalPlayers) targetPlayer.addItem(player); + } else { + for (String player : Profile.getCurrent().getCollection(selectedServerValue).getServer().getPlayers()) { + targetPlayer.addItem(player); + } + } + } + } + /** * Opens an url in the user's default application. * diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java index dcb9ac9..ef3cef9 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerConsoles.java @@ -29,19 +29,17 @@ public class ServerConsoles { frame.getContentPane().add(consolesTab, BorderLayout.CENTER); } + public static Console addTab(String name) { + return new Console(consolesTab, name); + } + public static void show() { frame.setVisible(true); } - public static void update() { consolesTab.removeAll(); for (Collection collection : Profile.getCurrent().getCollections()) { consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); } } - - public static Console addTab(String name) { - return new Console(consolesTab, name); - } - } \ No newline at end of file