Changes a lot of things to make everything cleaner. Closes #3
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Drops the idea of using serializable Adds a new controller object which takes care of profiles and saving Moves profile independent settings to its own file Makes saving and loading from file a lot cleaner Fixes the bug preventing lastly used profile from loading Makes the profile object only do profile things Moves gui initialization to the controller object Updates vanilla version from 1.16.1 to 1.16.2 Moves backup to common functions
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Profile;
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.DefaultCaret;
|
||||
@ -66,7 +66,7 @@ public class Console implements ActionListener, KeyListener {
|
||||
//Sends the command from the input to the server with the same name.
|
||||
if (e.getSource() == textInput) {
|
||||
java.lang.String text = textInput.getText();
|
||||
Profile.getCurrent().sendCommand(this.name, text);
|
||||
Main.getController().getCurrentProfile().sendCommand(this.name, text);
|
||||
commands.add(text);
|
||||
if (commands.size() > 25) {
|
||||
commands.remove(0);
|
||||
|
@ -1,5 +1,7 @@
|
||||
package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Describes a generic GUI
|
||||
*/
|
||||
@ -42,4 +44,12 @@ public interface GUI {
|
||||
*/
|
||||
void showMessage(String message);
|
||||
|
||||
/**
|
||||
* Asks the user for a directory as a file object
|
||||
*
|
||||
* @param prompt <p>The prompt to show the user</p>
|
||||
* @return <p>The directory given by the user</p>
|
||||
*/
|
||||
File askForDirectory(String prompt);
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* This class handles displaying messages to the user
|
||||
*/
|
||||
public abstract class MessageHandler implements GUI {
|
||||
|
||||
private final boolean silent;
|
||||
|
@ -2,6 +2,7 @@ package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Collection;
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Controller;
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Profile;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
@ -62,6 +63,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
private String aboutText;
|
||||
private SystemTray tray;
|
||||
private TrayIcon trayIcon;
|
||||
private Controller controller;
|
||||
|
||||
/**
|
||||
* Creates the application window
|
||||
@ -70,6 +72,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
super(false);
|
||||
initialize(440, 170);
|
||||
loadMessages();
|
||||
this.controller = Main.getController();
|
||||
this.globalPlayers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@ -83,6 +86,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
super(false);
|
||||
initialize(width, height);
|
||||
loadMessages();
|
||||
this.controller = Main.getController();
|
||||
this.globalPlayers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@ -105,6 +109,21 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File askForDirectory(String prompt) {
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setCurrentDirectory(new java.io.File("."));
|
||||
chooser.setDialogTitle(prompt);
|
||||
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
chooser.setAcceptAllFileFilterUsed(false);
|
||||
|
||||
if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
|
||||
setStatus("Choice aborted by user");
|
||||
return null;
|
||||
}
|
||||
return chooser.getSelectedFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a player to the global playerlist, and updates the players combo.
|
||||
*
|
||||
@ -129,10 +148,12 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
* Updates the profiles combo.
|
||||
*/
|
||||
public void updateProfiles() {
|
||||
String selectedProfile = Main.getController().getCurrentProfile().getName();
|
||||
this.profiles.removeAllItems();
|
||||
for (Profile profile : Profile.getProfiles()) {
|
||||
this.profiles.addItem(profile.getName());
|
||||
for (String profile : Main.getController().getProfileNames()) {
|
||||
this.profiles.addItem(profile);
|
||||
}
|
||||
this.profiles.setSelectedItem(selectedProfile);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,16 +169,15 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
* Updates ServerLauncherGUI according to current profile settings.
|
||||
*/
|
||||
public void update() {
|
||||
Controller controller = Main.getController();
|
||||
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());
|
||||
chckbxmntmRunInBackground.setState(controller.getRunInBackground());
|
||||
chckbxmntmDelayStartup.setState(controller.getDelayStartup() > 0);
|
||||
chckbxmntmDownloadJars.setState(controller.getDownloadAllJars());
|
||||
this.targetServer.removeAllItems();
|
||||
this.targetServer.addItem("All");
|
||||
for (Collection collection : Profile.getCurrent().getCollections()) {
|
||||
for (Collection collection : controller.getCurrentProfile().getCollections()) {
|
||||
serversPane.addTab(collection.getName(), collection.getServerTab().getPanel());
|
||||
this.targetServer.addItem(collection.getName());
|
||||
}
|
||||
}
|
||||
@ -453,11 +473,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
trayIcon.setImageAutoSize(true);
|
||||
ActionListener exitListener = e -> {
|
||||
stop();
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
controller.saveState();
|
||||
System.exit(0);
|
||||
};
|
||||
|
||||
@ -481,7 +497,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
if (Profile.getCurrent().getRunInBackground() && SystemTray.isSupported()) {
|
||||
if (controller.getRunInBackground() && SystemTray.isSupported()) {
|
||||
try {
|
||||
tray.add(trayIcon);
|
||||
frame.setVisible(false);
|
||||
@ -490,11 +506,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
}
|
||||
} else {
|
||||
stop();
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
controller.saveState();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@ -514,11 +526,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
controller.saveState();
|
||||
stop();
|
||||
System.exit(0);
|
||||
}
|
||||
@ -572,68 +580,60 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
} 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();
|
||||
}
|
||||
controller.saveState();
|
||||
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);
|
||||
controller.getCurrentProfile().addCollection(serverName);
|
||||
} catch (ConfigurationException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
this.update();
|
||||
Profile.getCurrent().updateConsoles();
|
||||
controller.getCurrentProfile().updateConsoles();
|
||||
} else if (e.getSource() == backup) {
|
||||
backup();
|
||||
CommonFunctions.backup(this);
|
||||
} else if (e.getSource() == addProfile) {
|
||||
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
|
||||
controller.addProfile(JOptionPane.showInputDialog("Profile name: "));
|
||||
updateProfiles();
|
||||
} else if (e.getSource() == delProfile) {
|
||||
Object selected = profiles.getSelectedItem();
|
||||
if (selected != null) {
|
||||
Profile.removeProfile(selected.toString());
|
||||
controller.removeProfile(selected.toString());
|
||||
updateProfiles();
|
||||
}
|
||||
} else if (e.getSource() == profiles) {
|
||||
try {
|
||||
changeProfile();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
changeProfile();
|
||||
} else if (e.getSource() == btnKick) {
|
||||
if (selectedServerValue != null && selectedPlayerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
|
||||
}
|
||||
} else if (e.getSource() == btnBan) {
|
||||
if (selectedServerValue != null && selectedPlayerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
|
||||
}
|
||||
} else if (e.getSource() == btnOp) {
|
||||
if (selectedServerValue != null && selectedPlayerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
|
||||
}
|
||||
} else if (e.getSource() == btnDeop) {
|
||||
if (selectedServerValue != null && selectedPlayerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
|
||||
}
|
||||
} else if (e.getSource() == btnCustomCommand) {
|
||||
if (selectedServerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, customCommand.getText());
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, customCommand.getText());
|
||||
customCommand.setText("");
|
||||
}
|
||||
} else if (e.getSource() == btnSaveserver) {
|
||||
if (selectedServerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "save-all");
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "save-all");
|
||||
}
|
||||
} else if (e.getSource() == btnReload) {
|
||||
if (selectedServerValue != null) {
|
||||
Profile.getCurrent().sendCommand(selectedServerValue, "reload");
|
||||
controller.getCurrentProfile().sendCommand(selectedServerValue, "reload");
|
||||
}
|
||||
} else if (e.getSource() == btnServerConsoles) {
|
||||
ServerConsoles.setAsVisible();
|
||||
@ -661,14 +661,14 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
/**
|
||||
* Saves the previous profile and loads data from the new profile.
|
||||
*/
|
||||
private void changeProfile() throws FileNotFoundException {
|
||||
Profile.getCurrent().save();
|
||||
private void changeProfile() {
|
||||
controller.saveState();
|
||||
Object current = this.profiles.getSelectedItem();
|
||||
if (current != null) {
|
||||
Profile.setCurrent(current.toString());
|
||||
controller.setCurrentProfile(current.toString());
|
||||
}
|
||||
this.update();
|
||||
Profile.getCurrent().updateConsoles();
|
||||
controller.getCurrentProfile().updateConsoles();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -690,11 +690,15 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
private void delay() {
|
||||
Object selected = profiles.getSelectedItem();
|
||||
if (selected != null) {
|
||||
Profile profile = Profile.getProfile(selected.toString());
|
||||
Profile profile = controller.getProfileByName(selected.toString());
|
||||
if (chckbxmntmDelayStartup.isSelected()) {
|
||||
Objects.requireNonNull(profile).setDelayStartup(
|
||||
Integer.parseInt(JOptionPane.showInputDialog("Seconds to delay: "))
|
||||
);
|
||||
String response = JOptionPane.showInputDialog("Seconds to delay: ");
|
||||
if (response == null) {
|
||||
chckbxmntmDelayStartup.setState(false);
|
||||
return;
|
||||
}
|
||||
int parsed = Integer.parseInt(response);
|
||||
Objects.requireNonNull(profile).setDelayStartup(parsed);
|
||||
} else {
|
||||
Objects.requireNonNull(profile).setDelayStartup(0);
|
||||
}
|
||||
@ -709,7 +713,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
private void background() {
|
||||
Object selected = profiles.getSelectedItem();
|
||||
if (selected != null) {
|
||||
Profile profile = Profile.getProfile(selected.toString());
|
||||
Profile profile = controller.getProfileByName(selected.toString());
|
||||
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
|
||||
} else {
|
||||
showError("No profile selected");
|
||||
@ -722,47 +726,12 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
private void downloadJars() {
|
||||
Object selected = profiles.getSelectedItem();
|
||||
if (selected != null) {
|
||||
Profile profile = Profile.getProfile(selected.toString());
|
||||
Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected());
|
||||
controller.setDownloadAllJars(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,
|
||||
*/
|
||||
@ -777,7 +746,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
targetPlayer.addItem(player);
|
||||
}
|
||||
} else {
|
||||
for (String player : Profile.getCurrent().getCollection(selectedServerValue).getServer().getPlayers()) {
|
||||
for (String player : controller.getCurrentProfile().getCollection(selectedServerValue).getServer().getPlayers()) {
|
||||
targetPlayer.addItem(player);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.profile.Profile;
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
|
||||
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
|
||||
@ -29,7 +29,7 @@ public class ServerTab implements ActionListener {
|
||||
public ServerTab(String name) throws ConfigurationException {
|
||||
this.name = name;
|
||||
panel = new JPanel();
|
||||
Profile.getGUI().getPane().addTab(name, null, panel, null);
|
||||
Main.getController().getGUI().getPane().addTab(name, null, panel, null);
|
||||
SpringLayout sl_panel_3 = new SpringLayout();
|
||||
panel.setLayout(sl_panel_3);
|
||||
|
||||
@ -197,9 +197,9 @@ public class ServerTab implements ActionListener {
|
||||
* Removes the collection containing this ServerTab, and updates everything necessary.
|
||||
*/
|
||||
private void remove() {
|
||||
Profile.getCurrent().removeCollection(this.name);
|
||||
Profile.getGUI().update();
|
||||
Profile.getCurrent().updateConsoles();
|
||||
Main.getController().getCurrentProfile().removeCollection(this.name);
|
||||
Main.getController().getGUI().update();
|
||||
Main.getController().getCurrentProfile().updateConsoles();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user