package net.knarcraft.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 static final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from. private static final String BUKKITURL = BASEURL + "/Bukkit/"; private static final String MCPCURL = BASEURL + "/MCPC+/"; private static final String SPIGOTURL = 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 AdvancedServerType 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.getType()).isFile()) { switch (this.type.getName()) { case "Custom": throw new FileNotFoundException("Specified custom jar was not found."); case "Spigot": staticJar(SPIGOTURL); break; case "Craftbukkit": staticJar(BUKKITURL); break; case "MCPCplus": staticJar(MCPCURL); break; case "Vanilla": if (this.serverVersion.equals("Latest")) { } else { } break; case "Sponge": staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType()); } //TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched. } } private boolean staticJar(String url) { return downloadFile(url + this.getType(), Paths.get(this.path + this.getType())); } private boolean staticJar(String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart, String outfile) { return downloadFile(versionURL + this.getType(), Paths.get(this.path + this.getType())); } /** * 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. */ private 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; } } private String stringBetween(String string, String start, String end) { return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end)); } }