Cleanup and bugfixes

Reformats long lines.
Restructures methods.
Removes some unnecessary methods.
Adds more and better comments.
Checks for illegal characters in collection and profile names.
Profiles.txt saves the profile selected on save.
Adds some methods to avoid code repetition.
Changes the name of at least one method.
I probably forgot to mention something.
This commit is contained in:
2018-02-05 00:00:51 +01:00
parent 9b14603d85
commit b662afe0c6
8 changed files with 477 additions and 279 deletions

View File

@ -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.

View File

@ -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.
*

View File

@ -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);
}
}