mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2025-12-26 01:59:20 +00:00
Edit: fix UpdateChecker version check;
Add: version utils;
This commit is contained in:
@@ -11,38 +11,35 @@ public abstract class UpdateChecker {
|
||||
|
||||
private final static int SPIGOT_PROJECT_ID = 97144;
|
||||
|
||||
private final String currentVersion;
|
||||
private String latestVersion;
|
||||
private final VersionUtils.Version currentVersion;
|
||||
private VersionUtils.Version latestVersion;
|
||||
|
||||
public UpdateChecker(String currentVersion) {
|
||||
this.currentVersion = currentVersion;
|
||||
this.currentVersion = VersionUtils.Version.of(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();
|
||||
this.latestVersion = VersionUtils.Version.of(new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine());
|
||||
} catch (IOException e) {
|
||||
log(Level.WARNING, "Failed to check for updates: An IOException occurred.");
|
||||
this.latestVersion = "Unknown";
|
||||
this.latestVersion = new VersionUtils.Version();
|
||||
} catch (Exception e) {
|
||||
log(Level.WARNING, "Failed to check for updates: An exception occurred.");
|
||||
this.latestVersion = "Unknown";
|
||||
this.latestVersion = new VersionUtils.Version();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUpToDate() {
|
||||
if (latestVersion.equalsIgnoreCase("Unknown")) {
|
||||
return true;
|
||||
}
|
||||
return latestVersion.equals(currentVersion);
|
||||
return this.currentVersion.compareTo(latestVersion) >= 0;
|
||||
}
|
||||
|
||||
public String getLatestVersion() {
|
||||
return latestVersion;
|
||||
return latestVersion.toString();
|
||||
}
|
||||
|
||||
public String getCurrentVersion() {
|
||||
return currentVersion;
|
||||
return currentVersion.toString();
|
||||
}
|
||||
|
||||
public abstract void log(Level level, String message);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package me.william278.husksync.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class VersionUtils {
|
||||
|
||||
private final static char META_SEPARATOR = '+';
|
||||
private final static String VERSION_SEPARATOR = "\\.";
|
||||
|
||||
|
||||
public static class Version implements Comparable<Version> {
|
||||
public int[] versions = new int[]{};
|
||||
public String metadata = "";
|
||||
|
||||
public Version() {
|
||||
}
|
||||
|
||||
public Version(String version) {
|
||||
this.parse(version);
|
||||
}
|
||||
|
||||
public static Version of(String version) {
|
||||
return new Version(version);
|
||||
}
|
||||
|
||||
private void parse(String version) {
|
||||
int metaIndex = version.indexOf(META_SEPARATOR);
|
||||
if (metaIndex > 0) {
|
||||
this.metadata = version.substring(metaIndex + 1);
|
||||
version = version.substring(0, metaIndex);
|
||||
}
|
||||
String[] versions = version.split(VERSION_SEPARATOR);
|
||||
this.versions = Arrays.stream(versions).mapToInt(Integer::parseInt).toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Version version) {
|
||||
int length = Math.max(this.versions.length, version.versions.length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int a = i < this.versions.length ? this.versions[i] : 0;
|
||||
int b = i < version.versions.length ? version.versions[i] : 0;
|
||||
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder stringBuffer = new StringBuilder();
|
||||
for (int version : this.versions) {
|
||||
stringBuffer.append(version).append('.');
|
||||
}
|
||||
return stringBuffer.append(this.metadata).toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user