Fixes downloading of jar files
Links to knarcraft.net updated. Code for parsing the new minecraft vanilla json files added. Adds auto updates
This commit is contained in:
@ -1,5 +1,9 @@
|
||||
package net.knarcraft.serverlauncher;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.knarcraft.serverlauncher.profile.Collection;
|
||||
import net.knarcraft.serverlauncher.profile.Profile;
|
||||
import net.knarcraft.serverlauncher.server.Server;
|
||||
@ -7,13 +11,17 @@ import net.knarcraft.serverlauncher.server.ServerType;
|
||||
import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static net.knarcraft.serverlauncher.Shared.downloadFile;
|
||||
import static net.knarcraft.serverlauncher.Shared.readFile;
|
||||
import static net.knarcraft.serverlauncher.Shared.stringBetween;
|
||||
//Java 8 required.
|
||||
|
||||
@ -26,56 +34,65 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween;
|
||||
*/
|
||||
|
||||
public class Main {
|
||||
@SuppressWarnings("CanBeFinal")
|
||||
private static String appDir;
|
||||
private static boolean running = false;
|
||||
private static final String updateChannel = "alpha";
|
||||
private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher";
|
||||
|
||||
static {
|
||||
try {
|
||||
appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile());
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
public static void main(String[] args) throws IOException {
|
||||
checkForUpdate();
|
||||
try (PrintWriter file = new PrintWriter(Main.getAppDir() + File.separator + "latestrun.log")) {
|
||||
file.print("");
|
||||
} catch (IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
setup();
|
||||
new ServerConsoles();
|
||||
Profile.load();
|
||||
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
||||
exec.scheduleAtFixedRate(Main::updateConsoles, 10, 500, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
setup();
|
||||
new ServerConsoles();
|
||||
Profile.load();
|
||||
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
||||
exec.scheduleAtFixedRate(Main::updateConsoles, 10, 500, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Loads all server types, so that they are ready for use
|
||||
*/
|
||||
private static void setup() {
|
||||
try {
|
||||
ServerType.loadServerTypes();
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setup() {
|
||||
try {
|
||||
ServerType.loadServerTypes();
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAppDir() {
|
||||
/**
|
||||
* Retrieves the directory the .jar file is running from
|
||||
*
|
||||
* @return A string path
|
||||
*/
|
||||
public static String getAppDir() {
|
||||
if (appDir == null) {
|
||||
try {
|
||||
appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile());
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
return appDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from server processes, and writes the output to consoles.
|
||||
*/
|
||||
private static void updateConsoles() {
|
||||
try {
|
||||
private static void updateConsoles() {
|
||||
try {
|
||||
for (Collection collection : Profile.getCurrent().getCollections()) {
|
||||
Server server = collection.getServer();
|
||||
if (server.isEnabled() && server.getProcess() != null) {
|
||||
@ -102,7 +119,7 @@ public class Main {
|
||||
}
|
||||
running = runningNew;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,7 +129,7 @@ public class Main {
|
||||
* @return Is at least one server running?
|
||||
*/
|
||||
private static boolean serversRunning() {
|
||||
int num = 0;
|
||||
int num = 0;
|
||||
for (Collection collection : Profile.getCurrent().getCollections()) {
|
||||
if (collection.getServer().isStarted() || (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) {
|
||||
num++;
|
||||
@ -127,11 +144,11 @@ public class Main {
|
||||
* @param text The text to search.
|
||||
* @param server The server which sent the text.
|
||||
*/
|
||||
private static void updatePlayerList(String text, Server server) {
|
||||
if (!server.getTypeName().equals("Bungee")) {
|
||||
String joinedPlayer = getPlayer(text, true);
|
||||
String leftPlayer = getPlayer(text, false);
|
||||
if (!joinedPlayer.equals("")) {
|
||||
private static void updatePlayerList(String text, Server server) {
|
||||
if (!server.getTypeName().equals("Bungee")) {
|
||||
String joinedPlayer = getPlayer(text, true);
|
||||
String leftPlayer = getPlayer(text, false);
|
||||
if (!joinedPlayer.equals("")) {
|
||||
if (!server.hasPlayer(joinedPlayer)) {
|
||||
server.addPlayer(joinedPlayer);
|
||||
}
|
||||
@ -152,7 +169,7 @@ public class Main {
|
||||
*/
|
||||
private static String getPlayer(String text, boolean joined) {
|
||||
String playerName;
|
||||
if (joined) {
|
||||
if (joined) {
|
||||
playerName = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
|
||||
if (playerName.equals("")) {
|
||||
playerName = stringBetween(text, "UUID of player ", " is ");
|
||||
@ -165,4 +182,84 @@ public class Main {
|
||||
}
|
||||
return playerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a newer version is available
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private static void checkForUpdate() throws IOException {
|
||||
Scanner file;
|
||||
try {
|
||||
file = new Scanner(new File("config/currentversion.csv"));
|
||||
} catch (FileNotFoundException e) {
|
||||
file = new Scanner(Main.class.getResourceAsStream("/config/currentversion.csv"));
|
||||
}
|
||||
|
||||
String oldType = file.nextLine();
|
||||
String oldVer = file.nextLine();
|
||||
|
||||
String data = readFile(updateURL);
|
||||
JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
|
||||
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
|
||||
|
||||
if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) {
|
||||
String dir = getAppDir() + File.separator;
|
||||
JsonArray versionList = jsonObject.getAsJsonArray("versions");
|
||||
String url = "";
|
||||
for (JsonElement elem : versionList) {
|
||||
JsonObject obj = elem.getAsJsonObject();
|
||||
String ver = obj.get("id").getAsString();
|
||||
String type = obj.get("type").getAsString();
|
||||
if (ver.equals(latest) && type.equals(updateChannel)) {
|
||||
url = obj.get("url").getAsString();
|
||||
}
|
||||
}
|
||||
if (downloadFile(url, new File(dir + "update.jar").toPath())) {
|
||||
doUpdate(dir);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"An update is available, but could not be downloaded.",
|
||||
"Update available",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void doUpdate(String dir) {
|
||||
int answer = JOptionPane.showConfirmDialog(
|
||||
null,
|
||||
"An update is available. Do you want to update?",
|
||||
"Update available",
|
||||
JOptionPane.YES_NO_OPTION
|
||||
);
|
||||
if (answer == JOptionPane.YES_NO_OPTION) {
|
||||
if (new File (dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir + "Old.jar"))) {
|
||||
if (new File(dir + "update.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) {
|
||||
if (new File(dir + "Old.jar").delete()) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Update finished. Please run the software again.",
|
||||
"Update complete",
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
System.exit(0);
|
||||
}
|
||||
} else {
|
||||
if (!new File(dir + "Old.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) {
|
||||
System.out.println("Shit");
|
||||
}
|
||||
}
|
||||
}
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Could not replace the main .jar with the downloaded .jar.",
|
||||
"Update failed",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -521,10 +521,16 @@ public class Profile {
|
||||
downloadAll();
|
||||
printToGui("Finished downloading jars");
|
||||
} catch (FileNotFoundException e) {
|
||||
printToGui("One or more downloads failed: " + e.getMessage());
|
||||
throw new FileNotFoundException("One or more downloads failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints something to the gui status field if the gui exists
|
||||
* Otherwise it prints to the console
|
||||
*
|
||||
* @param str The string to show the user
|
||||
*/
|
||||
private static void printToGui(String str) {
|
||||
if (gui != null) {
|
||||
gui.setStatus(str);
|
||||
|
@ -552,7 +552,7 @@ public class GUI implements ActionListener {
|
||||
} else if (e.getSource() == chckbxmntmDownloadJars) {
|
||||
downloadJars();
|
||||
} else if (e.getSource() == mntmErrors) {
|
||||
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Info/");
|
||||
goToURL("https://archive.knarcraft.net/BungeeMinecraftServerLauncherInfo/");
|
||||
} else if (e.getSource() == mntmSetup) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
@ -561,7 +561,7 @@ public class GUI implements ActionListener {
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
} else if (e.getSource() == mntmManualUpdate) {
|
||||
goToURL("https://knarcraft.net/Downloads/Bungeeminecraftserverlauncher/");
|
||||
goToURL("https://git.knarcraft.net/EpicKnarvik97/Minecraft-Server-Launcher");
|
||||
} else if (e.getSource() == mntmRunInBackground) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
@ -591,7 +591,7 @@ public class GUI implements ActionListener {
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
} else if (e.getSource() == mntmStory) {
|
||||
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/");
|
||||
goToURL("https://archive.knarcraft.net/BungeeminecraftserverlauncherStory/");
|
||||
} else if (e.getSource() == btnStartServer) {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
|
Reference in New Issue
Block a user