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

Migrate to DesertWell for about menu, version checking

This commit is contained in:
William278
2022-09-10 19:52:34 +03:00
parent 61020e04d9
commit 59a0002c16
12 changed files with 101 additions and 213 deletions

View File

@@ -1,57 +0,0 @@
package net.william278.husksync.util;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class UpdateChecker {
private final static int SPIGOT_PROJECT_ID = 97144;
private final Logger logger;
private final Version currentVersion;
public UpdateChecker(@NotNull Version currentVersion, @NotNull Logger logger) {
this.currentVersion = currentVersion;
this.logger = logger;
}
public CompletableFuture<Version> fetchLatestVersion() {
return CompletableFuture.supplyAsync(() -> {
try {
final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID);
URLConnection urlConnection = url.openConnection();
return Version.pluginVersion(new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine());
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to fetch the latest plugin version", e);
}
return new Version();
});
}
public boolean isUpdateAvailable(@NotNull Version latestVersion) {
return latestVersion.compareTo(currentVersion) > 0;
}
public Version getCurrentVersion() {
return currentVersion;
}
public CompletableFuture<Boolean> isUpToDate() {
return fetchLatestVersion().thenApply(this::isUpdateAvailable);
}
public void logToConsole() {
fetchLatestVersion().thenAccept(latestVersion -> {
if (isUpdateAvailable(latestVersion)) {
logger.log(Level.WARNING, "A new version of HuskSync is available: v" + latestVersion);
} else {
logger.log(Level.INFO, "HuskSync is up-to-date! (Running: v" + getCurrentVersion().toString() + ")");
}
});
}
}

View File

@@ -1,71 +0,0 @@
package net.william278.husksync.util;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.regex.Pattern;
public class Version implements Comparable<Version> {
private final static String VERSION_SEPARATOR = ".";
private final static String MINECRAFT_META_SEPARATOR = "-";
private final static String PLUGIN_META_SEPARATOR = "-";
private int[] versions = new int[]{};
@NotNull
private String metadata = "";
@NotNull
private String metaSeparator = "";
protected Version() {
}
private Version(@NotNull String version, @NotNull String metaSeparator) {
this.parse(version, metaSeparator);
this.metaSeparator = metaSeparator;
}
@NotNull
public static Version pluginVersion(@NotNull String versionString) {
return new Version(versionString, PLUGIN_META_SEPARATOR);
}
@NotNull
public static Version minecraftVersion(@NotNull String versionString) {
return new Version(versionString, MINECRAFT_META_SEPARATOR);
}
private void parse(@NotNull String version, @NotNull String metaSeparator) {
int metaIndex = version.indexOf(metaSeparator);
if (metaIndex > 0) {
this.metadata = version.substring(metaIndex + 1);
version = version.substring(0, metaIndex);
}
String[] versions = version.split(Pattern.quote(VERSION_SEPARATOR));
this.versions = Arrays.stream(versions).mapToInt(Integer::parseInt).toArray();
}
@Override
public int compareTo(@NotNull Version other) {
int length = Math.max(this.versions.length, other.versions.length);
for (int i = 0; i < length; i++) {
int a = i < this.versions.length ? this.versions[i] : 0;
int b = i < other.versions.length ? other.versions[i] : 0;
if (a < b) return -1;
if (a > b) return 1;
}
return 0;
}
@Override
public String toString() {
final StringJoiner joiner = new StringJoiner(VERSION_SEPARATOR);
for (int version : this.versions) {
joiner.add(String.valueOf(version));
}
return joiner + ((!this.metadata.isEmpty()) ? (this.metaSeparator + this.metadata) : "");
}
}