Does some much needed cleanup, adds the Paper server type and fixes broken downloads
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit

This commit is contained in:
2020-08-06 19:55:27 +02:00
parent 9ef314b178
commit 26cc5370e2
30 changed files with 1722 additions and 1557 deletions

View File

@ -1,799 +1,45 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.Shared;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.Server;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
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.Shared.getResourceAsScanner;
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsStream;
/**
* Generates a GUI.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
* Describes a generic GUI
*/
public class GUI implements ActionListener {
private JFrame frame;
private JTabbedPane tabbedPane;
private JTabbedPane serversPane;
//Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
private JMenuItem mntmErrors, mntmSetup, mntmManualUpdate; //Help
private JMenuItem mntmRunInBackground, mntmDelayStartup, mntmDownloadJars; //Info/options
private JMenuItem mntmAbout, mntmStory; //Info/about
//Basic controls
private JButton btnStartServer, btnStopServer, addServer, backup, addProfile, delProfile;
private JComboBox<String> profiles;
private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
//Server controls
private JComboBox<String> targetServer;
private JComboBox<String> targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;
//Text
private String setupText;
private String runInBackgroundText;
private String delayStartupText;
private String downloadJarsText;
private String aboutText;
private final ArrayList<String> globalPlayers;
private SystemTray tray;
private TrayIcon trayIcon;
public interface GUI {
/**
* Creates the application window
*/
public GUI() throws IOException {
initialize(440, 170);
loadMessages();
this.globalPlayers = new ArrayList<>();
}
/**
* Creates the application window with a preferred width and height
* @param width <p>The preferred width</p>
* @param height <p>The preferred height</p>
*/
public GUI(int width, int height) throws IOException {
initialize(width, height);
loadMessages();
this.globalPlayers = new ArrayList<>();
}
/**
* Gets the pane used for server configurations
* Displays a message to the user in the GUI message field
*
* @return A JTabbedPane
* @param message <p>The message contents</p>
*/
public JTabbedPane getPane() {
return this.serversPane;
}
void printMessageToGUI(String message);
/**
* Sets the text of the status label.
* Displays an error to the user as an independent box
*
* @param text The new text
* @param title <p>The title of the error message</p>
* @param message <p>The error message contents</p>
*/
public void setStatus(String text) {
this.lblStatuslabel.setText(text);
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", true))) {
file.println(text);
} catch (IOException e ) {
e.printStackTrace();
}
}
void showError(String title, String message);
/**
* Adds a player to the global playerlist, and updates the players combo.
* Displays an error to the user as an independent box
*
* @param name The name of the player to add
* @param message <p>The error message contents</p>
*/
public void addPlayer(String name) {
this.globalPlayers.add(name);
this.updatePlayers();
}
void showError(String message);
/**
* Removes a player from the global list of players.
* Displays a message to the user as an independent box
*
* @param name The name of the player to remove.
* @param title <p>The title of the message</p>
* @param message <p>The message contents</p>
*/
public void removePlayer(String name) {
globalPlayers.removeIf(playerName -> playerName.equals(name));
this.updatePlayers();
}
void showMessage(String title, String message);
/**
* Updates the profiles combo.
*/
public void updateProfiles() {
this.profiles.removeAllItems();
for (Profile profile : Profile.getProfiles()) {
this.profiles.addItem(profile.getName());
}
}
/**
* Gets the size of the main JFrame
* Displays a message to the user as an independent box
*
* @return The Dimension of the frame
* @param message <p>The message contents</p>
*/
public Dimension getSize() {
return frame.getContentPane().getSize();
}
void showMessage(String message);
/**
* Updates GUI according to current profile 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().getDownloadAllAvailableJARFiles());
this.targetServer.removeAllItems();
this.targetServer.addItem("All");
for (Collection collection : Profile.getCurrent().getCollections()) {
this.targetServer.addItem(collection.getName());
}
}
/**
* Creates the GUI,
*/
private void initialize(int width, int height) throws IOException {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException |
UnsupportedLookAndFeelException |
InstantiationException |
IllegalAccessException e
) {
e.printStackTrace();
}
frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
ImageIcon img = new ImageIcon(ImageIO.read(getResourceAsStream("GUIIcon.png")));
frame.setIconImage(img.getImage());
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
chckbxmntmRunInBackground = new JCheckBoxMenuItem("Run in background on exit");
mnOptions.add(chckbxmntmRunInBackground);
chckbxmntmRunInBackground.addActionListener(this);
chckbxmntmDelayStartup = new JCheckBoxMenuItem("Delay Startup");
mnOptions.add(chckbxmntmDelayStartup);
chckbxmntmDelayStartup.addActionListener(this);
chckbxmntmDownloadJars = new JCheckBoxMenuItem("Download jars");
mnOptions.add(chckbxmntmDownloadJars);
chckbxmntmDownloadJars.addActionListener(this);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
mntmErrors = new JMenuItem("Errors");
mnHelp.add(mntmErrors);
mntmErrors.addActionListener(this);
mntmSetup = new JMenuItem("Setup");
mnHelp.add(mntmSetup);
mntmSetup.addActionListener(this);
mntmManualUpdate = new JMenuItem("Manual update");
mnHelp.add(mntmManualUpdate);
mntmManualUpdate.addActionListener(this);
JMenu mnInfo = new JMenu("Info");
menuBar.add(mnInfo);
JMenu mnOptionsInfo = new JMenu("Options");
mnInfo.add(mnOptionsInfo);
mntmRunInBackground = new JMenuItem("Run in background on exit");
mnOptionsInfo.add(mntmRunInBackground);
mntmRunInBackground.addActionListener(this);
mntmDelayStartup = new JMenuItem("Delay Startup");
mnOptionsInfo.add(mntmDelayStartup);
mntmDelayStartup.addActionListener(this);
mntmDownloadJars = new JMenuItem("Download jars");
mnOptionsInfo.add(mntmDownloadJars);
mntmDownloadJars.addActionListener(this);
JMenu mnAbout = new JMenu("About");
mnInfo.add(mnAbout);
mntmAbout = new JMenuItem("About");
mnAbout.add(mntmAbout);
mntmAbout.addActionListener(this);
mntmStory = new JMenuItem("Story");
mnAbout.add(mntmStory);
mntmStory.addActionListener(this);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane);
JPanel panelBasic = new JPanel();
tabbedPane.addTab("Control panel", null, panelBasic, null);
SpringLayout sl_panel = new SpringLayout();
panelBasic.setLayout(sl_panel);
JLabel lblBasicControls = new JLabel("Basic controls");
sl_panel.putConstraint(SpringLayout.NORTH, lblBasicControls, 10, SpringLayout.NORTH, panelBasic);
panelBasic.add(lblBasicControls);
btnStartServer = new JButton("Start servers");
sl_panel.putConstraint(SpringLayout.WEST, lblBasicControls, 0, SpringLayout.WEST, btnStartServer);
sl_panel.putConstraint(SpringLayout.NORTH, btnStartServer, 6, SpringLayout.SOUTH, lblBasicControls);
sl_panel.putConstraint(SpringLayout.WEST, btnStartServer, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(btnStartServer);
btnStartServer.addActionListener(this);
btnStopServer = new JButton("Stop servers");
sl_panel.putConstraint(SpringLayout.NORTH, btnStopServer, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, btnStopServer, 6, SpringLayout.EAST, btnStartServer);
panelBasic.add(btnStopServer);
btnStopServer.addActionListener(this);
JLabel lblProfile = new JLabel("Profile");
sl_panel.putConstraint(SpringLayout.NORTH, lblProfile, 6, SpringLayout.SOUTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, lblProfile, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(lblProfile);
addProfile = new JButton("+");
sl_panel.putConstraint(SpringLayout.NORTH, addProfile, 6, SpringLayout.SOUTH, lblProfile);
sl_panel.putConstraint(SpringLayout.WEST, addProfile, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(addProfile);
addProfile.addActionListener(this);
delProfile = new JButton("-");
sl_panel.putConstraint(SpringLayout.NORTH, delProfile, 0, SpringLayout.NORTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, delProfile, 6, SpringLayout.EAST, addProfile);
panelBasic.add(delProfile);
delProfile.addActionListener(this);
profiles = new JComboBox<>();
sl_panel.putConstraint(SpringLayout.NORTH, profiles, 0, SpringLayout.NORTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, profiles, 6, SpringLayout.EAST, delProfile);
sl_panel.putConstraint(SpringLayout.EAST, profiles, 124, SpringLayout.EAST, delProfile);
panelBasic.add(profiles);
profiles.addActionListener(this);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 6, SpringLayout.SOUTH, addProfile);
sl_panel.putConstraint(SpringLayout.SOUTH, lblStatuslabel, -10, SpringLayout.SOUTH, panelBasic);
sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, -10, SpringLayout.EAST, panelBasic);
panelBasic.add(lblStatuslabel);
addServer = new JButton("Add server");
sl_panel.putConstraint(SpringLayout.NORTH, addServer, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, addServer, 6, SpringLayout.EAST, btnStopServer);
panelBasic.add(addServer);
addServer.addActionListener(this);
backup = new JButton("Backup");
sl_panel.putConstraint(SpringLayout.NORTH, backup, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, backup, 6, SpringLayout.EAST, addServer);
panelBasic.add(backup);
backup.addActionListener(this);
JPanel controlServers = new JPanel();
tabbedPane.addTab("Control servers", null, controlServers, null);
SpringLayout sl_panel_1 = new SpringLayout();
controlServers.setLayout(sl_panel_1);
targetServer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetServer, 10, SpringLayout.NORTH, controlServers);
controlServers.add(targetServer);
targetServer.addActionListener(this);
targetPlayer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetPlayer, 6, SpringLayout.SOUTH, targetServer);
targetPlayer.setEditable(true);
controlServers.add(targetPlayer);
btnKick = new JButton("Kick");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnKick, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.WEST, btnKick, 6, SpringLayout.EAST, targetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnKick, 104, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.SOUTH, targetServer, 0, SpringLayout.SOUTH, btnKick);
controlServers.add(btnKick);
btnKick.addActionListener(this);
btnBan = new JButton("Ban");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnBan, 6, SpringLayout.SOUTH, btnKick);
sl_panel_1.putConstraint(SpringLayout.WEST, btnBan, 6, SpringLayout.EAST, targetPlayer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnBan, 104, SpringLayout.WEST, btnBan);
sl_panel_1.putConstraint(SpringLayout.SOUTH, targetPlayer, 0, SpringLayout.SOUTH, btnBan);
controlServers.add(btnBan);
btnBan.addActionListener(this);
btnOp = new JButton("OP");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnOp, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.WEST, btnOp, 6, SpringLayout.EAST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnOp, -10, SpringLayout.EAST, controlServers);
controlServers.add(btnOp);
btnOp.addActionListener(this);
btnDeop = new JButton("DEOP");
sl_panel_1.putConstraint(SpringLayout.WEST, btnDeop, 6, SpringLayout.EAST, btnBan);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnDeop, 5, SpringLayout.SOUTH, btnOp);
sl_panel_1.putConstraint(SpringLayout.EAST, btnDeop, -10, SpringLayout.EAST, controlServers);
controlServers.add(btnDeop);
btnDeop.addActionListener(this);
JLabel lblTargetServer = new JLabel("Target server");
sl_panel_1.putConstraint(SpringLayout.WEST, targetServer, 6, SpringLayout.EAST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, targetServer, 121, SpringLayout.EAST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.NORTH, lblTargetServer, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.SOUTH, lblTargetServer, 0, SpringLayout.SOUTH, targetServer);
sl_panel_1.putConstraint(SpringLayout.WEST, lblTargetServer, 10, SpringLayout.WEST, controlServers);
controlServers.add(lblTargetServer);
JLabel lblTargetPlayer = new JLabel("Target player");
sl_panel_1.putConstraint(SpringLayout.WEST, targetPlayer, 7, SpringLayout.EAST, lblTargetPlayer);
sl_panel_1.putConstraint(SpringLayout.EAST, targetPlayer, 122, SpringLayout.EAST, lblTargetPlayer);
sl_panel_1.putConstraint(SpringLayout.NORTH, lblTargetPlayer, 6, SpringLayout.SOUTH, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.SOUTH, lblTargetPlayer, 0, SpringLayout.SOUTH, targetPlayer);
sl_panel_1.putConstraint(SpringLayout.WEST, lblTargetPlayer, 0, SpringLayout.WEST, lblTargetServer);
controlServers.add(lblTargetPlayer);
btnCustomCommand = new JButton("Custom command");
sl_panel_1.putConstraint(SpringLayout.WEST, btnCustomCommand, 250, SpringLayout.WEST, controlServers);
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);
sl_panel_1.putConstraint(SpringLayout.EAST, customCommand, -6, SpringLayout.WEST, btnCustomCommand);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnCustomCommand, 0, SpringLayout.NORTH, customCommand);
sl_panel_1.putConstraint(SpringLayout.SOUTH, customCommand, 0, SpringLayout.SOUTH, btnCustomCommand);
controlServers.add(customCommand);
customCommand.setColumns(10);
btnSaveserver = new JButton("Save server");
sl_panel_1.putConstraint(SpringLayout.NORTH, customCommand, 6, SpringLayout.SOUTH, btnSaveserver);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnSaveserver, 6, SpringLayout.SOUTH, btnBan);
sl_panel_1.putConstraint(SpringLayout.WEST, btnSaveserver, 0, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick);
controlServers.add(btnSaveserver);
btnSaveserver.addActionListener(this);
btnReload = new JButton("Reload");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnReload, 6, SpringLayout.SOUTH, btnDeop);
sl_panel_1.putConstraint(SpringLayout.WEST, btnReload, 0, SpringLayout.WEST, btnDeop);
sl_panel_1.putConstraint(SpringLayout.EAST, btnReload, 0, SpringLayout.EAST, btnOp);
controlServers.add(btnReload);
btnReload.addActionListener(this);
btnServerConsoles = new JButton("View server consoles");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnServerConsoles, 0, SpringLayout.NORTH, btnSaveserver);
sl_panel_1.putConstraint(SpringLayout.WEST, btnServerConsoles, 0, SpringLayout.WEST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnServerConsoles, 0, SpringLayout.EAST, targetServer);
controlServers.add(btnServerConsoles);
btnServerConsoles.addActionListener(this);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("Servers", null, panel_2, null);
SpringLayout sl_panel_2 = new SpringLayout();
panel_2.setLayout(sl_panel_2);
JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
sl_panel_2.putConstraint(SpringLayout.NORTH, tabbedPane_1, 0, SpringLayout.NORTH, panel_2);
sl_panel_2.putConstraint(SpringLayout.WEST, tabbedPane_1, 0, SpringLayout.WEST, panel_2);
sl_panel_2.putConstraint(SpringLayout.SOUTH, tabbedPane_1, 0, SpringLayout.SOUTH, panel_2);
sl_panel_2.putConstraint(SpringLayout.EAST, tabbedPane_1, 0, SpringLayout.EAST, panel_2);
panel_2.add(tabbedPane_1);
this.serversPane = tabbedPane_1;
tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
frame.validate();
frame.pack();
frame.setVisible(true);
tray();
updateRunning(false);
}
/**
* Prepares the system tray if available.
*/
private void tray() {
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
Image trayImage = Toolkit.getDefaultToolkit().getImage("files/GUIIcon.png");
PopupMenu popup = new PopupMenu();
trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup);
trayIcon.setImageAutoSize(true);
ActionListener exitListener= e -> {
stop();
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.exit(0);
};
MenuItem restoreItem = new MenuItem("Restore");
popup.add(restoreItem);
restoreItem.addActionListener(e -> {
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
});
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
popup.add(exitItem);
frame.addWindowStateListener(e -> {
if (e.getNewState() == NORMAL) {
tray.remove(trayIcon);
frame.setVisible(true);
}
});
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (Profile.getCurrent().getRunInBackground() && SystemTray.isSupported()) {
try {
tray.add(trayIcon);
frame.setVisible(false);
} catch (AWTException e1) {
e1.printStackTrace();
}
} else {
stop();
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
});
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getClickCount() >= 1 && e.getButton() == MouseEvent.BUTTON1){
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
}
}
});
} else {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
stop();
System.exit(0);
}
});
}
}
/**
* Hides the gui to the tray,
*/
public void hide() {
frame.setVisible(false);
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String selectedServerValue = null, selectedPlayerValue = null;
Object selectedServer = targetServer.getSelectedItem();
if (selectedServer != null) {
selectedServerValue = selectedServer.toString();
}
Object selectedPlayer = targetPlayer.getSelectedItem();
if (selectedPlayer != null) {
selectedPlayerValue = selectedPlayer.toString();
}
if (e.getSource() == chckbxmntmRunInBackground) {
background();
} else if (e.getSource() == chckbxmntmDelayStartup) {
delay();
} else if (e.getSource() == chckbxmntmDownloadJars) {
downloadJars();
} else if (e.getSource() == mntmErrors) {
Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherInfo/");
} else if (e.getSource() == mntmSetup) {
Profile.showMessage("Setup", setupText);
} else if (e.getSource() == mntmManualUpdate) {
Shared.goToURL("https://git.knarcraft.net/KnarCraft/Minecraft-Server-Launcher/releases");
} else if (e.getSource() == mntmRunInBackground) {
Profile.showMessage("Run in background", runInBackgroundText);
} else if (e.getSource() == mntmDelayStartup) {
Profile.showMessage("Delay startup", delayStartupText);
} else if (e.getSource() == mntmDownloadJars) {
Profile.showMessage("Download jars", downloadJarsText);
} else if (e.getSource() == mntmAbout) {
Profile.showMessage("About", aboutText);
} else if (e.getSource() == mntmStory) {
Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherStory/");
} else if (e.getSource() == btnStartServer) {
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Executors.newSingleThreadExecutor().execute(Server::startServers);
} else if (e.getSource() == btnStopServer) {
stop();
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
Profile.getCurrent().addCollection(serverName);
this.update();
Profile.getCurrent().updateConsoles();
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
updateProfiles();
} else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile.removeProfile(selected.toString());
updateProfiles();
}
} else if (e.getSource() == profiles) {
try {
changeProfile();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
}
} else if (e.getSource() == btnBan) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
}
} else if (e.getSource() == btnOp) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
}
} else if (e.getSource() == btnDeop) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
}
} else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, customCommand.getText());
customCommand.setText("");
}
} else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "save-all");
}
} else if (e.getSource() == btnReload) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
ServerConsoles.setAsVisible();
} else if (e.getSource() == targetServer) {
updatePlayers();
}
}
/**
* Updates the GUI components to block a user from doing illegal actions.
*
* @param running Are the servers currently running?
*/
public void updateRunning(boolean running) {
boolean stopped = !running; //Most gui is only enabled when the server is stopped rather than running.
profiles.setEnabled(stopped);
addProfile.setEnabled(stopped);
delProfile.setEnabled(stopped);
btnStartServer.setEnabled(stopped);
addServer.setEnabled(stopped);
tabbedPane.setEnabledAt(2, stopped);
btnStopServer.setEnabled(running);
}
/**
* Saves the previous profile and loads data from the new profile.
*/
private void changeProfile() throws FileNotFoundException {
Profile.getCurrent().save();
Object current = this.profiles.getSelectedItem();
if (current != null) {
Profile.setCurrent(current.toString());
}
this.update();
Profile.getCurrent().updateConsoles();
}
/**
* Stops all servers
*/
private void stop() {
try {
setStatus("Servers are stopping...");
Server.stop();
} catch (IOException e1) {
Profile.showError("Could not stop server.");
e1.printStackTrace();
}
}
/**
* Asks the user for a delay if checked, and sets the value to the current profile.
*/
private void delay() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
if (chckbxmntmDelayStartup.isSelected()) {
Objects.requireNonNull(profile).setDelayStartup(
Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "))
);
} else {
Objects.requireNonNull(profile).setDelayStartup(0);
}
} else {
Profile.showError("No profile selected");
}
}
/**
* Saves the runInBackground setting to the current profile.
*/
private void background() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else {
Profile.showError("No profile selected");
}
}
/**
* Saves the downloadJars setting to the current profile.
*/
private void downloadJars() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected());
} else {
Profile.showError("No profile selected");
}
}
/**
* Copies all server directories to a folder specified by the user.
*/
private void backup() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Backup folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile();
for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().getPath().equals("") && collection.getServer().isEnabled()) {
String name = collection.getServer().getName();
File srcFolder = new File(collection.getServer().getPath());
File destFolder = new File(path, name);
if (!destFolder.exists()) {
if (destFolder.mkdirs()) {
try {
Shared.copyFolder(this, srcFolder, destFolder);
} catch (IOException e) {
e.printStackTrace();
return;
}
} else {
return;
}
}
}
}
}
this.setStatus("Backup finished");
}
/**
* 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);
}
}
}
}
/**
* Loads popup messages from a text file.
*/
private void loadMessages() throws FileNotFoundException {
Scanner file = getResourceAsScanner("menumsg.csv");
while (file.hasNextLine()) {
String nextLine = file.nextLine();
String[] line = nextLine.split("=");
String content = line[1].replaceAll("_BREAK_", System.getProperty("line.separator"));
switch (line[0]) {
case "setup":
setupText = content;
break;
case "runinbk":
runInBackgroundText = content;
break;
case "delaystartup":
delayStartupText = content;
break;
case "downloadjars":
downloadJarsText = content;
break;
case "about":
aboutText = content;
}
}
}
}

View File

@ -0,0 +1,61 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import javax.swing.*;
public abstract class MessageHandler implements GUI {
private final boolean silent;
/***
* Initializes a new message handler
*
* @param silent <p>Whether to print to cli instead of showing a GUI element</p>
*/
public MessageHandler(boolean silent) {
this.silent = silent;
}
@Override
public void showError(String title, String message) {
if (silent) {
System.out.println(message);
} else {
showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void showError(String message) {
showError("Error", message);
}
@Override
public void showMessage(String title, String message) {
if (silent) {
System.out.println(message);
} else {
showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void showMessage(String message) {
showMessage("Info", message);
}
/**
* Shows a JOptionPane
*
* @param title <p>The title of the pane</p>
* @param message <p>The message of the pane</p>
* @param paneType <p>The type of the pane</p>
*/
private void showJOptionPane(String title, String message, int paneType) {
JOptionPane.showMessageDialog(
null,
message,
title,
paneType
);
}
}

View File

@ -0,0 +1,811 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.Server;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import javax.imageio.ImageIO;
import javax.naming.ConfigurationException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
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;
/**
* Generates a ServerLauncherGUI.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public class ServerLauncherGUI extends MessageHandler implements ActionListener, GUI {
private JFrame frame;
private JTabbedPane tabbedPane;
private JTabbedPane serversPane;
//Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
private JMenuItem mntmErrors, mntmSetup, mntmManualUpdate; //Help
private JMenuItem mntmRunInBackground, mntmDelayStartup, mntmDownloadJars; //Info/options
private JMenuItem mntmAbout, mntmStory; //Info/about
//Basic controls
private JButton btnStartServer, btnStopServer, addServer, backup, addProfile, delProfile;
private JComboBox<String> profiles;
private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
//Server controls
private JComboBox<String> targetServer;
private JComboBox<String> targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;
//Text
private String setupText;
private String runInBackgroundText;
private String delayStartupText;
private String downloadJarsText;
private String aboutText;
private final ArrayList<String> globalPlayers;
private SystemTray tray;
private TrayIcon trayIcon;
/**
* Creates the application window
*/
public ServerLauncherGUI() throws IOException {
super(false);
initialize(440, 170);
loadMessages();
this.globalPlayers = new ArrayList<>();
}
/**
* Creates the application window with a preferred width and height
* @param width <p>The preferred width</p>
* @param height <p>The preferred height</p>
*/
public ServerLauncherGUI(int width, int height) throws IOException {
super(false);
initialize(width, height);
loadMessages();
this.globalPlayers = new ArrayList<>();
}
/**
* Gets the pane used for server configurations
*
* @return A JTabbedPane
*/
public JTabbedPane getPane() {
return this.serversPane;
}
/**
* Sets the text of the status label.
*
* @param text The new text
*/
public void setStatus(String text) {
this.lblStatuslabel.setText(text);
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", true))) {
file.println(text);
} catch (IOException e ) {
e.printStackTrace();
}
}
/**
* Adds a player to the global playerlist, and updates the players combo.
*
* @param name The name of the player to add
*/
public void addPlayer(String name) {
this.globalPlayers.add(name);
this.updatePlayers();
}
/**
* Removes a player from the global list of players.
*
* @param name The name of the player to remove.
*/
public void removePlayer(String name) {
globalPlayers.removeIf(playerName -> playerName.equals(name));
this.updatePlayers();
}
/**
* Updates the profiles combo.
*/
public void updateProfiles() {
this.profiles.removeAllItems();
for (Profile profile : Profile.getProfiles()) {
this.profiles.addItem(profile.getName());
}
}
/**
* Gets the size of the main JFrame
*
* @return The Dimension of the frame
*/
public Dimension getSize() {
return frame.getContentPane().getSize();
}
/**
* Updates ServerLauncherGUI according to current profile 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().getDownloadAllAvailableJARFiles());
this.targetServer.removeAllItems();
this.targetServer.addItem("All");
for (Collection collection : Profile.getCurrent().getCollections()) {
this.targetServer.addItem(collection.getName());
}
}
/**
* Creates the ServerLauncherGUI,
*/
private void initialize(int width, int height) throws IOException {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException |
UnsupportedLookAndFeelException |
InstantiationException |
IllegalAccessException e
) {
e.printStackTrace();
}
frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
ImageIcon img = new ImageIcon(ImageIO.read(getResourceAsStream("GUIIcon.png")));
frame.setIconImage(img.getImage());
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
chckbxmntmRunInBackground = new JCheckBoxMenuItem("Run in background on exit");
mnOptions.add(chckbxmntmRunInBackground);
chckbxmntmRunInBackground.addActionListener(this);
chckbxmntmDelayStartup = new JCheckBoxMenuItem("Delay Startup");
mnOptions.add(chckbxmntmDelayStartup);
chckbxmntmDelayStartup.addActionListener(this);
chckbxmntmDownloadJars = new JCheckBoxMenuItem("Download jars");
mnOptions.add(chckbxmntmDownloadJars);
chckbxmntmDownloadJars.addActionListener(this);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
mntmErrors = new JMenuItem("Errors");
mnHelp.add(mntmErrors);
mntmErrors.addActionListener(this);
mntmSetup = new JMenuItem("Setup");
mnHelp.add(mntmSetup);
mntmSetup.addActionListener(this);
mntmManualUpdate = new JMenuItem("Manual update");
mnHelp.add(mntmManualUpdate);
mntmManualUpdate.addActionListener(this);
JMenu mnInfo = new JMenu("Info");
menuBar.add(mnInfo);
JMenu mnOptionsInfo = new JMenu("Options");
mnInfo.add(mnOptionsInfo);
mntmRunInBackground = new JMenuItem("Run in background on exit");
mnOptionsInfo.add(mntmRunInBackground);
mntmRunInBackground.addActionListener(this);
mntmDelayStartup = new JMenuItem("Delay Startup");
mnOptionsInfo.add(mntmDelayStartup);
mntmDelayStartup.addActionListener(this);
mntmDownloadJars = new JMenuItem("Download jars");
mnOptionsInfo.add(mntmDownloadJars);
mntmDownloadJars.addActionListener(this);
JMenu mnAbout = new JMenu("About");
mnInfo.add(mnAbout);
mntmAbout = new JMenuItem("About");
mnAbout.add(mntmAbout);
mntmAbout.addActionListener(this);
mntmStory = new JMenuItem("Story");
mnAbout.add(mntmStory);
mntmStory.addActionListener(this);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane);
JPanel panelBasic = new JPanel();
tabbedPane.addTab("Control panel", null, panelBasic, null);
SpringLayout sl_panel = new SpringLayout();
panelBasic.setLayout(sl_panel);
JLabel lblBasicControls = new JLabel("Basic controls");
sl_panel.putConstraint(SpringLayout.NORTH, lblBasicControls, 10, SpringLayout.NORTH, panelBasic);
panelBasic.add(lblBasicControls);
btnStartServer = new JButton("Start servers");
sl_panel.putConstraint(SpringLayout.WEST, lblBasicControls, 0, SpringLayout.WEST, btnStartServer);
sl_panel.putConstraint(SpringLayout.NORTH, btnStartServer, 6, SpringLayout.SOUTH, lblBasicControls);
sl_panel.putConstraint(SpringLayout.WEST, btnStartServer, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(btnStartServer);
btnStartServer.addActionListener(this);
btnStopServer = new JButton("Stop servers");
sl_panel.putConstraint(SpringLayout.NORTH, btnStopServer, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, btnStopServer, 6, SpringLayout.EAST, btnStartServer);
panelBasic.add(btnStopServer);
btnStopServer.addActionListener(this);
JLabel lblProfile = new JLabel("Profile");
sl_panel.putConstraint(SpringLayout.NORTH, lblProfile, 6, SpringLayout.SOUTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, lblProfile, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(lblProfile);
addProfile = new JButton("+");
sl_panel.putConstraint(SpringLayout.NORTH, addProfile, 6, SpringLayout.SOUTH, lblProfile);
sl_panel.putConstraint(SpringLayout.WEST, addProfile, 10, SpringLayout.WEST, panelBasic);
panelBasic.add(addProfile);
addProfile.addActionListener(this);
delProfile = new JButton("-");
sl_panel.putConstraint(SpringLayout.NORTH, delProfile, 0, SpringLayout.NORTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, delProfile, 6, SpringLayout.EAST, addProfile);
panelBasic.add(delProfile);
delProfile.addActionListener(this);
profiles = new JComboBox<>();
sl_panel.putConstraint(SpringLayout.NORTH, profiles, 0, SpringLayout.NORTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, profiles, 6, SpringLayout.EAST, delProfile);
sl_panel.putConstraint(SpringLayout.EAST, profiles, 124, SpringLayout.EAST, delProfile);
panelBasic.add(profiles);
profiles.addActionListener(this);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 6, SpringLayout.SOUTH, addProfile);
sl_panel.putConstraint(SpringLayout.SOUTH, lblStatuslabel, -10, SpringLayout.SOUTH, panelBasic);
sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, -10, SpringLayout.EAST, panelBasic);
panelBasic.add(lblStatuslabel);
addServer = new JButton("Add server");
sl_panel.putConstraint(SpringLayout.NORTH, addServer, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, addServer, 6, SpringLayout.EAST, btnStopServer);
panelBasic.add(addServer);
addServer.addActionListener(this);
backup = new JButton("Backup");
sl_panel.putConstraint(SpringLayout.NORTH, backup, 0, SpringLayout.NORTH, btnStartServer);
sl_panel.putConstraint(SpringLayout.WEST, backup, 6, SpringLayout.EAST, addServer);
panelBasic.add(backup);
backup.addActionListener(this);
JPanel controlServers = new JPanel();
tabbedPane.addTab("Control servers", null, controlServers, null);
SpringLayout sl_panel_1 = new SpringLayout();
controlServers.setLayout(sl_panel_1);
targetServer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetServer, 10, SpringLayout.NORTH, controlServers);
controlServers.add(targetServer);
targetServer.addActionListener(this);
targetPlayer = new JComboBox<>();
sl_panel_1.putConstraint(SpringLayout.NORTH, targetPlayer, 6, SpringLayout.SOUTH, targetServer);
targetPlayer.setEditable(true);
controlServers.add(targetPlayer);
btnKick = new JButton("Kick");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnKick, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.WEST, btnKick, 6, SpringLayout.EAST, targetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnKick, 104, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.SOUTH, targetServer, 0, SpringLayout.SOUTH, btnKick);
controlServers.add(btnKick);
btnKick.addActionListener(this);
btnBan = new JButton("Ban");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnBan, 6, SpringLayout.SOUTH, btnKick);
sl_panel_1.putConstraint(SpringLayout.WEST, btnBan, 6, SpringLayout.EAST, targetPlayer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnBan, 104, SpringLayout.WEST, btnBan);
sl_panel_1.putConstraint(SpringLayout.SOUTH, targetPlayer, 0, SpringLayout.SOUTH, btnBan);
controlServers.add(btnBan);
btnBan.addActionListener(this);
btnOp = new JButton("OP");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnOp, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.WEST, btnOp, 6, SpringLayout.EAST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnOp, -10, SpringLayout.EAST, controlServers);
controlServers.add(btnOp);
btnOp.addActionListener(this);
btnDeop = new JButton("DEOP");
sl_panel_1.putConstraint(SpringLayout.WEST, btnDeop, 6, SpringLayout.EAST, btnBan);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnDeop, 5, SpringLayout.SOUTH, btnOp);
sl_panel_1.putConstraint(SpringLayout.EAST, btnDeop, -10, SpringLayout.EAST, controlServers);
controlServers.add(btnDeop);
btnDeop.addActionListener(this);
JLabel lblTargetServer = new JLabel("Target server");
sl_panel_1.putConstraint(SpringLayout.WEST, targetServer, 6, SpringLayout.EAST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, targetServer, 121, SpringLayout.EAST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.NORTH, lblTargetServer, 10, SpringLayout.NORTH, controlServers);
sl_panel_1.putConstraint(SpringLayout.SOUTH, lblTargetServer, 0, SpringLayout.SOUTH, targetServer);
sl_panel_1.putConstraint(SpringLayout.WEST, lblTargetServer, 10, SpringLayout.WEST, controlServers);
controlServers.add(lblTargetServer);
JLabel lblTargetPlayer = new JLabel("Target player");
sl_panel_1.putConstraint(SpringLayout.WEST, targetPlayer, 7, SpringLayout.EAST, lblTargetPlayer);
sl_panel_1.putConstraint(SpringLayout.EAST, targetPlayer, 122, SpringLayout.EAST, lblTargetPlayer);
sl_panel_1.putConstraint(SpringLayout.NORTH, lblTargetPlayer, 6, SpringLayout.SOUTH, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.SOUTH, lblTargetPlayer, 0, SpringLayout.SOUTH, targetPlayer);
sl_panel_1.putConstraint(SpringLayout.WEST, lblTargetPlayer, 0, SpringLayout.WEST, lblTargetServer);
controlServers.add(lblTargetPlayer);
btnCustomCommand = new JButton("Custom command");
sl_panel_1.putConstraint(SpringLayout.WEST, btnCustomCommand, 250, SpringLayout.WEST, controlServers);
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);
sl_panel_1.putConstraint(SpringLayout.EAST, customCommand, -6, SpringLayout.WEST, btnCustomCommand);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnCustomCommand, 0, SpringLayout.NORTH, customCommand);
sl_panel_1.putConstraint(SpringLayout.SOUTH, customCommand, 0, SpringLayout.SOUTH, btnCustomCommand);
controlServers.add(customCommand);
customCommand.setColumns(10);
btnSaveserver = new JButton("Save server");
sl_panel_1.putConstraint(SpringLayout.NORTH, customCommand, 6, SpringLayout.SOUTH, btnSaveserver);
sl_panel_1.putConstraint(SpringLayout.NORTH, btnSaveserver, 6, SpringLayout.SOUTH, btnBan);
sl_panel_1.putConstraint(SpringLayout.WEST, btnSaveserver, 0, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick);
sl_panel_1.putConstraint(SpringLayout.EAST, btnSaveserver, 104, SpringLayout.WEST, btnKick);
controlServers.add(btnSaveserver);
btnSaveserver.addActionListener(this);
btnReload = new JButton("Reload");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnReload, 6, SpringLayout.SOUTH, btnDeop);
sl_panel_1.putConstraint(SpringLayout.WEST, btnReload, 0, SpringLayout.WEST, btnDeop);
sl_panel_1.putConstraint(SpringLayout.EAST, btnReload, 0, SpringLayout.EAST, btnOp);
controlServers.add(btnReload);
btnReload.addActionListener(this);
btnServerConsoles = new JButton("View server consoles");
sl_panel_1.putConstraint(SpringLayout.NORTH, btnServerConsoles, 0, SpringLayout.NORTH, btnSaveserver);
sl_panel_1.putConstraint(SpringLayout.WEST, btnServerConsoles, 0, SpringLayout.WEST, lblTargetServer);
sl_panel_1.putConstraint(SpringLayout.EAST, btnServerConsoles, 0, SpringLayout.EAST, targetServer);
controlServers.add(btnServerConsoles);
btnServerConsoles.addActionListener(this);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("Servers", null, panel_2, null);
SpringLayout sl_panel_2 = new SpringLayout();
panel_2.setLayout(sl_panel_2);
JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
sl_panel_2.putConstraint(SpringLayout.NORTH, tabbedPane_1, 0, SpringLayout.NORTH, panel_2);
sl_panel_2.putConstraint(SpringLayout.WEST, tabbedPane_1, 0, SpringLayout.WEST, panel_2);
sl_panel_2.putConstraint(SpringLayout.SOUTH, tabbedPane_1, 0, SpringLayout.SOUTH, panel_2);
sl_panel_2.putConstraint(SpringLayout.EAST, tabbedPane_1, 0, SpringLayout.EAST, panel_2);
panel_2.add(tabbedPane_1);
this.serversPane = tabbedPane_1;
tabbedPane_1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
frame.validate();
frame.pack();
frame.setVisible(true);
tray();
updateRunning(false);
}
/**
* Prepares the system tray if available.
*/
private void tray() {
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
Image trayImage = Toolkit.getDefaultToolkit().getImage("files/GUIIcon.png");
PopupMenu popup = new PopupMenu();
trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup);
trayIcon.setImageAutoSize(true);
ActionListener exitListener= e -> {
stop();
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.exit(0);
};
MenuItem restoreItem = new MenuItem("Restore");
popup.add(restoreItem);
restoreItem.addActionListener(e -> {
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
});
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
popup.add(exitItem);
frame.addWindowStateListener(e -> {
if (e.getNewState() == NORMAL) {
tray.remove(trayIcon);
frame.setVisible(true);
}
});
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (Profile.getCurrent().getRunInBackground() && SystemTray.isSupported()) {
try {
tray.add(trayIcon);
frame.setVisible(false);
} catch (AWTException e1) {
e1.printStackTrace();
}
} else {
stop();
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
});
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getClickCount() >= 1 && e.getButton() == MouseEvent.BUTTON1){
frame.setExtendedState(NORMAL);
tray.remove(trayIcon);
frame.setVisible(true);
}
}
});
} else {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
stop();
System.exit(0);
}
});
}
}
/**
* Hides the gui to the tray,
*/
public void hide() {
frame.setVisible(false);
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String selectedServerValue = null, selectedPlayerValue = null;
Object selectedServer = targetServer.getSelectedItem();
if (selectedServer != null) {
selectedServerValue = selectedServer.toString();
}
Object selectedPlayer = targetPlayer.getSelectedItem();
if (selectedPlayer != null) {
selectedPlayerValue = selectedPlayer.toString();
}
if (e.getSource() == chckbxmntmRunInBackground) {
background();
} else if (e.getSource() == chckbxmntmDelayStartup) {
delay();
} else if (e.getSource() == chckbxmntmDownloadJars) {
downloadJars();
} else if (e.getSource() == mntmErrors) {
CommonFunctions.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherInfo/");
} else if (e.getSource() == mntmSetup) {
showMessage("Setup", setupText);
} else if (e.getSource() == mntmManualUpdate) {
CommonFunctions.goToURL("https://git.knarcraft.net/KnarCraft/Minecraft-Server-Launcher/releases");
} else if (e.getSource() == mntmRunInBackground) {
showMessage("Run in background", runInBackgroundText);
} else if (e.getSource() == mntmDelayStartup) {
showMessage("Delay startup", delayStartupText);
} else if (e.getSource() == mntmDownloadJars) {
showMessage("Download jars", downloadJarsText);
} else if (e.getSource() == mntmAbout) {
showMessage("About", aboutText);
} else if (e.getSource() == mntmStory) {
CommonFunctions.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherStory/");
} else if (e.getSource() == btnStartServer) {
try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Executors.newSingleThreadExecutor().execute(Server::startServers);
} else if (e.getSource() == btnStopServer) {
stop();
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
try {
Profile.getCurrent().addCollection(serverName);
} catch (ConfigurationException e1) {
e1.printStackTrace();
}
this.update();
Profile.getCurrent().updateConsoles();
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
updateProfiles();
} else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile.removeProfile(selected.toString());
updateProfiles();
}
} else if (e.getSource() == profiles) {
try {
changeProfile();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
}
} else if (e.getSource() == btnBan) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
}
} else if (e.getSource() == btnOp) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
}
} else if (e.getSource() == btnDeop) {
if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
}
} else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, customCommand.getText());
customCommand.setText("");
}
} else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "save-all");
}
} else if (e.getSource() == btnReload) {
if (selectedServerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
ServerConsoles.setAsVisible();
} else if (e.getSource() == targetServer) {
updatePlayers();
}
}
/**
* Updates the ServerLauncherGUI components to block a user from doing illegal actions.
*
* @param running Are the servers currently running?
*/
public void updateRunning(boolean running) {
boolean stopped = !running; //Most gui is only enabled when the server is stopped rather than running.
profiles.setEnabled(stopped);
addProfile.setEnabled(stopped);
delProfile.setEnabled(stopped);
btnStartServer.setEnabled(stopped);
addServer.setEnabled(stopped);
tabbedPane.setEnabledAt(2, stopped);
btnStopServer.setEnabled(running);
}
/**
* Saves the previous profile and loads data from the new profile.
*/
private void changeProfile() throws FileNotFoundException {
Profile.getCurrent().save();
Object current = this.profiles.getSelectedItem();
if (current != null) {
Profile.setCurrent(current.toString());
}
this.update();
Profile.getCurrent().updateConsoles();
}
/**
* Stops all servers
*/
private void stop() {
try {
setStatus("Servers are stopping...");
Server.stop();
} catch (IOException e1) {
showError("Could not stop server.");
e1.printStackTrace();
}
}
/**
* Asks the user for a delay if checked, and sets the value to the current profile.
*/
private void delay() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
if (chckbxmntmDelayStartup.isSelected()) {
Objects.requireNonNull(profile).setDelayStartup(
Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "))
);
} else {
Objects.requireNonNull(profile).setDelayStartup(0);
}
} else {
showError("No profile selected");
}
}
/**
* Saves the runInBackground setting to the current profile.
*/
private void background() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else {
showError("No profile selected");
}
}
/**
* Saves the downloadJars setting to the current profile.
*/
private void downloadJars() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected());
} else {
showError("No profile selected");
}
}
/**
* Copies all server directories to a folder specified by the user.
*/
private void backup() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Backup folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile();
for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().getPath().equals("") && collection.getServer().isEnabled()) {
String name = collection.getServer().getName();
File srcFolder = new File(collection.getServer().getPath());
File destFolder = new File(path, name);
if (!destFolder.exists()) {
if (destFolder.mkdirs()) {
try {
CommonFunctions.copyFolder(this, srcFolder, destFolder);
} catch (IOException e) {
e.printStackTrace();
return;
}
} else {
return;
}
}
}
}
}
this.setStatus("Backup finished");
}
/**
* 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);
}
}
}
}
/**
* Loads popup messages from a text file.
*/
private void loadMessages() throws FileNotFoundException {
Scanner file = getResourceAsScanner("menumsg.csv");
while (file.hasNextLine()) {
String nextLine = file.nextLine();
String[] line = nextLine.split("=");
String content = line[1].replaceAll("_BREAK_", System.getProperty("line.separator"));
switch (line[0]) {
case "setup":
setupText = content;
break;
case "runinbk":
runInBackgroundText = content;
break;
case "delaystartup":
delayStartupText = content;
break;
case "downloadjars":
downloadJarsText = content;
break;
case "about":
aboutText = content;
}
}
}
@Override
public void printMessageToGUI(String message) {
setStatus(message);
}
}

View File

@ -2,8 +2,10 @@ package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.server.Server;
import net.knarcraft.minecraftserverlauncher.server.ServerType;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
import javax.naming.ConfigurationException;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -33,7 +35,7 @@ public class ServerTab implements ActionListener {
* @param serverVersion The version of the server
* @param maxRam The max usable ram for the server
*/
public void setData(String path, boolean enabled, String typeName, String serverVersion, String maxRam) {
public void setData(String path, boolean enabled, String typeName, String serverVersion, String maxRam) throws ConfigurationException {
this.directory.setText(path);
this.chckbxEnabled.setSelected(enabled);
this.serverTypes.setSelectedItem(typeName);
@ -42,7 +44,7 @@ public class ServerTab implements ActionListener {
this.maxRam.setSelectedItem(maxRam);
}
public ServerTab(String name) {
public ServerTab(String name) throws ConfigurationException {
this.name = name;
panel = new JPanel();
Profile.getGUI().getPane().addTab(name, null, panel, null);
@ -59,7 +61,7 @@ public class ServerTab implements ActionListener {
sl_panel_3.putConstraint(SpringLayout.SOUTH, lblServerVersion, 26, SpringLayout.SOUTH, lblServerType);
panel.add(lblServerVersion);
serverTypes = new JComboBox<>(ServerType.getTypeNames());
serverTypes = new JComboBox<>(ServerTypeHandler.getTypeNames());
sl_panel_3.putConstraint(SpringLayout.NORTH, serverTypes, 10, SpringLayout.NORTH, panel);
sl_panel_3.putConstraint(SpringLayout.WEST, serverTypes, 5, SpringLayout.EAST, lblServerVersion);
sl_panel_3.putConstraint(SpringLayout.EAST, serverTypes, 154, SpringLayout.WEST, serverTypes);
@ -68,7 +70,7 @@ public class ServerTab implements ActionListener {
panel.add(serverTypes);
serverTypes.addActionListener(this);
serverVersions = new JComboBox<>(ServerType.getServerTypes().get(0).getVersions());
serverVersions = new JComboBox<>(ServerTypeHandler.getServerTypes().get(0).getVersions());
sl_panel_3.putConstraint(SpringLayout.NORTH, serverVersions, 6, SpringLayout.SOUTH, serverTypes);
sl_panel_3.putConstraint(SpringLayout.EAST, serverVersions, 0, SpringLayout.EAST, serverTypes);
sl_panel_3.putConstraint(SpringLayout.WEST, serverVersions, 6, SpringLayout.EAST, lblServerVersion);
@ -183,7 +185,11 @@ public class ServerTab implements ActionListener {
} else if (e.getSource() == btnBrowse) {
browse();
} else if (e.getSource() == serverTypes) {
serverTypes();
try {
serverTypes();
} catch (ConfigurationException e1) {
e1.printStackTrace();
}
}
}
@ -214,7 +220,7 @@ public class ServerTab implements ActionListener {
/**
* Updates the versions combo according to the value of the server type combo.
*/
private void serverTypes() {
private void serverTypes() throws ConfigurationException {
serverVersions.removeAllItems();
String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem();
@ -227,7 +233,7 @@ public class ServerTab implements ActionListener {
} else {
serverVersions.setEditable(false);
ServerType current = null;
for (ServerType servertype : ServerType.getServerTypes()) {
for (ServerType servertype : ServerTypeHandler.getServerTypes()) {
if (servertype.getName().equals(selectedserverTypes)) {
current = servertype;
}