2018-01-24 12:18:06 +01:00
|
|
|
package net.knarcraft.serverlauncher.server;
|
2018-01-23 23:49:48 +01:00
|
|
|
|
|
|
|
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;
|
2018-01-24 12:18:06 +01:00
|
|
|
/* Contains all necessary information to create, run and manage a Minecraft server. */
|
2018-01-23 23:49:48 +01:00
|
|
|
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<String> playerList;
|
2018-01-24 12:18:06 +01:00
|
|
|
private ServerType type;
|
2018-01-23 23:49:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|