diff --git a/src/Main.java b/src/Main.java index d60e468..0ebf079 100644 --- a/src/Main.java +++ b/src/Main.java @@ -37,8 +37,8 @@ class Main { try { String readText = collection.getServer().read(); if (!readText.equals("")) { - //TODO: If the text contains joining or leaving message of player, update playerlist. collection.getServerConsole().output(readText); + updatePlayerList(readText, collection); } } catch (IOException e) { e.printStackTrace(); @@ -60,4 +60,76 @@ class Main { System.exit(1); } } + + private static void updatePlayerList(String text, Collection collection) { + try { + if (!collection.getServer().typeName().equals("Bungee")) { //Bungee servers are not to be treated as normal servers. + String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game"); + if (!joinedPlayer.equals("")) { + if (!collection.getServer().hasPlayer(joinedPlayer)) { + collection.getServer().addPlayer(joinedPlayer); + } + } else { + joinedPlayer = stringBetween(text, "UUID of player ", " is "); + if (!joinedPlayer.equals("")) { + if (!collection.getServer().hasPlayer(joinedPlayer)) { + collection.getServer().addPlayer(joinedPlayer); + } + } + } + if (joinedPlayer.equals("")) { + String leftPlayer = stringBetween(text, "INFO]: ", " lost connection"); + if (!leftPlayer.equals("")) { + if (collection.getServer().hasPlayer(leftPlayer)) { + collection.getServer().removePlayer(leftPlayer); + } + } else { + leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game"); + if (!leftPlayer.equals("")) { + if (collection.getServer().hasPlayer(leftPlayer)) { + collection.getServer().removePlayer(leftPlayer); + } + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + /* + If Not ($PlayersArray = "BUNGEE") Then + $NewDataLine = StringSplit($NewData, @CRLF, 1) + For $x = 0 To UBound($NewDataLine) - 1 + $NewPlayer = _StringBetween($NewDataLine[$x], "[Server thread/INFO]: ", " joined the game", 1, True) + If Not IsArray($NewPlayer) Then $NewPlayer = _StringBetween($NewDataLine[$x], "UUID of player ", " is ", 1, True) + $LeftPlayer = _StringBetween($NewDataLine[$x], "INFO]: ", " lost connection", 1, True) + If Not IsArray($LeftPlayer) Then $LeftPlayer = _StringBetween($NewDataLine[$x], "[Server thread/INFO]: ", " left the game", 1, True) + If IsArray($NewPlayer) Then + If _ArraySearch($PlayersArray, StringReplace($NewPlayer[0], " ", "")) = -1 Then + _ArrayAdd($PlayersArray, StringReplace($NewPlayer[0], " ", "")) + EndIf + EndIf + If IsArray($LeftPlayer) Then + _ArrayDelete($PlayersArray, _ArraySearch($PlayersArray, $LeftPlayer[0])) + EndIf + Next + EndIf + */ + } + + /** + * Finds a substring between two substrings in a string. + * + * @param string The string containing the substrings + * @param start The substring before the wanted substring + * @param end The substring after the wanted substring + * @return The wanted substring. + */ + private static String stringBetween(String string, String start, String end) throws StringIndexOutOfBoundsException { + int startPos = string.indexOf(start) + start.length(); + if (string.indexOf(end, startPos) < startPos) { + return ""; + } + return string.substring(startPos, string.indexOf(end, startPos)); + } } \ No newline at end of file diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index 24f8b6a..b71cbb1 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -39,18 +39,20 @@ public class Profile { } public void addCollection(String name) { - if (!collectionExists(name)) { + if (!collectionExists(name) && !name.equals("") && !name.equals("All")) { collections.add(new Collection(name)); + GUI.getGUI().updateSelectServers(); } else { - JOptionPane.showMessageDialog(null, "A server name must my unique and not empty.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE); } } public void removeCollection(int i) { this.collections.remove(i); + GUI.getGUI().updateSelectServers(); } - private Collection getCollection(String name) { + public Collection getCollection(String name) { for (Collection collection : this.collections) { if (collection.getName().equals(name)) { return collection; diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 3f1a54e..028fbe5 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -50,15 +50,29 @@ public class Server { public void addPlayer(String name) { this.playerList.add(name); + GUI.getGUI().addPlayer(name); } public void removePlayer(String name) { for (int i = 0; i < playerList.size(); i++) { if (name.equals(playerList.get(i))) { playerList.remove(i); - return; } } + GUI.getGUI().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; } /** @@ -122,6 +136,10 @@ public class Server { } } + public String typeName() { + return this.type.getName(); + } + /** * Sets the server's server version to a valid version, or ignores the request. * diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index 3c7293f..ec20756 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -42,7 +42,8 @@ public class GUI implements ActionListener { private JComboBox profiles; private final JLabel lblStatuslabel = new JLabel("Servers are stopped"); //Server controls - private JComboBox targetServer, targetPlayer; + private JComboBox targetServer; + private JComboBox targetPlayer; private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles; private JTextField customCommand; //Text @@ -52,13 +53,13 @@ public class GUI implements ActionListener { private String downloadJarsText; private String aboutText; + private final ArrayList globalPlayers; + private JTabbedPane serversPane; private final ArrayList serverTabs = new ArrayList<>(); - //TODO: Update target server list with the list of servers when adding or removing a server. - /** * Create the application window. */ @@ -66,6 +67,36 @@ public class GUI implements ActionListener { initialize(); loadMessages(); gui = this; + this.globalPlayers = new ArrayList<>(); + } + + public void addPlayer(String name) { + this.globalPlayers.add(name); + this.updatePlayers(); + } + + public void removePlayer(String name) { + for (int i = 0; i < this.globalPlayers.size(); i++) { + if (this.globalPlayers.get(i).equals(name)) { + this.globalPlayers.remove(i); + } + } + this.updatePlayers(); + } + + 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 : this.currentProfile().getCollection(selectedServerValue).getServer().getPlayers()) + targetPlayer.addItem(player); + } + } } public void setStatus(String text) { @@ -112,7 +143,6 @@ public class GUI implements ActionListener { i = this.serverTabs.size(); } } - } private void initialize() { @@ -274,11 +304,11 @@ public class GUI implements ActionListener { SpringLayout sl_panel_1 = new SpringLayout(); controlServers.setLayout(sl_panel_1); - targetServer = new JComboBox(); + targetServer = new JComboBox<>(); sl_panel_1.putConstraint(SpringLayout.NORTH, targetServer, 10, SpringLayout.NORTH, controlServers); controlServers.add(targetServer); - targetPlayer = new JComboBox(); + targetPlayer = new JComboBox<>(); sl_panel_1.putConstraint(SpringLayout.NORTH, targetPlayer, 6, SpringLayout.SOUTH, targetServer); targetPlayer.setEditable(true); controlServers.add(targetPlayer); @@ -334,6 +364,7 @@ public class GUI implements ActionListener { sl_panel_1.putConstraint(SpringLayout.EAST, btnCustomCommand, 0, SpringLayout.EAST, btnOp); controlServers.add(btnCustomCommand); btnCustomCommand.addActionListener(this); + frame.getRootPane().setDefaultButton(btnCustomCommand); customCommand = new JTextField(); sl_panel_1.putConstraint(SpringLayout.WEST, customCommand, 10, SpringLayout.WEST, controlServers); @@ -391,6 +422,14 @@ public class GUI implements ActionListener { serverTabs.add(serverTab); } + public void updateSelectServers() { + this.targetServer.removeAllItems(); + this.targetServer.addItem("All"); + for (Collection collection : currentProfile().getCollections()) { + this.targetServer.addItem(collection.getName()); + } + } + @Override public void actionPerformed(ActionEvent e) { String selectedServerValue = null, selectedPlayerValue = null; @@ -459,7 +498,8 @@ public class GUI implements ActionListener { } } else if (e.getSource() == btnCustomCommand) { if (selectedServerValue != null) { - currentProfile().sendCommand(selectedServerValue, customCommand.getSelectedText()); + currentProfile().sendCommand(selectedServerValue, customCommand.getText()); + customCommand.setText(""); } } else if (e.getSource() == btnSaveserver) { if (selectedServerValue != null) {