diff --git a/src/net/knarcraft/serverlauncher/Main.java b/src/net/knarcraft/serverlauncher/Main.java index a53f554..d0f5157 100644 --- a/src/net/knarcraft/serverlauncher/Main.java +++ b/src/net/knarcraft/serverlauncher/Main.java @@ -32,7 +32,7 @@ public class Main { setup(); gui = new GUI(); new ServerConsoles(); - Profile.addProfile("Default"); + Profile.load(); //TODO: replace with profiles loading generating a default profile if empty. ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); diff --git a/src/net/knarcraft/serverlauncher/profile/Collection.java b/src/net/knarcraft/serverlauncher/profile/Collection.java index ddb8bf8..734901f 100644 --- a/src/net/knarcraft/serverlauncher/profile/Collection.java +++ b/src/net/knarcraft/serverlauncher/profile/Collection.java @@ -22,6 +22,14 @@ public class Collection { 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() { return this.name; } diff --git a/src/net/knarcraft/serverlauncher/profile/Profile.java b/src/net/knarcraft/serverlauncher/profile/Profile.java index 9a36cef..073dc1c 100644 --- a/src/net/knarcraft/serverlauncher/profile/Profile.java +++ b/src/net/knarcraft/serverlauncher/profile/Profile.java @@ -6,10 +6,9 @@ import net.knarcraft.serverlauncher.userinterface.ServerTab; import net.knarcraft.serverlauncher.Main; import javax.swing.*; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.PrintWriter; +import java.io.*; import java.util.ArrayList; +import java.util.Scanner; /** * 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) { if (name == null) { //If a user cancels or crosses out window return; @@ -195,17 +206,62 @@ public class Profile { } server.toggle(serverTab.enabled()); } - StringBuilder saveString = new StringBuilder(String.format("%s;%b;%b;%b?", this.name, this.runInBackground, this.delayStartup, this.downloadJars)); - for (Collection collection : this.collections) { - 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 = new StringBuilder(saveString.substring(0, saveString.length() - 1)); try (PrintWriter file = new PrintWriter("files/Profiles.txt")) { - file.println(saveString); - } catch (FileNotFoundException e) { - JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "About", JOptionPane.INFORMATION_MESSAGE); + 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(); + 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)); + try (PrintWriter fileAppend = new PrintWriter(new FileWriter("files/Profiles.txt", true))) { + fileAppend.println(saveString); + } catch (IOException e) { + 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(); + } } diff --git a/src/net/knarcraft/serverlauncher/userinterface/GUI.java b/src/net/knarcraft/serverlauncher/userinterface/GUI.java index f265d58..3157a59 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/GUI.java +++ b/src/net/knarcraft/serverlauncher/userinterface/GUI.java @@ -118,15 +118,6 @@ public class GUI implements ActionListener { 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() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); @@ -511,7 +502,6 @@ public class GUI implements ActionListener { */ private void save() { Profile.getCurrent().save(); - //TODO: Save to a text file. } /** diff --git a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java index cc7194a..5f12a12 100644 --- a/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java +++ b/src/net/knarcraft/serverlauncher/userinterface/ServerTab.java @@ -25,6 +25,15 @@ public class ServerTab implements ActionListener { private final JPanel panel; 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) { this.name = name; panel = new JPanel();