More gui added, and profiles half implemented

This commit is contained in:
Kristian Knarvik 2018-01-28 19:06:50 +01:00
parent bcaf8640d4
commit b0979c6e0f
5 changed files with 249 additions and 39 deletions

View File

@ -1,3 +1,4 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.server.*;
import net.knarcraft.serverlauncher.userinterface.GUI; import net.knarcraft.serverlauncher.userinterface.GUI;
@ -18,8 +19,8 @@ class Main {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {
setup(); setup();
GUI window = new GUI(); new GUI();
window.getFrame().setVisible(true); Profile.addProfile("Default");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -0,0 +1,80 @@
package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.userinterface.GUI;
import java.util.ArrayList;
public class Profile {
private static final GUI gui = Server.getGUI();
private static final ArrayList<Profile> profiles = new ArrayList<>();
private final ArrayList<Server> servers;
private final String name;
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
private Profile(String name) {
this.servers = new ArrayList<>();
this.name = name;
this.runInBackground = false;
this.delayStartup = 0;
this.downloadJars = false;
profiles.add(this);
}
public void addServer(Server server) {
this.servers.add(server);
}
public ArrayList<Server> getServers() {
return this.servers;
}
public static void addProfile(String name) {
if (name.equals("")) {
return;
}
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
return;
}
}
new Profile(name);
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);
gui.removeProfile(i);
}
}
}
}
public void setRunInBackground(boolean value) {
this.runInBackground = value;
}
public void setDelayStartup(int value) {
if (value >= 0) {
this.delayStartup = value;
}
}
public void setDownloadJars(boolean value) {
this.downloadJars = value;
}
}

View File

@ -19,7 +19,6 @@ import java.util.ArrayList;
public class Server { public class Server {
/** Available ram sizes. For GUI dropdown */ /** Available ram sizes. For GUI dropdown */
private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"}; private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private static final ArrayList<Server> servers = new ArrayList<>();
private static GUI gui; private static GUI gui;
private final String name; private final String name;
@ -41,7 +40,7 @@ public class Server {
this.serverVersion = null; this.serverVersion = null;
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.process = null; this.process = null;
servers.add(this); gui.currentProfile().addServer(this);
window.addServer(name, this); window.addServer(name, this);
} }
@ -62,6 +61,10 @@ public class Server {
gui = _gui; gui = _gui;
} }
public static GUI getGUI() {
return gui;
}
/** /**
* @return A representation of the name of a jarfile. * @return A representation of the name of a jarfile.
*/ */
@ -69,8 +72,12 @@ public class Server {
return this.type.getName() + this.serverVersion + ".jar"; return this.type.getName() + this.serverVersion + ".jar";
} }
public static ArrayList<Server> getServers() { public String getName() {
return servers; return this.name;
}
public String getPath() {
return this.path;
} }
public Process getProcess() { public Process getProcess() {
@ -93,8 +100,12 @@ public class Server {
return ramList; return ramList;
} }
public boolean isEnabled() {
return this.enabled;
}
public static void stop() throws IOException { public static void stop() throws IOException {
for (Server server : servers) { for (Server server : gui.currentProfile().getServers()) {
if (server.writer != null) { if (server.writer != null) {
if (server.type.getName().equals("Bungee")) { if (server.type.getName().equals("Bungee")) {
server.writer.write("end\n"); server.writer.write("end\n");
@ -132,7 +143,7 @@ public class Server {
*/ */
public static void startServers() { public static void startServers() {
System.out.println("Starting servers."); System.out.println("Starting servers.");
for (Server server : servers) { for (Server server : gui.currentProfile().getServers()) {
if (!server.run()) { if (!server.run()) {
gui.setStatus("An error occurred. Start aborted"); gui.setStatus("An error occurred. Start aborted");
} }

View File

@ -2,24 +2,20 @@ package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
public class GUI implements ActionListener { public class GUI implements ActionListener {
private JFrame frame; private JFrame frame;
//Settings
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
//Menu //Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
@ -28,7 +24,7 @@ public class GUI implements ActionListener {
private JMenuItem mntmAbout, mntmStory; //Info/about private JMenuItem mntmAbout, mntmStory; //Info/about
//Basic controls //Basic controls
private JButton btnStartServer, btnStopServer, addServer, backup, addProfile, delProfile; private JButton btnStartServer, btnStopServer, addServer, backup, addProfile, delProfile;
private JComboBox profiles; private JComboBox<String> profiles;
private final JLabel lblStatuslabel = new JLabel("Servers are stopped"); private final JLabel lblStatuslabel = new JLabel("Servers are stopped");
//Server controls //Server controls
private JComboBox targetServer, targetPlayer; private JComboBox targetServer, targetPlayer;
@ -61,6 +57,26 @@ public class GUI implements ActionListener {
this.lblStatuslabel.setText(text); this.lblStatuslabel.setText(text);
} }
public JTabbedPane getPane() {
return this.serversPane;
}
public void addProfile(String name) {
this.profiles.addItem(name);
}
public void removeProfile(int index) {
this.profiles.removeItemAt(index);
}
public Profile currentProfile() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
return Profile.getProfile(selected.toString());
}
return null;
}
/** /**
* Initialize the contents of the frame. * Initialize the contents of the frame.
*/ */
@ -182,14 +198,14 @@ public class GUI implements ActionListener {
panelBasic.add(delProfile); panelBasic.add(delProfile);
delProfile.addActionListener(this); delProfile.addActionListener(this);
profiles = new JComboBox(); profiles = new JComboBox<>();
sl_panel.putConstraint(SpringLayout.NORTH, profiles, 1, SpringLayout.NORTH, addProfile); sl_panel.putConstraint(SpringLayout.NORTH, profiles, 1, SpringLayout.NORTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, profiles, 6, SpringLayout.EAST, delProfile); sl_panel.putConstraint(SpringLayout.WEST, profiles, 6, SpringLayout.EAST, delProfile);
sl_panel.putConstraint(SpringLayout.EAST, profiles, 124, SpringLayout.EAST, delProfile); sl_panel.putConstraint(SpringLayout.EAST, profiles, 124, SpringLayout.EAST, delProfile);
panelBasic.add(profiles); panelBasic.add(profiles);
profiles.addActionListener(this); profiles.addActionListener(this);
sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 15, SpringLayout.SOUTH, addProfile); sl_panel.putConstraint(SpringLayout.NORTH, lblStatuslabel, 8, SpringLayout.SOUTH, addProfile);
sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic); sl_panel.putConstraint(SpringLayout.WEST, lblStatuslabel, 10, SpringLayout.WEST, panelBasic);
sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, 386, SpringLayout.WEST, panelBasic); sl_panel.putConstraint(SpringLayout.EAST, lblStatuslabel, 386, SpringLayout.WEST, panelBasic);
panelBasic.add(lblStatuslabel); panelBasic.add(lblStatuslabel);
@ -314,6 +330,8 @@ public class GUI implements ActionListener {
panel_2.add(tabbedPane_1); panel_2.add(tabbedPane_1);
this.serversPane = tabbedPane_1; this.serversPane = tabbedPane_1;
frame.setVisible(true);
} }
public void addServer(String name, Server server) { public void addServer(String name, Server server) {
@ -395,15 +413,11 @@ public class GUI implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == chckbxmntmRunInBackground) { if (e.getSource() == chckbxmntmRunInBackground) {
this.runInBackground = chckbxmntmRunInBackground.isSelected(); background();
} else if (e.getSource() == chckbxmntmDelayStartup) { } else if (e.getSource() == chckbxmntmDelayStartup) {
if (chckbxmntmDelayStartup.isSelected()) { delay();
this.delayStartup = Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "));
} else {
this.delayStartup = 0;
}
} else if (e.getSource() == chckbxmntmDownloadJars) { } else if (e.getSource() == chckbxmntmDownloadJars) {
this.downloadJars = chckbxmntmDownloadJars.isSelected(); downloadJars();
} 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) {
@ -423,6 +437,28 @@ public class GUI implements ActionListener {
} else if (e.getSource() == btnStartServer) { } else if (e.getSource() == btnStartServer) {
Server.startServers(); Server.startServers();
} else if (e.getSource() == btnStopServer) { } else if (e.getSource() == btnStopServer) {
stop();
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
new Server(serverName, this);
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
} else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile.deleteProfile(selected.toString());
}
}
/*
//Server controls
private JComboBox targetServer, targetPlayer;
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles;
private JTextField customCommand;*/
}
private void stop() {
try { try {
setStatus("Servers are stopping"); setStatus("Servers are stopping");
Server.stop(); Server.stop();
@ -430,21 +466,74 @@ public class GUI implements ActionListener {
} 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);
} }
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
new Server(serverName, this);
} else if (e.getSource() == backup) {
} }
/*
//Basic controls /**
backup, addProfile, delProfile; * Asks the user for a delay if checked, and sets the value to the current profile.
private JComboBox profiles; */
private JLabel lblStatuslabel; private void delay() {
//Server controls Object selected = profiles.getSelectedItem();
private JComboBox targetServer, targetPlayer; if (selected != null) {
private JButton btnKick, btnBan, btnOp, btnDeop, btnCustomCommand, btnSaveserver, btnReload, btnServerConsoles; Profile profile = Profile.getProfile(selected.toString());
private JTextField customCommand;*/ if (chckbxmntmDelayStartup.isSelected()) {
Objects.requireNonNull(profile).setDelayStartup(Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: ")));
} else {
Objects.requireNonNull(profile).setDelayStartup(0);
}
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void background() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void downloadJars() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected());
} else {
JOptionPane.showMessageDialog(null, "No profile selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
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 (Server server : currentProfile().getServers()) {
if (!server.getPath().equals("") && server.isEnabled()) {
String name = server.getName();
File srcFolder = new File(server.getPath());
File destFolder = new File(path, name);
if (!destFolder.exists()) {
if (destFolder.mkdirs()) {
try {
copyFolder(srcFolder, destFolder);
} catch (IOException e) {
e.printStackTrace();
return;
}
} else {
return;
}
}
}
}
}
} }
private static void goToURL(String url) { private static void goToURL(String url) {
@ -481,6 +570,36 @@ public class GUI implements ActionListener {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Messages file could not be read", "Setup", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "Messages file could not be read", "Setup", JOptionPane.ERROR_MESSAGE);
} }
}
private void copyFolder(File src, File dest) throws IOException{
if (!src.isDirectory()) {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
this.setStatus("File copied from " + src + " to " + dest);
} else {
if(!dest.exists()){
if (dest.mkdir()) {
this.setStatus("Directory copied from " + src + " to " + dest);
} else {
return;
}
}
String files[] = src.list();
if (files != null) {
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile, destFile);
}
}
}
} }
} }

View File

@ -22,8 +22,7 @@ class ServerTest {
window.getFrame().setVisible(true); window.getFrame().setVisible(true);
new Server("Server1", window); Server server1 = new Server("Server1", window);
Server server1 = Server.getServers().get(0);
server1.toggle(); server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType(ServerType.getServerTypes().get(4)); server1.setType(ServerType.getServerTypes().get(4));