Edit: fix UpdateChecker version check;

Add: version utils;
feat/data-edit-commands
Harvels X 3 years ago
parent 3c50245540
commit 28c14ed393
No known key found for this signature in database
GPG Key ID: 9637BCFE856BB8F3

@ -1,6 +1,7 @@
package me.william278.husksync.bukkit.util.nms; package me.william278.husksync.bukkit.util.nms;
import me.william278.husksync.util.ThrowSupplier; import me.william278.husksync.util.ThrowSupplier;
import me.william278.husksync.util.VersionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class MinecraftVersionUtils { public class MinecraftVersionUtils {
@ -8,27 +9,10 @@ public class MinecraftVersionUtils {
public final static String CRAFTBUKKIT_PACKAGE_PATH = Bukkit.getServer().getClass().getPackage().getName(); public final static String CRAFTBUKKIT_PACKAGE_PATH = Bukkit.getServer().getClass().getPackage().getName();
public final static String PACKAGE_VERSION = CRAFTBUKKIT_PACKAGE_PATH.split("\\.")[3]; public final static String PACKAGE_VERSION = CRAFTBUKKIT_PACKAGE_PATH.split("\\.")[3];
public final static String MINECRAFT_PACKAGE = compare("1.17") < 0 ? public final static VersionUtils.Version SERVER_VERSION
= VersionUtils.Version.of(Bukkit.getBukkitVersion().split("-")[0]);
public final static String MINECRAFT_PACKAGE = SERVER_VERSION.compareTo(VersionUtils.Version.of("1.17")) < 0 ?
"net.minecraft.server.".concat(PACKAGE_VERSION) : "net.minecraft.server"; "net.minecraft.server.".concat(PACKAGE_VERSION) : "net.minecraft.server";
public final static String SERVER_VERSION = Bukkit.getBukkitVersion().split("-")[0];
public static int compare(String version) {
if (version == null || SERVER_VERSION == null) return 1;
String[] as = SERVER_VERSION.split("\\.");
String[] bs = version.split("\\.");
int length = Math.max(as.length, bs.length);
for (int i = 0; i < length; i++) {
int a = i < as.length ? Integer.parseInt(as[i]) : 0;
int b = i < bs.length ? Integer.parseInt(bs[i]) : 0;
if (a < b) return -1;
if (a > b) return 1;
}
return 0;
}
public static Class<?> getBukkitClass(String path) { public static Class<?> getBukkitClass(String path) {
return ThrowSupplier.get(() -> Class.forName(CRAFTBUKKIT_PACKAGE_PATH.concat(".").concat(path))); return ThrowSupplier.get(() -> Class.forName(CRAFTBUKKIT_PACKAGE_PATH.concat(".").concat(path)));

@ -11,38 +11,35 @@ public abstract class UpdateChecker {
private final static int SPIGOT_PROJECT_ID = 97144; private final static int SPIGOT_PROJECT_ID = 97144;
private final String currentVersion; private final VersionUtils.Version currentVersion;
private String latestVersion; private VersionUtils.Version latestVersion;
public UpdateChecker(String currentVersion) { public UpdateChecker(String currentVersion) {
this.currentVersion = currentVersion; this.currentVersion = VersionUtils.Version.of(currentVersion);
try { try {
final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID); final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID);
URLConnection urlConnection = url.openConnection(); 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) { } catch (IOException e) {
log(Level.WARNING, "Failed to check for updates: An IOException occurred."); log(Level.WARNING, "Failed to check for updates: An IOException occurred.");
this.latestVersion = "Unknown"; this.latestVersion = new VersionUtils.Version();
} catch (Exception e) { } catch (Exception e) {
log(Level.WARNING, "Failed to check for updates: An exception occurred."); log(Level.WARNING, "Failed to check for updates: An exception occurred.");
this.latestVersion = "Unknown"; this.latestVersion = new VersionUtils.Version();
} }
} }
public boolean isUpToDate() { public boolean isUpToDate() {
if (latestVersion.equalsIgnoreCase("Unknown")) { return this.currentVersion.compareTo(latestVersion) >= 0;
return true;
}
return latestVersion.equals(currentVersion);
} }
public String getLatestVersion() { public String getLatestVersion() {
return latestVersion; return latestVersion.toString();
} }
public String getCurrentVersion() { public String getCurrentVersion() {
return currentVersion; return currentVersion.toString();
} }
public abstract void log(Level level, String message); 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();
}
}
}
Loading…
Cancel
Save