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:
@@ -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;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.william278.husksync;
|
||||
package me.william278.husksync.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -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 + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user