Implements proper saving and loading of servers and profiles

This commit is contained in:
Kristian Knarvik 2018-02-03 14:39:22 +01:00
parent 27024d7b8f
commit e9e500fb2f
5 changed files with 87 additions and 24 deletions

View File

@ -32,7 +32,7 @@ public class Main {
setup(); setup();
gui = new GUI(); gui = new GUI();
new ServerConsoles(); new ServerConsoles();
Profile.addProfile("Default"); Profile.load();
//TODO: replace with profiles loading generating a default profile if empty. //TODO: replace with profiles loading generating a default profile if empty.
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();

View File

@ -22,6 +22,14 @@ public class Collection {
this.name = name; this.name = name;
} }
Collection(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam, String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) {
this.serverTab = new ServerTab(name);
this.server = new Server(name, path, enabled, typeName, serverVersion, maxRam, vanillaVersion, snapshotVersion, spongeVanillaVersion, bungeeVersion);
this.serverConsole = ServerConsoles.addTab(name);
this.name = name;
this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam);
}
public String getName() { public String getName() {
return this.name; return this.name;
} }

View File

@ -6,10 +6,9 @@ import net.knarcraft.serverlauncher.userinterface.ServerTab;
import net.knarcraft.serverlauncher.Main; import net.knarcraft.serverlauncher.Main;
import javax.swing.*; import javax.swing.*;
import java.io.FileNotFoundException; import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
/** /**
* Contains all user settings, and a list of servers. * Contains all user settings, and a list of servers.
@ -40,6 +39,18 @@ public class Profile {
} }
} }
private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadJars) {
this.collections = new ArrayList<>();
this.name = name;
this.runInBackground = runInBackground;
this.delayStartup = delayStartup;
this.downloadJars = downloadJars;
profiles.add(this);
if (current == null) {
current = this;
}
}
public void addCollection(String name) { public void addCollection(String name) {
if (name == null) { //If a user cancels or crosses out window if (name == null) { //If a user cancels or crosses out window
return; return;
@ -195,17 +206,62 @@ public class Profile {
} }
server.toggle(serverTab.enabled()); server.toggle(serverTab.enabled());
} }
StringBuilder saveString = new StringBuilder(String.format("%s;%b;%b;%b?", this.name, this.runInBackground, this.delayStartup, this.downloadJars)); try (PrintWriter file = new PrintWriter("files/Profiles.txt")) {
for (Collection collection : this.collections) { file.print("");
for (Profile profile : profiles) {
StringBuilder saveString = new StringBuilder(String.format("%s;%b;%d;%b?", profile.name, profile.runInBackground, profile.delayStartup, profile.downloadJars));
for (Collection collection : profile.collections) {
Server server = collection.getServer(); Server server = collection.getServer();
saveString.append(String.format("%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!", server.getName(), server.getPath(), server.isEnabled(), server.typeName(), server.getServerVersion(), server.getMaxRam(), server.getVanillaVersion(), server.getSnapshotVersion(), server.getSpongeVanillaVersion(), server.getBungeeVersion())); saveString.append(String.format("%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!", server.getName(), server.getPath(), server.isEnabled(), server.typeName(), server.getServerVersion(), server.getMaxRam(), server.getVanillaVersion(), server.getSnapshotVersion(), server.getSpongeVanillaVersion(), server.getBungeeVersion()));
} }
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1)); saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
try (PrintWriter file = new PrintWriter("files/Profiles.txt")) { try (PrintWriter fileAppend = new PrintWriter(new FileWriter("files/Profiles.txt", true))) {
file.println(saveString); fileAppend.println(saveString);
} catch (FileNotFoundException e) { } catch (IOException e) {
JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "About", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "Error", JOptionPane.ERROR_MESSAGE);
} }
} }
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void load() {
try (Scanner in = new Scanner(new File("files/Profiles.txt"))) {
try {
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.contains("?")) {
String[] data = line.split("\\?");
String[] profileData = data[0].split(";", -1);
Profile profile = new Profile(profileData[0], Boolean.parseBoolean(profileData[1]), Integer.parseInt(profileData[2]), Boolean.parseBoolean(profileData[3]));
Main.gui().addProfile(profileData[0]);
if (data[1].contains("!")) {
String[] servers = data[1].split("!", -1);
for (String server : servers) {
String[] serverData = server.split(";", -1);
profile.collections.add(new Collection(serverData[0], serverData[1], Boolean.parseBoolean(serverData[2]), serverData[3], serverData[4], serverData[5], serverData[6], serverData[7], serverData[8], serverData[9]));
}
} else {
String[] serverData = data[1].split(";", -1);
profile.collections.add(new Collection(serverData[0], serverData[1], Boolean.parseBoolean(serverData[2]), serverData[3], serverData[4], serverData[5], serverData[6], serverData[7], serverData[8], serverData[9]));
}
} else {
String[] profileData = line.split(";", -1);
new Profile(profileData[0], Boolean.parseBoolean(profileData[1]), Integer.parseInt(profileData[2]), Boolean.parseBoolean(profileData[3]));
Main.gui().addProfile(profileData[0]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (profiles.size() == 0) {
addProfile("Default");
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "A profiles file was not found. Default profile was created.", "Info", JOptionPane.INFORMATION_MESSAGE);
addProfile("Default");
}
Main.gui().update();
}
} }

View File

@ -118,15 +118,6 @@ public class GUI implements ActionListener {
this.profiles.removeItemAt(index); this.profiles.removeItemAt(index);
} }
/*public Profile currentProfile() {
Object selected = profiles.getSelectedItem();
if (selected != null) {
return Profile.getProfile(selected.toString());
} else {
return null;
}
}*/
private void initialize() { private void initialize() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -511,7 +502,6 @@ public class GUI implements ActionListener {
*/ */
private void save() { private void save() {
Profile.getCurrent().save(); Profile.getCurrent().save();
//TODO: Save to a text file.
} }
/** /**

View File

@ -25,6 +25,15 @@ public class ServerTab implements ActionListener {
private final JPanel panel; private final JPanel panel;
private final String name; private final String name;
public void setData(String path, boolean enabled, String typeName, String serverVersion, String maxRam) {
this.directory.setText(path);
this.chckbxEnabled.setSelected(enabled);
this.serverTypes.setSelectedItem(typeName);
this.serverTypes();
this.serverTypes.setSelectedItem(serverVersion);
this.maxRam.setSelectedItem(maxRam);
}
public ServerTab(String name) { public ServerTab(String name) {
this.name = name; this.name = name;
panel = new JPanel(); panel = new JPanel();