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:
Kristian Knarvik 2018-02-05 00:00:51 +01:00
parent 9b14603d85
commit b662afe0c6
8 changed files with 477 additions and 279 deletions

View File

@ -83,7 +83,7 @@ public class Main {
*/ */
private static void updatePlayerList(String text, Collection collection) { private static void updatePlayerList(String text, Collection collection) {
Server server = collection.getServer(); Server server = collection.getServer();
if (!server.typeName().equals("Bungee")) { if (!server.getTypeName().equals("Bungee")) {
String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game"); String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
if (!joinedPlayer.equals("")) { if (!joinedPlayer.equals("")) {
if (!server.hasPlayer(joinedPlayer)) { if (!server.hasPlayer(joinedPlayer)) {

View File

@ -22,9 +22,30 @@ public class Collection {
this.name = name; 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.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.serverConsole = ServerConsoles.addTab(name);
this.name = name; this.name = name;
this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam); this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam);

View File

@ -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<Collection> 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() { public boolean getRunInBackground() {
return this.runInBackground; return this.runInBackground;
} }
@ -162,6 +64,42 @@ public class Profile {
return this.downloadJars; return this.downloadJars;
} }
public static Profile getCurrent() {
return current;
}
public ArrayList<Collection> 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) { public void setDelayStartup(int value) {
if (value >= 0) { if (value >= 0) {
this.delayStartup = value; this.delayStartup = value;
@ -172,6 +110,97 @@ public class Profile {
this.downloadJars = value; 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 * Sends a command to a server, or all servers
* *
@ -184,7 +213,12 @@ public class Profile {
try { try {
collection.getServer().sendCommand(command); collection.getServer().sendCommand(command);
} catch (IOException e) { } 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 { } else {
@ -194,10 +228,19 @@ public class Profile {
try { try {
target.sendCommand(command); target.sendCommand(command);
} catch (IOException e) { } 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 { } 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 { try {
server.setServerVersion(serverTab.getVersion()); server.setServerVersion(serverTab.getVersion());
} catch (IllegalArgumentException e) { } 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()); server.toggle(serverTab.enabled());
} }
try (PrintWriter file = new PrintWriter("files/Profiles.txt")) { try (PrintWriter file = new PrintWriter("files/Profiles.txt")) {
file.print(""); file.println(current.name);
file.close();
for (Profile profile : profiles) { 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) { for (Collection collection : profile.collections) {
Server server = collection.getServer(); 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)); 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); fileAppend.println(saveString);
} catch (IOException e) { } 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) { } 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() { public static void load() {
try (Scanner in = new Scanner(new File("files/Profiles.txt"))) { try (Scanner in = new Scanner(new File("files/Profiles.txt"))) {
String profileName = in.nextLine();
try { try {
while (in.hasNextLine()) { while (in.hasNextLine()) {
String line = in.nextLine(); String line = in.nextLine();
if (line.contains("?")) { if (line.contains("?")) {
String[] data = line.split("\\?"); String[] data = line.split("\\?");
String[] profileData = data[0].split(";", -1); 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]); Main.gui().addProfile(profileData[0]);
if (data[1].contains("!")) { if (data[1].contains("!")) {
String[] servers = data[1].split("!", -1); String[] servers = data[1].split("!", -1);
for (String server : servers) { for (String server : servers) {
String[] serverData = server.split(";", -1); 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 { } else {
String[] serverData = data[1].split(";", -1); 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 { } else {
String[] profileData = line.split(";", -1); 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]); Main.gui().addProfile(profileData[0]);
} }
} }
current = getProfile(profileName);
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { } 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); System.exit(1);
} }
if (profiles.size() == 0) { if (profiles.size() == 0) {
addProfile("Default"); addProfile("Default");
} }
} catch (FileNotFoundException e) { } 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"); addProfile("Default");
} }
Main.gui().update(); Main.gui().update();
@ -286,4 +379,28 @@ public class Profile {
Executors.newSingleThreadExecutor().execute(Server::startServers); 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])
);
}
} }

View File

@ -13,7 +13,7 @@ class AdvancedServerType extends ServerType {
private final String srcStart; private final String srcStart;
private final String srcEnd; 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); super(name, versions, downloadURL);
this.srcStart = srcStart; this.srcStart = srcStart;
this.srcEnd = srcEnd; this.srcEnd = srcEnd;

View File

@ -80,6 +80,34 @@ public class Server {
this.playerList = new ArrayList<>(); 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() { public String getVanillaVersion() {
return this.vanillaVersion; return this.vanillaVersion;
} }
@ -96,6 +124,38 @@ public class Server {
return this.bungeeVersion; return this.bungeeVersion;
} }
public ArrayList<String> 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) { public void addPlayer(String name) {
this.playerList.add(name); this.playerList.add(name);
Main.gui().addPlayer(name); Main.gui().addPlayer(name);
@ -110,46 +170,6 @@ public class Server {
Main.gui().removePlayer(name); Main.gui().removePlayer(name);
} }
public ArrayList<String> 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) { public void setPath(String path) {
this.path = path; this.path = path;
} }
@ -158,44 +178,10 @@ public class Server {
this.type = type; this.type = type;
} }
public static String[] getRamList() {
return ramList;
}
public boolean isEnabled() {
return this.enabled;
}
public void setMaxRam(String ram) { public void setMaxRam(String ram) {
this.maxRam = 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. * 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. * Runs all enabled servers with their settings.
*/ */

View File

@ -42,14 +42,14 @@ public class Console implements ActionListener {
textOutput.setLineWrap(true); textOutput.setLineWrap(true);
} }
public void output(String text) {
this.textOutput.setText(this.textOutput.getText() + "\n" + text);
}
public JPanel getPanel() { public JPanel getPanel() {
return this.panel; return this.panel;
} }
public void output(String text) {
this.textOutput.setText(this.textOutput.getText() + "\n" + text);
}
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name. 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. * Create the application window.
*/ */
public GUI() { public GUI() {
initialize(); initialize();
loadMessages(); loadMessages();
this.globalPlayers = new ArrayList<>(); this.globalPlayers = new ArrayList<>();
}
public JTabbedPane getPane() {
return this.serversPane;
}
public void setStatus(String text) {
this.lblStatuslabel.setText(text);
} }
public void addPlayer(String name) { public void addPlayer(String name) {
@ -81,42 +89,6 @@ public class GUI implements ActionListener {
this.updatePlayers(); 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) { public void addProfile(String name) {
this.profiles.addItem(name); this.profiles.addItem(name);
} }
@ -125,10 +97,35 @@ public class GUI implements ActionListener {
this.profiles.removeItemAt(index); 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() { private void initialize() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) { } catch (ClassNotFoundException |
UnsupportedLookAndFeelException |
InstantiationException |
IllegalAccessException e
) {
e.printStackTrace(); e.printStackTrace();
} }
@ -392,15 +389,9 @@ public class GUI implements ActionListener {
tray(); tray();
} }
public void hide() { /**
frame.setVisible(false); * Prepares the system tray if available.
try { */
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
private void tray() { private void tray() {
if (SystemTray.isSupported()) { if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray(); tray = SystemTray.getSystemTray();
@ -409,7 +400,7 @@ public class GUI implements ActionListener {
trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup); trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup);
trayIcon.setImageAutoSize(true); trayIcon.setImageAutoSize(true);
ActionListener exitListener= e -> { ActionListener exitListener= e -> {
save(); Profile.getCurrent().save();
System.exit(0); System.exit(0);
}; };
@ -441,7 +432,7 @@ public class GUI implements ActionListener {
e1.printStackTrace(); e1.printStackTrace();
} }
} else { } else {
save(); Profile.getCurrent().save();
stop(); stop();
System.exit(0); System.exit(0);
} }
@ -462,7 +453,7 @@ public class GUI implements ActionListener {
frame.addWindowListener(new WindowAdapter() { frame.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
save(); Profile.getCurrent().save();
stop(); stop();
System.exit(0); System.exit(0);
} }
@ -470,11 +461,15 @@ public class GUI implements ActionListener {
} }
} }
public void updateSelectServers() { /**
this.targetServer.removeAllItems(); * Hides the gui to the tray,
this.targetServer.addItem("All"); */
for (Collection collection : Profile.getCurrent().getCollections()) { public void hide() {
this.targetServer.addItem(collection.getName()); 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) { } else if (e.getSource() == mntmErrors) {
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Info/"); goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Info/");
} else if (e.getSource() == mntmSetup) { } 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) { } else if (e.getSource() == mntmManualUpdate) {
goToURL("https://knarcraft.net/Downloads/Bungeeminecraftserverlauncher/"); goToURL("https://knarcraft.net/Downloads/Bungeeminecraftserverlauncher/");
} else if (e.getSource() == mntmRunInBackground) { } 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) { } 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) { } 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) { } 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) { } else if (e.getSource() == mntmStory) {
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/");
} else if (e.getSource() == btnStartServer) { } else if (e.getSource() == btnStartServer) {
this.save(); Profile.getCurrent().save();
Executors.newSingleThreadExecutor().execute(Server::startServers); Executors.newSingleThreadExecutor().execute(Server::startServers);
} else if (e.getSource() == btnStopServer) { } else if (e.getSource() == btnStopServer) {
stop(); stop();
@ -528,7 +548,7 @@ public class GUI implements ActionListener {
} else if (e.getSource() == delProfile) { } else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
Profile.deleteProfile(selected.toString()); Profile.removeProfile(selected.toString());
} }
} else if (e.getSource() == profiles) { } else if (e.getSource() == profiles) {
changeProfile(); changeProfile();
@ -566,8 +586,11 @@ public class GUI implements ActionListener {
} }
} }
/**
* Saves the previous profile and loads data from the new profile.
*/
private void changeProfile() { private void changeProfile() {
save(); Profile.getCurrent().save();
Object current = profiles.getSelectedItem(); Object current = profiles.getSelectedItem();
if (current != null) { if (current != null) {
Profile.setCurrent(current.toString()); Profile.setCurrent(current.toString());
@ -576,13 +599,6 @@ public class GUI implements ActionListener {
ServerConsoles.update(); ServerConsoles.update();
} }
/**
* Reads all combo boxes, updates variables (and saves to disk).
*/
private void save() {
Profile.getCurrent().save();
}
/** /**
* Stops all servers * Stops all servers
*/ */
@ -592,7 +608,12 @@ public class GUI implements ActionListener {
Server.stop(); Server.stop();
setStatus("Servers are stopped"); setStatus("Servers are stopped");
} catch (IOException e1) { } 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) { if (selected != null) {
Profile profile = Profile.getProfile(selected.toString()); Profile profile = Profile.getProfile(selected.toString());
if (chckbxmntmDelayStartup.isSelected()) { 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 { } else {
Objects.requireNonNull(profile).setDelayStartup(0); Objects.requireNonNull(profile).setDelayStartup(0);
} }
} else { } 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()); Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected()); Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else { } 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()); Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected()); Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected());
} else { } 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. * Opens an url in the user's default application.
* *

View File

@ -29,19 +29,17 @@ public class ServerConsoles {
frame.getContentPane().add(consolesTab, BorderLayout.CENTER); frame.getContentPane().add(consolesTab, BorderLayout.CENTER);
} }
public static Console addTab(String name) {
return new Console(consolesTab, name);
}
public static void show() { public static void show() {
frame.setVisible(true); frame.setVisible(true);
} }
public static void update() { public static void update() {
consolesTab.removeAll(); consolesTab.removeAll();
for (Collection collection : Profile.getCurrent().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); consolesTab.add(collection.getName(), collection.getServerConsole().getPanel());
} }
} }
public static Console addTab(String name) {
return new Console(consolesTab, name);
}
} }