Version string is saved in each server, rather than each profile

To prevent one server from being updated, and making the other servers
think they are updated, this was necessary.
This commit is contained in:
Kristian Knarvik 2018-02-03 11:00:45 +01:00
parent 75d44f6d1d
commit fd65b46ea2
2 changed files with 114 additions and 57 deletions

View File

@ -6,7 +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.util.ArrayList;
/**
@ -25,10 +27,6 @@ public class Profile {
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
private String vanillaVersion;
private String snapshotVersion;
private String spongeVanillaVersion;
private String bungeeVersion;
private Profile(String name) {
this.collections = new ArrayList<>();
@ -180,51 +178,6 @@ 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) {
case "Vanilla":
this.vanillaVersion = version;
break;
case "Snapshot":
this.snapshotVersion = version;
break;
case "SpongeVanilla":
this.spongeVanillaVersion = version;
break;
case "Bungee":
this.bungeeVersion = version;
}
}
}
/**
* 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":
return this.vanillaVersion;
case "Snapshot":
return this.snapshotVersion;
case "SpongeVanilla":
return this.spongeVanillaVersion;
case "Bungee":
return this.bungeeVersion;
default:
return "";
}
}
/**
* Reads all server tabs, and saves it to the variables of the corresponding servers.
*/
@ -238,5 +191,17 @@ public class Profile {
server.setServerVersion(serverTab.getVersion());
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("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);
}
}
}

View File

@ -33,6 +33,10 @@ public class Server {
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
private String vanillaVersion;
private String snapshotVersion;
private String spongeVanillaVersion;
private String bungeeVersion;
public Server(String name) {
this.name = name;
@ -40,9 +44,45 @@ public class Server {
this.enabled = false;
this.playerList = new ArrayList<>();
this.type = null;
this.serverVersion = null;
this.serverVersion = "";
this.maxRam = ramList[0];
this.process = null;
this.writer = null;
this.reader = null;
this.vanillaVersion = "";
this.snapshotVersion = "";
this.spongeVanillaVersion = "";
this.bungeeVersion = "";
}
public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam, String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) {
this.name = name;
this.path = path;
this.enabled = enabled;
this.type = ServerType.getByName(typeName);
this.serverVersion = serverVersion;
this.maxRam = maxRam;
this.vanillaVersion = vanillaVersion;
this.snapshotVersion = snapshotVersion;
this.spongeVanillaVersion = spongeVanillaVersion;
this.bungeeVersion = bungeeVersion;
this.playerList = new ArrayList<>();
}
public String getVanillaVersion() {
return this.vanillaVersion;
}
public String getSnapshotVersion() {
return this.snapshotVersion;
}
public String getSpongeVanillaVersion() {
return this.spongeVanillaVersion;
}
public String getBungeeVersion() {
return this.bungeeVersion;
}
public void addPlayer(String name) {
@ -137,6 +177,14 @@ public class Server {
return this.type.getName();
}
public String getServerVersion() {
return this.serverVersion;
}
public String getMaxRam() {
return this.maxRam;
}
/**
* Sets the server's server version to a valid version, or ignores the request.
*
@ -226,7 +274,6 @@ public class Server {
AdvancedServerType type;
File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
Profile currentProfile = Profile.getCurrent();
String versionText;
String newestVersion;
String url = this.type.getDownloadURL();
@ -260,9 +307,9 @@ public class Server {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
currentProfile.setVersion(this.type.getName(), newestVersion);
this.setVersion(this.type.getName(), newestVersion);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
@ -284,9 +331,9 @@ public class Server {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
currentProfile.setVersion(name, newestVersion);
this.setVersion(name, newestVersion);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
@ -300,9 +347,9 @@ public class Server {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile(url, filePath);
currentProfile.setVersion(name, newestVersion);
this.setVersion(name, newestVersion);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
@ -310,6 +357,51 @@ public class Server {
}
}
/**
* Returns the current version of a type
*
* @param type The version type
* @return The version string
*/
private String getVersion(String type) {
switch (type) {
case "Vanilla":
return this.vanillaVersion;
case "Snapshot":
return this.snapshotVersion;
case "SpongeVanilla":
return this.spongeVanillaVersion;
case "Bungee":
return this.bungeeVersion;
default:
return "";
}
}
/**
* Sets a server type's last downloaded version.
*
* @param type The version type
* @param version The version string
*/
private void setVersion(String type, String version) {
if (!version.equals("")) {
switch (type) {
case "Vanilla":
this.vanillaVersion = version;
break;
case "Snapshot":
this.snapshotVersion = version;
break;
case "SpongeVanilla":
this.spongeVanillaVersion = version;
break;
case "Bungee":
this.bungeeVersion = version;
}
}
}
/**
* Reads a file from a website.
* This is used to find the newest version of the software.