Refactor bootstrap procedure, use enum for command loading

This commit is contained in:
William committed 2022-08-09 14:18:48 +01:00
1 parent eff21a16d5
commit ec1c0b744f
42 files changed
+982 -718

No files matched your search

@@ -1,20 +1,37 @@
package net.william278.huskhomes.util;
import de.themoep.minedown.MineDown;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
/**
* An abstract, cross-platform representation of a logger
*/
public interface Logger {
public abstract class Logger {
void log(Level level, String message, Exception e);
private boolean debug;
void log(Level level, String message);
public abstract void log(@NotNull Level level, @NotNull String message, @NotNull Exception e);
void info(String message);
public abstract void log(@NotNull Level level, @NotNull String message);
void severe(String message);
public abstract void log(@NotNull Level level, @NotNull MineDown mineDown);
void config(String message);
public abstract void info(@NotNull String message);
public abstract void severe(@NotNull String message);
public final void debug(@NotNull String message) {
if (debug) {
log(Level.INFO, "[DEBUG] " + message);
}
}
public abstract void config(@NotNull String message);
public final void showDebugLogs(boolean debug) {
this.debug = debug;
}
}
@@ -0,0 +1,57 @@
package net.william278.huskhomes.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 = 83767;
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 HuskHomes is available: v" + latestVersion);
} else {
logger.log(Level.INFO, "HuskHomes is up-to-date! (Running: v" + getCurrentVersion().toString() + ")");
}
});
}
}
@@ -0,0 +1,71 @@
package net.william278.huskhomes.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) : "");
}
}