Fixes casting problems

This commit is contained in:
Kristian Knarvik 2018-01-25 19:55:28 +01:00
parent 5e4b956e19
commit 7843331605
3 changed files with 63 additions and 61 deletions

View File

@ -26,35 +26,6 @@ public class Main {
// TODO: Add gui
/**
* Adds all the valid server types and versions.
*/
private static void loadServerTypes() throws ConfigurationException {
try (Scanner in = new Scanner(new File("config/servertypes.csv"))) {
while (in.hasNextLine()) {
String[] str = in.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
if (len == 7) {
serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]));
} else if (len == 3) {
serverTypes.add(new ServerType(str[0], ver, str[2]));
} else {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
/**
* Runs all enabled servers with their settings.
*/
@ -83,4 +54,33 @@ public class Main {
}
}
}
/**
* Adds all the valid server types and versions.
*/
private static void loadServerTypes() throws ConfigurationException {
try (Scanner in = new Scanner(new File("config/servertypes.csv"))) {
while (in.hasNextLine()) {
String[] str = in.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
if (len == 7) {
serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]));
} else if (len == 3) {
serverTypes.add(new ServerType(str[0], ver, str[2]));
} else {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
}

View File

@ -18,7 +18,7 @@ public class Server {
private String path;
private boolean enabled;
private ArrayList<String> playerList;
private AdvancedServerType type;
private ServerType type;
private String serverVersion;
private String maxRam;
private long pid;
@ -71,7 +71,7 @@ public class Server {
this.path = path;
}
public void setType(AdvancedServerType type) {
public void setType(ServerType type) {
this.type = type;
}
@ -127,6 +127,7 @@ public class Server {
* @throws FileNotFoundException if the file was not found and could not be acquired.
*/
public void downloadJar() throws FileNotFoundException {
AdvancedServerType type;
File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
String versionText;
@ -142,7 +143,7 @@ public class Server {
case "Craftbukkit":
case "MCPCplus":
if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + type.getName() + this.serverVersion + ".jar", filePath);
success = downloadFile(this.type.getDownloadURL() + this.type.getName() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
@ -150,21 +151,22 @@ public class Server {
break;
case "Vanilla":
case "Snapshot":
type = (AdvancedServerType) this.type;
if (this.serverVersion.equals("Latest")) {
try {
versionText = readFile(this.type.getVersionURL());
versionText = readFile(type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
success = downloadFile(this.type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
} else {
if (!file.isFile()) {
success = downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
success = downloadFile(this.type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
@ -172,26 +174,28 @@ public class Server {
}
break;
case "SpongeVanilla":
type = (AdvancedServerType) this.type;
try {
versionText = readFile(this.type.getVersionURL() + this.serverVersion);
versionText = readFile(type.getVersionURL() + this.serverVersion);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
success = downloadFile(this.type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
break;
case "Bungee":
/*try {
/*type = (AdvancedServerType) this.type;
try {
versionText = readFile(this.type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());*/
success = downloadFile(type.getDownloadURL(), filePath);
success = downloadFile(this.type.getDownloadURL(), filePath);
//TODO: Register the new version number, and only download if version mismatch or missing file.
if (!success) {
throw new FileNotFoundException("Jar file could not be downloaded.");

View File

@ -1,6 +1,3 @@
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.naming.ConfigurationException;
import net.knarcraft.serverlauncher.server.*;
@ -11,25 +8,36 @@ import java.util.ArrayList;
import java.util.Scanner;
public class ServerTest {
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
private static ArrayList<Server> servers = new ArrayList<>();
private static ArrayList<ServerType> serverTypes = new ArrayList<>();
public static void main(String[] args) {
/*try {
try {
loadServerTypes();
} catch (ConfigurationException e) {
e.printStackTrace();
return;
System.exit(1);
}
Server server1 = new Server("Server1");
server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType((AdvancedServerType) serverTypes.get(2));
server1.setServerVersion("1.10.2");
server1.setType(serverTypes.get(4));
//TODO: All types are inside serverTypes, but the ones of ServerType get a casting error.
server1.setServerVersion("1.12.2");
server1.setMaxRam("1G");
servers.add(server1);
startServers(servers);*/
startServers(servers);
}
/**
* Runs all enabled servers with their settings.
*/
private static void startServers(ArrayList<Server> servers) {
System.out.println("Starting servers.");
for (Server server : servers) {
server.run();
}
}
/**
* Adds all the valid server types and versions.
@ -50,23 +58,13 @@ public class ServerTest {
} else if (len == 3) {
serverTypes.add(new ServerType(str[0], ver, str[2]));
} else {
throw new ConfigurationException("Error: Config file invalid.");
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} catch (FileNotFoundException e) {
throw new ConfigurationException("Error: Config file not found.");
throw new ConfigurationException("Error: Configuration file not found.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new ConfigurationException("Error: Config file invalid.");
}
}
/**
* Runs all enabled servers with their settings.
*/
public static void startServers(ArrayList<Server> servers) {
System.out.println("Starting servers.");
for (Server server : servers) {
server.run();
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
}