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:
Kristian Knarvik 2018-11-07 17:40:39 +01:00
parent a3a8e4f377
commit 21737b7549
9 changed files with 154 additions and 49 deletions

1
.gitignore vendored
View File

@ -14,7 +14,6 @@
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.zip

View File

@ -0,0 +1,2 @@
alpha
1.1.0
1 alpha
2 1.1.0

View File

@ -2,7 +2,7 @@ Vanilla;Latest,1.12.2,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
Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server.
SpongeVanilla;1.12.2,1.11.2,1.10.2,1.8.9;https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=;"version":";",;https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/;/spongevanilla-
Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar;
Spigot;1.12.2,1.11.2,1.10.2,1.9.4,1.9.4,1.8.8,1.7.10,1.6.4,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/
MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/
Craftbukkit;1.12.2,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;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/
Spigot;1.13.1,1.12.2,1.11.2,1.10.2,1.9.4,1.9.4,1.8.8,1.7.10,1.6.4,1.5.2,1.4.7;https://static.knarcraft.net/public/jars/
MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://static.knarcraft.net/public/jars/
Craftbukkit;1.13.1,1.12.2,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;https://static.knarcraft.net/public/jars/
Custom;;
Can't render this file because it contains an unexpected character in line 1 and column 156.

0
git Normal file
View File

BIN
lib/gson-2.8.5.jar Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
Manifest-Version: 1.0
Class-Path: gson-2.8.5.jar
Main-Class: net.knarcraft.serverlauncher.Main

View File

@ -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,25 +34,18 @@ 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();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
setup();
@ -58,6 +59,9 @@ public class Main {
});
}
/**
* Loads all server types, so that they are ready for use
*/
private static void setup() {
try {
ServerType.loadServerTypes();
@ -67,7 +71,20 @@ public class Main {
}
}
/**
* 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;
}
@ -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
);
}
}
}

View File

@ -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);

View File

@ -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();