Profile switching and cleanup

It is now possible to switch between profiles, and the server tabs
will update.
This commit is contained in:
Kristian Knarvik 2018-02-02 22:02:57 +01:00
parent 062f57736b
commit 75d44f6d1d
8 changed files with 202 additions and 127 deletions

View File

@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main
Main-Class: net.knarcraft.serverlauncher.Main

View File

@ -1,5 +1,8 @@
package net.knarcraft.serverlauncher;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI;
import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
@ -20,38 +23,30 @@ import java.util.concurrent.TimeUnit;
* @since 0.0.0.1
*/
class Main {
public class Main {
private static GUI gui;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
setup();
new GUI();
gui = new GUI();
new ServerConsoles();
Profile.addProfile("Default");
//TODO: replace with profiles loading generating a default profile if empty.
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> {
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
if (collection.getServer().isEnabled() && collection.getServer().getProcess() != null) {
try {
String readText = collection.getServer().read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
updatePlayerList(readText, collection);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, 10, 250, TimeUnit.MILLISECONDS);
exec.scheduleAtFixedRate(Main::updateConsoles, 10, 250, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public static GUI gui() {
return gui;
}
private static void setup() {
try {
ServerType.loadServerTypes();
@ -61,32 +56,59 @@ class Main {
}
}
/**
* Reads from server processes, and writes the output to our custom consoles.
*/
private static void updateConsoles() {
for (Collection collection : Profile.getCurrent().getCollections()) {
Server server = collection.getServer();
if (server.isEnabled() && server.getProcess() != null) {
try {
String readText = server.read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
updatePlayerList(readText, collection);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Looks for strings implying a player has joined or left, and updates the appropriate lists.
*
* @param text The text to search.
* @param collection The server which sent the text.
*/
private static void updatePlayerList(String text, Collection collection) {
if (!collection.getServer().typeName().equals("Bungee")) { //Bungee servers are not to be treated as normal servers.
Server server = collection.getServer();
if (!server.typeName().equals("Bungee")) {
String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
if (!joinedPlayer.equals("")) {
if (!collection.getServer().hasPlayer(joinedPlayer)) {
collection.getServer().addPlayer(joinedPlayer);
if (!server.hasPlayer(joinedPlayer)) {
server.addPlayer(joinedPlayer);
}
} else {
joinedPlayer = stringBetween(text, "UUID of player ", " is ");
if (!joinedPlayer.equals("")) {
if (!collection.getServer().hasPlayer(joinedPlayer)) {
collection.getServer().addPlayer(joinedPlayer);
if (!server.hasPlayer(joinedPlayer)) {
server.addPlayer(joinedPlayer);
}
}
}
if (joinedPlayer.equals("")) {
String leftPlayer = stringBetween(text, "INFO]: ", " lost connection");
if (!leftPlayer.equals("")) {
if (collection.getServer().hasPlayer(leftPlayer)) {
collection.getServer().removePlayer(leftPlayer);
if (server.hasPlayer(leftPlayer)) {
server.removePlayer(leftPlayer);
}
} else {
leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game");
if (!leftPlayer.equals("")) {
if (collection.getServer().hasPlayer(leftPlayer)) {
collection.getServer().removePlayer(leftPlayer);
if (server.hasPlayer(leftPlayer)) {
server.removePlayer(leftPlayer);
}
}
}

View File

@ -2,8 +2,8 @@ package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI;
import net.knarcraft.serverlauncher.userinterface.ServerTab;
import net.knarcraft.serverlauncher.Main;
import javax.swing.*;
import java.io.IOException;
@ -18,6 +18,7 @@ import java.util.ArrayList;
*/
public class Profile {
private static final ArrayList<Profile> profiles = new ArrayList<>();
private static Profile current;
private final ArrayList<Collection> collections;
private final String name;
@ -36,25 +37,30 @@ public class Profile {
this.delayStartup = 0;
this.downloadJars = false;
profiles.add(this);
if (current == null) {
current = this;
}
}
public void addCollection(String name) {
if (name == null) { //If a user cancels or crosses out window
return;
}
if (!collectionExists(name) && !name.equals("") && !name.equals("All")) {
collections.add(new Collection(name));
GUI.getGUI().updateSelectServers();
} else {
JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void removeCollection(ServerTab serverTab) {
public void removeCollection(String name) {
for (int i = 0; i < collections.size(); i++) {
if (collections.get(i).getServerTab() == serverTab) {
if (collections.get(i).getName().equals(name)) {
this.collections.remove(i);
GUI.getGUI().updateSelectServers();
Main.gui().updateSelectServers();
break;
}
}
}
public Collection getCollection(String name) {
@ -94,7 +100,7 @@ public class Profile {
}
}
new Profile(name);
GUI.getGUI().addProfile(name);
Main.gui().addProfile(name);
}
public static Profile getProfile(String name) {
@ -111,12 +117,25 @@ public class Profile {
for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) {
profiles.remove(i);
GUI.getGUI().removeProfile(i);
Main.gui().removeProfile(i);
}
}
}
}
public static Profile getCurrent() {
return current;
}
public static void setCurrent(String name) {
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
current = profile;
break;
}
}
}
public void setRunInBackground(boolean value) {
this.runInBackground = value;
}
@ -131,13 +150,19 @@ public class Profile {
this.downloadJars = value;
}
/**
* Sends a command to a server, or all servers
*
* @param serverName The target server
* @param command The command to send.
*/
public void sendCommand(String serverName, String command) {
if (serverName.equals("All")) {
for (Collection collection : this.collections) {
try {
collection.getServer().sendCommand(command);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + collection.getServer().getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Server " + collection.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} else {
@ -155,6 +180,12 @@ public class Profile {
}
}
/**
* Sets a server type's last downloaded version.
*
* @param type The version type
* @param version The version string
*/
public void setVersion(String type, String version) {
if (!version.equals("")) {
switch (type) {
@ -173,6 +204,12 @@ public class Profile {
}
}
/**
* Returns the current version of a type
*
* @param type The version type
* @return The version string
*/
public String getVersion(String type) {
switch (type) {
case "Vanilla":
@ -188,6 +225,9 @@ public class Profile {
}
}
/**
* Reads all server tabs, and saves it to the variables of the corresponding servers.
*/
public void save() {
for (Collection collection : this.collections) {
Server server = collection.getServer();

View File

@ -1,9 +1,8 @@
package net.knarcraft.serverlauncher.server;
import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.userinterface.GUI;
import java.io.*;
import java.net.URL;
import java.util.Scanner;
@ -48,7 +47,7 @@ public class Server {
public void addPlayer(String name) {
this.playerList.add(name);
GUI.getGUI().addPlayer(name);
Main.gui().addPlayer(name);
}
public void removePlayer(String name) {
@ -57,7 +56,7 @@ public class Server {
playerList.remove(i);
}
}
GUI.getGUI().removePlayer(name);
Main.gui().removePlayer(name);
}
public ArrayList<String> getPlayers() {
@ -121,7 +120,7 @@ public class Server {
}
public static void stop() throws IOException {
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
for (Collection collection : Profile.getCurrent().getCollections()) {
if (collection.getServer().writer != null) {
if (collection.getServer().type.getName().equals("Bungee")) {
collection.getServer().writer.write("end\n");
@ -158,10 +157,10 @@ public class Server {
* Runs all enabled servers with their settings.
*/
public static void startServers() {
GUI.getGUI().setStatus("Starting servers");
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
Main.gui().setStatus("Starting servers");
for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().run()) {
GUI.getGUI().setStatus("An error occurred. Start aborted");
Main.gui().setStatus("An error occurred. Start aborted");
}
}
}
@ -172,11 +171,11 @@ public class Server {
private boolean run() {
if (this.enabled) {
try {
GUI.getGUI().setStatus("Downloading jar...");
Main.gui().setStatus("Downloading jar...");
this.downloadJar();
GUI.getGUI().setStatus("File downloaded");
Main.gui().setStatus("File downloaded");
} catch (FileNotFoundException e) {
GUI.getGUI().setStatus("Error: Jar file not found");
Main.gui().setStatus("Error: Jar file not found");
return false;
}
try {
@ -197,10 +196,10 @@ public class Server {
this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
InputStream stdout = this.process.getInputStream();
this.reader = new BufferedReader (new InputStreamReader(stdout));
GUI.getGUI().setStatus("Servers are running");
Main.gui().setStatus("Servers are running");
return true;
} catch (IOException e) {
GUI.getGUI().setStatus("Could not start server");
Main.gui().setStatus("Could not start server");
return false;
}
} else {
@ -227,7 +226,7 @@ public class Server {
AdvancedServerType type;
File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
Profile currentProfile = GUI.getGUI().currentProfile();
Profile currentProfile = Profile.getCurrent();
String versionText;
String newestVersion;
String url = this.type.getDownloadURL();
@ -263,7 +262,7 @@ public class Server {
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
GUI.getGUI().currentProfile().setVersion(this.type.getName(), newestVersion);
currentProfile.setVersion(this.type.getName(), newestVersion);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}

View File

@ -29,12 +29,10 @@ public class Console implements ActionListener {
panel = new JPanel();
tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0));
textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10);
textInput.addActionListener(this);
textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER);
@ -54,10 +52,9 @@ public class Console implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) {
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
String text = textInput.getText();
Profile profile = GUI.getGUI().currentProfile();
profile.sendCommand(this.name, text);
Profile.getCurrent().sendCommand(this.name, text);
textInput.setText("");
}
}

View File

@ -28,10 +28,7 @@ import java.util.concurrent.Executors;
*/
public class GUI implements ActionListener {
private static GUI gui;
private JFrame frame;
//Menu
private JCheckBoxMenuItem chckbxmntmRunInBackground, chckbxmntmDelayStartup, chckbxmntmDownloadJars; //Options
private JMenuItem mntmErrors, mntmSetup, mntmManualUpdate; //Help
@ -55,17 +52,15 @@ public class GUI implements ActionListener {
private final ArrayList<String> globalPlayers;
private JTabbedPane serversPane;
/**
* Create the application window.
*/
public GUI() {
initialize();
loadMessages();
gui = this;
this.globalPlayers = new ArrayList<>();
initialize();
loadMessages();
this.globalPlayers = new ArrayList<>();
}
public void addPlayer(String name) {
@ -82,6 +77,9 @@ public class GUI implements ActionListener {
this.updatePlayers();
}
/**
* Updates the list of players currently online on the selected server,
*/
private void updatePlayers() {
String selectedServerValue;
Object selectedServer = targetServer.getSelectedItem();
@ -91,7 +89,7 @@ public class GUI implements ActionListener {
if (selectedServerValue.equals("All")) {
for (String player : this.globalPlayers) targetPlayer.addItem(player);
} else {
for (String player : this.currentProfile().getCollection(selectedServerValue).getServer().getPlayers())
for (String player : Profile.getCurrent().getCollection(selectedServerValue).getServer().getPlayers())
targetPlayer.addItem(player);
}
}
@ -99,7 +97,7 @@ public class GUI implements ActionListener {
public void update() {
serversPane.removeAll();
for (Collection collection : currentProfile().getCollections()) {
for (Collection collection : Profile.getCurrent().getCollections()) {
serversPane.addTab(collection.getName(), collection.getServerTab().getPanel());
}
}
@ -120,18 +118,14 @@ public class GUI implements ActionListener {
this.profiles.removeItemAt(index);
}
public Profile currentProfile() {
/*public Profile currentProfile() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
return Profile.getProfile(selected.toString());
} else {
return null;
}
}
public static GUI getGUI() {
return gui;
}
}*/
private void initialize() {
try {
@ -409,7 +403,7 @@ public class GUI implements ActionListener {
public void updateSelectServers() {
this.targetServer.removeAllItems();
this.targetServer.addItem("All");
for (Collection collection : currentProfile().getCollections()) {
for (Collection collection : Profile.getCurrent().getCollections()) {
this.targetServer.addItem(collection.getName());
}
}
@ -454,9 +448,9 @@ public class GUI implements ActionListener {
stop();
} else if (e.getSource() == addServer) {
String serverName = JOptionPane.showInputDialog("Name of server: ");
this.currentProfile().addCollection(serverName);
Profile.getCurrent().addCollection(serverName);
this.update();
ServerConsoles.update(currentProfile().getCollections());
ServerConsoles.update();
} else if (e.getSource() == backup) {
backup();
} else if (e.getSource() == addProfile) {
@ -466,45 +460,57 @@ public class GUI implements ActionListener {
if (selected != null) {
Profile.deleteProfile(selected.toString());
}
} else if (e.getSource() == profiles) {
changeProfile();
} else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
}
} else if (e.getSource() == btnBan) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
Profile.getCurrent().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
}
} else if (e.getSource() == btnOp) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
Profile.getCurrent().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
}
} else if (e.getSource() == btnDeop) {
if (selectedServerValue != null && selectedPlayerValue != null) {
currentProfile().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
Profile.getCurrent().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
}
} else if (e.getSource() == btnCustomCommand) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, customCommand.getText());
Profile.getCurrent().sendCommand(selectedServerValue, customCommand.getText());
customCommand.setText("");
}
} else if (e.getSource() == btnSaveserver) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "save-all");
Profile.getCurrent().sendCommand(selectedServerValue, "save-all");
}
} else if (e.getSource() == btnReload) {
if (selectedServerValue != null) {
currentProfile().sendCommand(selectedServerValue, "reload");
Profile.getCurrent().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
ServerConsoles.show();
}
}
private void changeProfile() {
save();
Object current = profiles.getSelectedItem();
if (current != null) {
Profile.setCurrent(current.toString());
}
update();
ServerConsoles.update();
}
/**
* Reads all combo boxes, updates variables (and saves to disk).
*/
private void save() {
currentProfile().save();
Profile.getCurrent().save();
//TODO: Save to a text file.
}
@ -581,7 +587,7 @@ public class GUI implements ActionListener {
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile();
for (Collection collection : currentProfile().getCollections()) {
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());
@ -608,7 +614,7 @@ public class GUI implements ActionListener {
*
* @param url URL to open
*/
private static void goToURL(String url) {
private void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try {
desktop.browse(new URI(url));

View File

@ -1,10 +1,11 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.util.ArrayList;
/**
* A parent window for server consoles.
@ -16,7 +17,6 @@ import java.util.ArrayList;
* @since 0.0.0.1
*/
public class ServerConsoles {
private static ServerConsoles serverConsoles;
private static JFrame frame;
private static JTabbedPane consolesTab;
@ -27,20 +27,15 @@ public class ServerConsoles {
consolesTab = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(consolesTab, BorderLayout.CENTER);
serverConsoles = this;
}
public static ServerConsoles getGUI() {
return serverConsoles;
}
public static void show() {
frame.setVisible(true);
}
public static void update(ArrayList<Collection> collections) {
public static void update() {
consolesTab.removeAll();
for (Collection collection : collections) {
for (Collection collection : Profile.getCurrent().getCollections()) {
consolesTab.add(collection.getName(), collection.getServerConsole().getPanel());
}
}

View File

@ -1,5 +1,7 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
@ -21,10 +23,12 @@ public class ServerTab implements ActionListener {
private final JButton btnRemoveServer, btnBrowse;
private final JTextField directory;
private final JPanel panel;
private final String name;
public ServerTab(String name) {
this.name = name;
panel = new JPanel();
GUI.getGUI().getPane().addTab(name, null, panel, null);
Main.gui().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout();
panel.setLayout(sl_panel_3);
@ -137,7 +141,7 @@ public class ServerTab implements ActionListener {
if (selected != null) {
return selected.toString();
} else {
return "Vanilla";
return "Latest";
}
}
@ -148,40 +152,52 @@ public class ServerTab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRemoveServer) {
GUI.getGUI().currentProfile().removeCollection(this);
GUI.getGUI().update();
ServerConsoles.update(GUI.getGUI().currentProfile().getCollections());
remove();
} else if (e.getSource() == btnBrowse) {
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) {
directory.setText(chooser.getSelectedFile().toString());
}
browse();
} else if (e.getSource() == serverTypes) {
serverVersions.removeAllItems();
String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem();
if (selectedType != null) {
selectedserverTypes = selectedType.toString();
}
if (selectedserverTypes != null) {
if (selectedserverTypes.equals("Custom")) {
serverVersions.setEditable(true);
} else {
serverVersions.setEditable(false);
ServerType current = null;
for (ServerType servertype : ServerType.getServerTypes()) {
if (servertype.getName().equals(selectedserverTypes)) {
current = servertype;
}
serverTypes();
}
}
private void remove() {
Profile.getCurrent().removeCollection(name);
Main.gui().update();
ServerConsoles.update();
}
private void browse() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("/"));
chooser.setDialogTitle("Server folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
directory.setText(chooser.getSelectedFile().toString());
}
}
private void serverTypes() {
serverVersions.removeAllItems();
String selectedserverTypes = null;
Object selectedType = serverTypes.getSelectedItem();
if (selectedType != null) {
selectedserverTypes = selectedType.toString();
}
if (selectedserverTypes != null) {
if (selectedserverTypes.equals("Custom")) {
serverVersions.setEditable(true);
} else {
serverVersions.setEditable(false);
ServerType current = null;
for (ServerType servertype : ServerType.getServerTypes()) {
if (servertype.getName().equals(selectedserverTypes)) {
current = servertype;
}
if (current != null) {
for (String version : current.getVersions()) {
serverVersions.addItem(version);
}
}
if (current != null) {
for (String version : current.getVersions()) {
serverVersions.addItem(version);
}
}
}