mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +00:00
Update Annotaml version, use DesertWell for about menus/update checking
This commit is contained in:
1 parent
86c9251c37
commit
9abbb59131
8 files changed
+30
-349
No files matched your search
@@ -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;
|
||||
|
||||
|
||||
@@ -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" -> {
|
||||
|
||||
|
||||
@@ -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<String, List<Credit>> attributions;
|
||||
|
||||
@NotNull
|
||||
private final List<Link> 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<Credit> 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<String, List<Credit>> 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<String, List<Credit>> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<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() + ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<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) : "");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user