From 80a04c946afe79f85fed6430359b84bfbf2dd51f Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Tue, 23 Jan 2018 23:49:48 +0100 Subject: [PATCH] Moves project to intellij structure The project now has folders made to work under intellij. Packages are also a thing. --- .gitignore | 5 +- Server.java | 57 -------- src/serverlauncher/Main.java | 3 +- .../serverlauncher/server/Server.java | 124 ++++++++++++++++++ .../serverlauncher/server/ServerType.java | 2 + 5 files changed, 131 insertions(+), 60 deletions(-) delete mode 100644 Server.java create mode 100644 src/serverlauncher/serverlauncher/server/Server.java rename ServerType.java => src/serverlauncher/serverlauncher/server/ServerType.java (92%) diff --git a/.gitignore b/.gitignore index 336cd61..06d018b 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,7 @@ *.rar # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* \ No newline at end of file +hs_err_pid* + +*.xml +*.iml \ No newline at end of file diff --git a/Server.java b/Server.java deleted file mode 100644 index d616ddc..0000000 --- a/Server.java +++ /dev/null @@ -1,57 +0,0 @@ -import java.util.ArrayList; -//Contains all necessary information to create, run and manage a Minecraft server. -public class Server { - 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 path; - private boolean enabled; - private ArrayList playerList; - private ServerType type; - private String serverVersion; - private String maxRam; - private long pid; - - public Server(String name) { - this.name = name; - this.path = ""; - this.enabled = false; - this.playerList = new ArrayList(); - this.type = null; - this.serverVersion = null; - this.maxRam = ramList[0]; - this.pid = -1; - } - - public void addPlayer(String name) { - this.playerList.add(name); - } - - public void removePlayer(String name) { - for (int i = 0; i < playerList.size(); i++) { - if (name.equals(playerList.get(i))) { - playerList.remove(i); - return; - } - } - } - - public String getPath() { - return this.path; - } - - public String getType() { - return this.type.getName() + this.serverVersion; - } - - public boolean isEnabled() { - return this.enabled; - } - - public String maxRam() { - return this.maxRam; - } - - public void updatePid(long pid) { - this.pid = pid; - } -} \ No newline at end of file diff --git a/src/serverlauncher/Main.java b/src/serverlauncher/Main.java index 5b06498..72a78ce 100644 --- a/src/serverlauncher/Main.java +++ b/src/serverlauncher/Main.java @@ -1,5 +1,4 @@ -import serverlauncher.server.Server; -import serverlauncher.server.ServerType; +import serverlauncher.server.*; import java.io.FileNotFoundException; import java.lang.Runtime; diff --git a/src/serverlauncher/serverlauncher/server/Server.java b/src/serverlauncher/serverlauncher/server/Server.java new file mode 100644 index 0000000..8c11ca9 --- /dev/null +++ b/src/serverlauncher/serverlauncher/server/Server.java @@ -0,0 +1,124 @@ +package serverlauncher.server; + +import java.io.File; +import java.net.URL; +import java.util.Scanner; +import java.io.InputStream; +import java.io.IOException; +import java.nio.file.StandardCopyOption; +import java.nio.file.*; +import java.util.ArrayList; +import java.io.FileNotFoundException; +//Contains all necessary information to create, run and manage a Minecraft server. +public class Server { + private final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from. + private final String BUKKITURL = BASEURL + "/Bukkit/"; + private final String MCPCURL = BASEURL + "/MCPC+/"; + private final String SPIGOT = BASEURL + "/Spigot/"; + 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 path; + private boolean enabled; + private ArrayList playerList; + private serverlauncher.server.ServerType type; + private String serverVersion; + private String maxRam; + private long pid; + + public Server(String name) { + this.name = name; + this.path = ""; + this.enabled = false; + this.playerList = new ArrayList<>(); + this.type = null; + this.serverVersion = null; + this.maxRam = ramList[0]; + this.pid = -1; + } + + public void addPlayer(String name) { + this.playerList.add(name); + } + + public void removePlayer(String name) { + for (int i = 0; i < playerList.size(); i++) { + if (name.equals(playerList.get(i))) { + playerList.remove(i); + return; + } + } + } + + public String getPath() { + return this.path; + } + + public String getType() { + return this.type.getName() + this.serverVersion + ".jar"; + } + + public boolean isEnabled() { + return this.enabled; + } + + public String maxRam() { + return this.maxRam; + } + + public void updatePid(long pid) { + this.pid = pid; + } + + /** + * Downloads necessary .jar file for the server. + * All server versions are downloaded if missing. + * Newest server versions (snapshot, vanilla and bungee) need to be checked against an online json file and downloaded if outdated. + * Custom files must exist, or trigger a message. + */ + public void downloadJar() throws FileNotFoundException { + if (!new File(this.path + "\\" + this.type.getName() + this.serverVersion).isFile()) { + //TODO: Download a jar file based on version and type. Return true if the file was downloaded and ready to run. + throw new FileNotFoundException(""); + } + } + + /** + * 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 { + URL url = new URL(path); + Scanner s = new Scanner(url.openStream()); + System.out.println(s.next()); + return true; + } catch (IOException e) { + System.out.println("Error"); + 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("Error"); + return false; + } + } +} \ No newline at end of file diff --git a/ServerType.java b/src/serverlauncher/serverlauncher/server/ServerType.java similarity index 92% rename from ServerType.java rename to src/serverlauncher/serverlauncher/server/ServerType.java index bf62023..ffc6538 100644 --- a/ServerType.java +++ b/src/serverlauncher/serverlauncher/server/ServerType.java @@ -1,3 +1,5 @@ +package serverlauncher.server; + import java.util.ArrayList; /** * Has a name and contains a list of valid server versions.