Fixes mistakes and adds comments

This commit is contained in:
Kristian Knarvik 2018-01-19 20:43:03 +00:00
parent d1fca3ca9d
commit 227e9141e9
3 changed files with 81 additions and 20 deletions

View File

@ -2,48 +2,98 @@ import java.lang.Runtime;
import java.util.ArrayList; import java.util.ArrayList;
import java.net.URL; import java.net.URL;
import java.util.Scanner; import java.util.Scanner;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.net.MalformedURLException;
public class Main { public class Main {
public ArrayList<Server> servers = new ArrayList<Server>(); public static ArrayList<Server> servers = new ArrayList<Server>();
public ArrayList<ServerType> serverTypes = new ArrayList<ServerType>(); public static ArrayList<ServerType> serverTypes = new ArrayList<ServerType>();
private final BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from.
private final BUKKITURL = BASEURL + "/Bukkit/"; final String BUKKITURL = BASEURL + "/Bukkit/";
private final MCPCURL = BASEURL + "/MCPC+/"; final String MCPCURL = BASEURL + "/MCPC+/";
private final SPIGOT = BASEURL + "/Spigot/"; final String SPIGOT = BASEURL + "/Spigot/";
public static void main(String[] args) { public static void main(String[] args) {
addServerTypes(); addServerTypes();
} }
/**
* Adds all the valid server types and versions.
*/
public static void addServerTypes() { public static void addServerTypes() {
serverTypes.add(new ServerType("Vanilla", {"Latest", "1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.9", "1.7.10", "1.6.4", "1.5.2", "1.4.7", "1.3.2", "1.2.5"})); //This could be changed to read from a list which is downloaded to the user's computer.
serverTypes.add(new ServerType("Snapshot", {"Latest"})); serverTypes.add(new ServerType("Vanilla", new String[]{"Latest", "1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.9", "1.7.10", "1.6.4", "1.5.2", "1.4.7", "1.3.2", "1.2.5"}));
serverTypes.add(new ServerType("SpongeVanilla", {"1.11.2", "1.10.2", "1.8.9"})); serverTypes.add(new ServerType("Snapshot", new String[]{"Latest"}));
serverTypes.add(new ServerType("Spigot", {"1.12", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"})); serverTypes.add(new ServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}));
serverTypes.add(new ServerType("MCPCplus", {"1.6.4", "1.6.2", "1.5.2", "1.4.7"})); serverTypes.add(new ServerType("Spigot", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}));
serverTypes.add(new ServerType("Craftbukkit", {"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"})); serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}));
serverTypes.add(new ServerType("Custom", {""})); serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}));
serverTypes.add(new ServerType("Custom", new String[]{""}));
} }
/**
* Runs all enabled servers with their settings.
*/
public static void startServers() { public static void startServers() {
//This should update the server status to running. The output should display in a custom commandline interface.
for (Server server : servers) { for (Server server : servers) {
if (server.isEnabled()) { if (server.isEnabled()) {
String path = server.getPath(); String path = server.getPath();
String type = server.getType(); String type = server.getType();
String ram = server.maxRam(); String ram = server.maxRam();
Runtime rt = Runtime.getRuntime(); Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -Xmx" + ram + " -Xms512M -jar " + '"' + path + "\\" + type + '" nogui'); try {
Process pr = rt.exec("java -Xmx" + ram + " -Xms512M -jar " + "\"" + path + "\\" + type + "\" nogui");
long pid = pr.pid();
server.updatePid(pid);
System.out.println("Success");
} catch (IOException e) {
System.out.println(e);
}
} }
} }
} }
public static void readFile(String path) { /**
* Reads the first line of a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read.
* @return True if successfull. False otherwise.
*/
public static boolean readFile(String path) {
//We might need to change this to look for specific lines in the file to find vanilla and snapshot versions.
//The version should probably be returned. Empty or null on failiure.
try { try {
URL url = new URL(path); URL url = new URL(path);
Scanner s = new Scanner(url.openStream()); Scanner s = new Scanner(url.openStream());
System.out.println(s.next()); System.out.println(s.next());
} catch (java.io.IOException e) { return true;
} catch (IOException e) {
System.out.println(e); System.out.println(e);
return false;
}
}
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to.
* @return True if successful. False otherwise.
*/
public static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL("https://knarcraft.net/Api/View/Jawns/Jawns%20instructions.txt");
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
System.out.println(e);
return false;
} }
} }
} }

View File

@ -1,6 +1,7 @@
import java.util.ArrayList; import java.util.ArrayList;
//Contains all necessary information to create, run and manage a Minecraft server.
public class Server { public class Server {
private static String[17] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"}; private static String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"};
private String name; private String name;
private String path; private String path;
private boolean enabled; private boolean enabled;
@ -8,15 +9,17 @@ public class Server {
private ServerType type; private ServerType type;
private String serverVersion; private String serverVersion;
private String maxRam; private String maxRam;
private long pid;
public Server(String name) { public Server(String name) {
this.name = name; this.name = name;
this.path = ""; this.path = "";
this.isEnabled = false; this.enabled = false;
this.playerList = new ArrayList<String>(); this.playerList = new ArrayList<String>();
this.type = null; this.type = null;
this.serverVersion = null; this.serverVersion = null;
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.pid = -1;
} }
public void addPlayer(String name) { public void addPlayer(String name) {
@ -37,7 +40,7 @@ public class Server {
} }
public String getType() { public String getType() {
return this.type.getName() + this.version; return this.type.getName() + this.serverVersion;
} }
public boolean isEnabled() { public boolean isEnabled() {
@ -47,4 +50,8 @@ public class Server {
public String maxRam() { public String maxRam() {
return this.maxRam; return this.maxRam;
} }
public void updatePid(long pid) {
this.pid = pid;
}
} }

View File

@ -1,3 +1,7 @@
import java.util.ArrayList;
/**
* Has a name and contains a list of valid server versions.
*/
public class ServerType { public class ServerType {
private String name; private String name;
private String[] versions; private String[] versions;
@ -11,7 +15,7 @@ public class ServerType {
return this.name; return this.name;
} }
public ArrayList<String> getVersions() { public String[] getVersions() {
return this.versions; return this.versions;
} }
} }