9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2026-01-06 15:41:56 +00:00

Add update checker

This commit is contained in:
William
2021-10-26 18:22:52 +01:00
parent f2b5df83c8
commit 4ca8f8a633
18 changed files with 175 additions and 24 deletions

View File

@@ -9,8 +9,8 @@ public class Settings {
* General settings
*/
// Messages language
public static String language;
// Whether to do automatic update checks on startup
public static boolean automaticUpdateChecks;
// The type of THIS server (Bungee or Bukkit)
public static ServerType serverType;
@@ -24,6 +24,9 @@ public class Settings {
* Bungee / Proxy server-only settings
*/
// Messages language
public static String language;
// SQL settings
public static DataStorageType dataStorageType;

View File

@@ -1,4 +1,4 @@
package me.william278.husksync;
package me.william278.husksync.util;
import java.util.HashMap;

View File

@@ -0,0 +1,56 @@
package me.william278.husksync.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
public abstract class UpdateChecker {
private final static int SPIGOT_PROJECT_ID = 97144;
private final String currentVersion;
private String latestVersion;
public UpdateChecker(String currentVersion) {
this.currentVersion = currentVersion;
try {
final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID);
URLConnection urlConnection = url.openConnection();
this.latestVersion = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine();
} catch (IOException e) {
log(Level.WARNING, "Failed to check for updates: An IOException occurred.");
this.latestVersion = "Unknown";
} catch (Exception e) {
log(Level.WARNING, "Failed to check for updates: An exception occurred.");
this.latestVersion = "Unknown";
}
}
public boolean isUpToDate() {
if (latestVersion.equalsIgnoreCase("Unknown")) {
return true;
}
return latestVersion.equals(currentVersion);
}
public String getLatestVersion() {
return latestVersion;
}
public String getCurrentVersion() {
return currentVersion;
}
public abstract void log(Level level, String message);
public void logToConsole() {
if (!isUpToDate()) {
log(Level.WARNING, "A new version of HuskSync is available: Version "
+ latestVersion + " (Currently running: " + currentVersion + ")");
}
}
}