From 9abbb59131e5367d26755f598941c005cd3964c0 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 24 Aug 2022 13:23:05 +0100 Subject: [PATCH] Update Annotaml version, use DesertWell for about menus/update checking --- bukkit/build.gradle | 4 +- .../william278/huskhomes/BukkitHuskHomes.java | 15 +- common/build.gradle | 4 +- .../net/william278/huskhomes/HuskHomes.java | 2 +- .../huskhomes/command/HuskHomesCommand.java | 14 +- .../william278/huskhomes/util/AboutMenu.java | 212 ------------------ .../huskhomes/util/UpdateChecker.java | 57 ----- .../william278/huskhomes/util/Version.java | 71 ------ 8 files changed, 30 insertions(+), 349 deletions(-) delete mode 100644 common/src/main/java/net/william278/huskhomes/util/AboutMenu.java delete mode 100644 common/src/main/java/net/william278/huskhomes/util/UpdateChecker.java delete mode 100644 common/src/main/java/net/william278/huskhomes/util/Version.java diff --git a/bukkit/build.gradle b/bukkit/build.gradle index ad02943d..5698c94a 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -10,7 +10,8 @@ dependencies { compileOnly 'com.google.guava:guava:31.1-jre' compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT' compileOnly 'org.jetbrains:annotations:23.0.0' - compileOnly 'net.william278:Annotaml:1.0.1' + compileOnly 'net.william278:Annotaml:1.0.2' + compileOnly 'net.william278:DesertWell:1.0' compileOnly "com.github.MilkBowl:VaultAPI:1.7.1" testImplementation 'com.github.seeseemelk:MockBukkit-v1.19:2.117.1' @@ -29,6 +30,7 @@ shadowJar { relocate 'com.zaxxer', 'net.william278.huskhomes.libraries' relocate 'net.william278.annotaml', 'net.william278.huskhomes.libraries.annotaml' relocate 'net.william278.paginedown', 'net.william278.huskhomes.libraries.paginedown' + relocate 'net.william278.desertwell', 'net.william278.huskhomes.libraries.desertwell' relocate 'org.yaml.snakeyaml', 'net.william278.huskhomes.libraries.snakeyaml' relocate 'com.google.gson', 'net.william278.huskhomes.libraries.gson' diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 6578cebf..305c936c 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -26,6 +26,8 @@ import net.william278.huskhomes.random.RtpEngine; import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.*; +import net.william278.desertwell.Version; +import net.william278.desertwell.UpdateChecker; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -205,7 +207,14 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { // Check for updates if (settings.checkForUpdates) { getLoggingAdapter().log(Level.INFO, "Checking for updates..."); - CompletableFuture.runAsync(() -> new UpdateChecker(getPluginVersion(), getLoggingAdapter()).logToConsole()); + final UpdateChecker updateChecker = UpdateChecker.create(getPluginVersion(), 83767); + updateChecker.isUpToDate().thenAccept(isUpToDate -> { + if (!isUpToDate) { + getLoggingAdapter().log(Level.WARNING, "An update is available for HuskHomes, v" + + updateChecker.getLatestVersion().join() + + " (Currently running v" + getPluginVersion() + ")"); + } + }); } } catch (HuskHomesInitializationException exception) { getLoggingAdapter().log(Level.SEVERE, exception.getMessage()); @@ -339,12 +348,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { @Override public @NotNull Version getPluginVersion() { - return Version.pluginVersion(getDescription().getVersion()); + return Version.fromString(getDescription().getVersion()); } @Override public @NotNull Version getMinecraftVersion() { - return Version.minecraftVersion(Bukkit.getBukkitVersion()); + return Version.fromMinecraftVersionString(Bukkit.getBukkitVersion()); } @Override diff --git a/common/build.gradle b/common/build.gradle index 8f5c6b22..55b388be 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -2,8 +2,9 @@ dependencies { implementation 'commons-io:commons-io:2.11.0' implementation 'de.themoep:minedown:1.7.1-SNAPSHOT' implementation 'com.google.code.gson:gson:2.9.0' - implementation 'net.william278:Annotaml:1.0.1' + implementation 'net.william278:Annotaml:1.0.2' implementation 'net.william278:PagineDown:1.0.3' + implementation 'net.william278:DesertWell:1.0' implementation('com.zaxxer:HikariCP:5.0.1') { exclude module: 'slf4j-api' } @@ -26,6 +27,7 @@ shadowJar { relocate 'com.zaxxer', 'net.william278.huskhomes.libraries' relocate 'net.william278.annotaml', 'net.william278.huskhomes.libraries.annotaml' relocate 'net.william278.paginedown', 'net.william278.huskhomes.libraries.paginedown' + relocate 'net.william278.desertwell', 'net.william278.huskhomes.libraries.desertwell' relocate 'org.yaml.snakeyaml', 'net.william278.huskhomes.libraries.snakeyaml' relocate 'com.google.gson', 'net.william278.huskhomes.libraries.gson' } \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index c25cfc47..602b8491 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -14,7 +14,7 @@ import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.Logger; import net.william278.huskhomes.util.Permission; -import net.william278.huskhomes.util.Version; +import net.william278.desertwell.Version; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java index d0d4ae0a..a5299d13 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java @@ -2,7 +2,7 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.OnlineUser; -import net.william278.huskhomes.util.AboutMenu; +import net.william278.desertwell.AboutMenu; import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -28,6 +28,14 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, .addAttribution("Contributors", AboutMenu.Credit.of("imDaniX").withDescription("Code, refactoring"), AboutMenu.Credit.of("Log1x").withDescription("Code")) + .addAttribution("Translators", + AboutMenu.Credit.of("SnivyJ").withDescription("Simplified Chinese (zh-cn)"), + AboutMenu.Credit.of("TonyPak").withDescription("Traditional Chinese (zh-tw)"), + AboutMenu.Credit.of("Villag3r_").withDescription("Italian (it-it)"), + AboutMenu.Credit.of("ReferTV").withDescription("Polish (pl)"), + AboutMenu.Credit.of("anchelthe").withDescription("Spanish (es-es)"), + AboutMenu.Credit.of("Ceddix").withDescription("German, (de-de)"), + AboutMenu.Credit.of("Pukejoy_1").withDescription("Bulgarian (bg-bg)")) .addButtons( AboutMenu.Link.of("https://william278.net/docs/huskhomes").withText("Documentation").withIcon("⛏"), AboutMenu.Link.of("https://github.com/WiIIiam278/HuskHomes2/issues").withText("Issues").withIcon("❌").withColor("#ff9f0f"), @@ -57,12 +65,12 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, @Override public void onConsoleExecute(@NotNull String[] args) { if (args.length == 0) { - Arrays.stream(aboutMenu.toConsoleString().split("\n")).forEach(message -> + Arrays.stream(aboutMenu.toString().split("\n")).forEach(message -> plugin.getLoggingAdapter().log(Level.INFO, message)); return; } switch (args[0].toLowerCase()) { - case "about" -> Arrays.stream(aboutMenu.toConsoleString().split("\n")).forEach(message -> + case "about" -> Arrays.stream(aboutMenu.toString().split("\n")).forEach(message -> plugin.getLoggingAdapter().log(Level.INFO, message)); case "help" -> { diff --git a/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java b/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java deleted file mode 100644 index 8072184f..00000000 --- a/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java +++ /dev/null @@ -1,212 +0,0 @@ -package net.william278.huskhomes.util; - -import de.themoep.minedown.MineDown; -import net.william278.huskhomes.config.Locales; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.*; - -public class AboutMenu { - - @NotNull - private final String title; - - @Nullable - private Version version; - - @Nullable - private String description; - - @NotNull - private final Map> attributions; - - @NotNull - private final List buttons; - - private AboutMenu(@NotNull String title) { - this.title = title; - this.buttons = new ArrayList<>(); - this.attributions = new LinkedHashMap<>(); - } - - @NotNull - public static AboutMenu create(@NotNull String title) { - return new AboutMenu(title); - } - - @NotNull - public AboutMenu withDescription(@Nullable String description) { - this.description = description; - return this; - } - - @NotNull - public AboutMenu withVersion(@NotNull Version version) { - this.version = version; - return this; - } - - @NotNull - public AboutMenu addAttribution(@NotNull String category, @NotNull Credit... credits) { - final List creditList = new ArrayList<>(Arrays.asList(credits)); - attributions.putIfAbsent(category, new ArrayList<>()); - attributions.get(category).addAll(creditList); - return this; - } - - @NotNull - public AboutMenu addButtons(@NotNull Link... links) { - buttons.addAll(Arrays.asList(links)); - return this; - } - - @NotNull - public MineDown toMineDown() { - final StringJoiner menu = new StringJoiner("\n") - .add("[" + Locales.escapeMineDown(title) + "](#00fb9a bold)" - + (version != null ? " [| Version " + Locales.escapeMineDown(version.toString()) + "](#00fb9a)" : "")); - if (description != null) { - menu.add("[" + Locales.escapeMineDown(description) + "](gray)"); - } - - if (!attributions.isEmpty()) { - menu.add(""); - } - for (Map.Entry> entry : attributions.entrySet()) { - final StringJoiner creditJoiner = new StringJoiner(", "); - for (final Credit credit : entry.getValue()) { - creditJoiner.add("[" + credit.name + "](" + credit.color + - (credit.description != null ? " show_text=&7" + Locales.escapeMineDown(credit.description) : "") + - (credit.url != null ? " open_url=" + Locales.escapeMineDown(credit.url) : "") - + ")"); - } - - if (!creditJoiner.toString().isBlank()) { - menu.add("[• " + entry.getKey() + ":](white) " + creditJoiner); - } - } - - if (!buttons.isEmpty()) { - final StringJoiner buttonsJoiner = new StringJoiner(" "); - for (final Link link : buttons) { - buttonsJoiner.add("[[" + (link.icon != null ? link.icon + " " : "") - + Locales.escapeMineDown(link.text) + "…]](" + link.color + " " - + "show_text=&7Click to open link open_url=" + Locales.escapeMineDown(link.url) + ")"); - } - menu.add("").add("[Links:](gray) " + buttonsJoiner); - } - - return new MineDown(menu.toString()).replace(); - } - - @NotNull - public String toConsoleString() { - final StringJoiner menu = new StringJoiner("\n") - .add(title + (version != null ? " | Version " + version : "")); - if (description != null) { - menu.add(description); - } - - if (!attributions.entrySet().isEmpty()) { - menu.add("━━━━━━━━━━━━━━━━━━━━━━━━"); - } - for (final Map.Entry> entry : attributions.entrySet()) { - final StringJoiner creditJoiner = new StringJoiner(", "); - for (final Credit credit : entry.getValue()) { - creditJoiner.add(credit.name + (credit.description != null ? " (" + credit.description + ")" : "")); - } - - if (!creditJoiner.toString().isBlank()) { - menu.add("- " + entry.getKey() + ": " + creditJoiner); - } - } - - if (!buttons.isEmpty()) { - menu.add("━━━━━━━━━━━━━━━━━━━━━━━━"); - for (final Link link : buttons) { - menu.add("- " + link.text + ": " + link.url); - } - } - - return menu.toString(); - } - - public static class Link { - - @NotNull - private String text = "Link"; - - @NotNull - private String color = "#00fb9a"; - - @Nullable - private String icon; - - @NotNull - private final String url; - - private Link(@NotNull String url) { - this.url = url; - } - - public static Link of(@NotNull String url) { - return new Link(url); - } - - public Link withText(@NotNull String text) { - this.text = text; - return this; - } - - public Link withIcon(@NotNull String icon) { - this.icon = icon; - return this; - } - - public Link withColor(@NotNull String color) { - this.color = color; - return this; - } - - } - - public static class Credit { - @NotNull - private final String name; - - @Nullable - private String description; - - @Nullable - private String url; - - @NotNull - private String color = "gray"; - - private Credit(@NotNull String name) { - this.name = name; - } - - public static Credit of(@NotNull String name) { - return new Credit(name); - } - - public Credit withDescription(@Nullable String description) { - this.description = description; - return this; - } - - public Credit withUrl(@Nullable String url) { - this.url = url; - return this; - } - - public Credit withColor(@NotNull String color) { - this.color = color; - return this; - } - - } - -} diff --git a/common/src/main/java/net/william278/huskhomes/util/UpdateChecker.java b/common/src/main/java/net/william278/huskhomes/util/UpdateChecker.java deleted file mode 100644 index 383b976c..00000000 --- a/common/src/main/java/net/william278/huskhomes/util/UpdateChecker.java +++ /dev/null @@ -1,57 +0,0 @@ -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 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 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() + ")"); - } - }); - } -} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/util/Version.java b/common/src/main/java/net/william278/huskhomes/util/Version.java deleted file mode 100644 index f9a16f91..00000000 --- a/common/src/main/java/net/william278/huskhomes/util/Version.java +++ /dev/null @@ -1,71 +0,0 @@ -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 { - 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) : ""); - } - -}