From d4c105534b358980856990a3d3d8bdb1096e74c1 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 1 Feb 2023 16:37:36 +0000 Subject: [PATCH 1/3] Added hex support and started rewriting formatting --- .../updatechecker/UpdateCheckerMessages.java | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java index 35a29a4..b2411e4 100644 --- a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java +++ b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java @@ -32,9 +32,12 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Stream; class UpdateCheckerMessages { + private static final Pattern hexPattern = Pattern.compile("&#[a-fA-F0-9]{6}"); @NotNull private static TextComponent createLink(@NotNull final String text, @NotNull final String link) { @@ -64,16 +67,16 @@ protected static void printCheckResultToConsole(UpdateCheckEvent event) { if (event.getResult() == UpdateCheckResult.RUNNING_LATEST_VERSION) { if (UpdateChecker.getInstance().isSuppressUpToDateMessage()) return; - plugin.getLogger().info(String.format("You are using the latest version of %s.", plugin.getName())); + plugin.getLogger().info(parsePlaceholders( "You are using the latest version of %plugin-name%.", instance)); return; } List lines = new ArrayList<>(); - lines.add(String.format("There is a new version of %s available!", plugin.getName())); + lines.add(parsePlaceholders("There is a new version of %plugin-name% available!", instance)); lines.add(" "); - lines.add(String.format("Your version: %s%s", instance.isColoredConsoleOutput() ? ChatColor.RED : "", event.getUsedVersion())); - lines.add(String.format("Latest version: %s%s", instance.isColoredConsoleOutput() ? ChatColor.GREEN : "", event.getLatestVersion())); + lines.add(parsePlaceholders("Your version: &c%current-version%", instance)); + lines.add(parsePlaceholders("Latest version: &a%latest-version%", instance)); List downloadLinks = instance.getAppropriateDownloadLinks(); @@ -85,10 +88,10 @@ protected static void printCheckResultToConsole(UpdateCheckEvent event) { lines.add("Download:"); lines.add(" " + downloadLinks.get(0)); } else if (downloadLinks.size() == 2) { - lines.add(String.format("Download (%s)", instance.getNamePaidVersion())); + lines.add(parsePlaceholders("Download (%resource-paid-name%)", instance)); lines.add(" " + downloadLinks.get(0)); lines.add(" "); - lines.add(String.format("Download (%s)", instance.getNameFreeVersion())); + lines.add(parsePlaceholders("Download (%resource-free-name%)", instance)); lines.add(" " + downloadLinks.get(1)); } } @@ -99,15 +102,15 @@ protected static void printCheckResultToConsole(UpdateCheckEvent event) { protected static void printCheckResultToPlayer(Player player, boolean showMessageWhenLatestVersion) { UpdateChecker instance = UpdateChecker.getInstance(); if (instance.getLastCheckResult() == UpdateCheckResult.NEW_VERSION_AVAILABLE) { - player.sendMessage(ChatColor.GRAY + "There is a new version of " + ChatColor.GOLD + instance.getPlugin().getName() + ChatColor.GRAY + " available."); + player.sendMessage(parsePlaceholders("&7There is a new version of &6%plugin-name% &7available.", instance)); sendLinks(player); - player.sendMessage(ChatColor.DARK_GRAY + "Latest version: " + ChatColor.GREEN + instance.getLatestVersion() + ChatColor.DARK_GRAY + " | Your version: " + ChatColor.RED + instance.getUsedVersion()); + player.sendMessage(parsePlaceholders("&8Latest version: &a%latest-version% &8| Your version: &c%current-version%", instance)); player.sendMessage(""); } else if (instance.getLastCheckResult() == UpdateCheckResult.UNKNOWN) { - player.sendMessage(ChatColor.GOLD + instance.getPlugin().getName() + ChatColor.RED + " could not check for updates."); + player.sendMessage(parsePlaceholders("&6%plugin-name% &ccould not check for updates.", instance)); } else { if (showMessageWhenLatestVersion) { - player.sendMessage(ChatColor.GREEN + "You are running the latest version of " + ChatColor.GOLD + instance.getPlugin().getName()); + player.sendMessage(parsePlaceholders("&aYou are running the latest version of &6%plugin-name%", instance)); } } } @@ -172,4 +175,44 @@ private static void sendLinks(@NotNull final Player... players) { player.spigot().sendMessage(text); } } + + private static String parsePlaceholders(String string, UpdateChecker instance) { + List downloadLinks = instance.getAppropriateDownloadLinks(); + + String changelogLink = instance.getChangelogLink(); + if (changelogLink == null) changelogLink = ""; + + String donationLink = instance.getDonationLink(); + if (donationLink == null) donationLink = ""; + + String supportLink = instance.getSupportLink(); + if (supportLink == null) supportLink = ""; + + string = translateHexColorCodes(string) + .replaceAll("%latest-version%", instance.getUsedVersion()) + .replaceAll("%current-version%", instance.getLatestVersion()) + .replaceAll("%resource-name%", instance.getNameFreeVersion()) + .replaceAll("%resource-link%", downloadLinks.get(1)) + .replaceAll("%resource-paid-name%", instance.getNamePaidVersion()) + .replaceAll("%resource-paid-link%", downloadLinks.get(0)) + .replaceAll("%changelog-link%", changelogLink) + .replaceAll("%donation-link%", donationLink) + .replaceAll("%support-link%", supportLink) + .replaceAll("%plugin-name%", instance.getPlugin().getName()); + + return string; + } + + private static String translateHexColorCodes(String string) { + string = string.replaceAll("ยง", "&"); + + // Parse message through Default Hex in format "&#rrggbb" + Matcher match = hexPattern.matcher(string); + while (match.find()) { + String color = string.substring(match.start() + 1, match.end()); + string = string.replace("&" + color, net.md_5.bungee.api.ChatColor.of(color) + ""); + match = hexPattern.matcher(string); + } + return net.md_5.bungee.api.ChatColor.translateAlternateColorCodes('&', string); + } } From 31ecf0bfdc22b49d5acb6abb1085f5910aeaf3bd Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 1 Feb 2023 21:42:15 +0000 Subject: [PATCH 2/3] Finshed rewriting format to allow for hex colors --- .../jeff_media/updatechecker/UpdateCheckerMessages.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java index b2411e4..aaf2612 100644 --- a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java +++ b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java @@ -142,8 +142,8 @@ private static void sendLinks(@NotNull final Player... players) { List downloadLinks = instance.getAppropriateDownloadLinks(); if (downloadLinks.size() == 2) { - links.add(createLink(String.format("Download (%s)", instance.getNamePaidVersion()), downloadLinks.get(0))); - links.add(createLink(String.format("Download (%s)", instance.getNameFreeVersion()), downloadLinks.get(1))); + links.add(createLink(parsePlaceholders("Download (%resource-paid-name%)", instance), downloadLinks.get(0))); + links.add(createLink(parsePlaceholders("Download (%resource-name%)", instance), downloadLinks.get(1))); } else if (downloadLinks.size() == 1) { links.add(createLink("Download", downloadLinks.get(0))); } @@ -157,8 +157,7 @@ private static void sendLinks(@NotNull final Player... players) { links.add(createLink("Support", instance.getSupportLink())); } - final TextComponent placeholder = new TextComponent(" | "); - placeholder.setColor(net.md_5.bungee.api.ChatColor.GRAY); + final TextComponent placeholder = new TextComponent(translateHexColorCodes(" &7| ")); TextComponent text = new TextComponent(""); From 3654d6298117a2362fb013477d2a9172ca72dc14 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 1 Feb 2023 22:52:45 +0000 Subject: [PATCH 3/3] Added configuration to messages --- .../updatechecker/UpdateChecker.java | 98 +++++++++++++++++++ .../updatechecker/UpdateCheckerMessages.java | 83 +++++++++++----- 2 files changed, 159 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java b/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java index 8bc9d1c..c696b69 100644 --- a/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java +++ b/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java @@ -888,4 +888,102 @@ public UpdateChecker setUserAgent(@Nullable String userAgent) { return this; } + /** + * Sets the Button Format string using plain text + * + * @param format message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setButtonFormat(@NotNull String format) { + UpdateCheckerMessages.setButtonFormat(format); + return this; + } + + /** + * Sets the Button Link Format string using plain text + * + * @param format message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setButtonLinkFormat(@NotNull String format) { + UpdateCheckerMessages.setButtonLinkFormat(format); + return this; + } + + /** + * Sets the Console's "using latest" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setConsoleUsingLatestMsg(@NotNull String message) { + UpdateCheckerMessages.setConsoleUsingLatestMsg(message); + return this; + } + + /** + * Sets the Console's "new version" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setConsoleNewVersionMsg(@NotNull String message) { + UpdateCheckerMessages.setConsoleNewVersionMsg(message); + return this; + } + + /** + * Sets whether the fancy console border is added + * + * @param border whether the border is enabled + * @return UpdateChecker instance being ran + */ + public UpdateChecker setConsoleBorder(boolean border) { + UpdateCheckerMessages.setConsoleBorder(border); + return this; + } + + /** + * Sets the ingame "using latest" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setIngameUsingLatestMsg(@NotNull String message) { + UpdateCheckerMessages.setIngameUsingLatestMsg(message); + return this; + } + + /** + * Sets the ingame "using latest" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setIngameNewVersionMsg(@NotNull String message) { + UpdateCheckerMessages.setIngameNewVersionMsg(message); + return this; + } + + /** + * Sets the ingame "version changes" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setIngameVersionChangesMsg(@NotNull String message) { + UpdateCheckerMessages.setIngameVersionChangesMsg(message); + return this; + } + + /** + * Sets the ingame "couldn't check for updates" message string using plain text + * + * @param message message format string + * @return UpdateChecker instance being ran + */ + public UpdateChecker setCouldNotCheckForUpdatesMsg(@NotNull String message) { + UpdateCheckerMessages.setCouldNotCheckForUpdatesMsg(message); + return this; + } } diff --git a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java index aaf2612..a463a16 100644 --- a/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java +++ b/src/main/java/com/jeff_media/updatechecker/UpdateCheckerMessages.java @@ -22,7 +22,6 @@ import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; @@ -39,16 +38,24 @@ class UpdateCheckerMessages { private static final Pattern hexPattern = Pattern.compile("&#[a-fA-F0-9]{6}"); + private static String buttonFormat = "&6&l%button-name%"; + private static String buttonLinkFormat = "&lLink: &r%link%"; + + // Console Messages + private static String consoleUsingLatestMsg = "You are using the latest version of %plugin-name%."; + private static String consoleNewVersionMsg = "There is a new version of %plugin-name% available!\n\nYour version: &c%current-version%\nLatest version: &a%latest-version%\nPlease update to the newest version.\n"; + private static boolean consoleBorder = true; + + // In-game Messages + private static String ingameUsingLatestMsg = "&aYou are running the latest version of &6%plugin-name%"; + private static String ingameNewVersionMsg = "&7There is a new version of &6%plugin-name% &7available."; + private static String ingameVersionChangesMsg = "&8Latest version: &a%latest-version% &8| Your version: &c%current-version%"; + private static String couldNotCheckForUpdatesMsg = "&6%plugin-name% &ccould not check for updates."; + @NotNull private static TextComponent createLink(@NotNull final String text, @NotNull final String link) { - final ComponentBuilder lore = new ComponentBuilder("Link: ") - .bold(true) - .append(link) - .bold(false); - final TextComponent component = new TextComponent(text); - component.setBold(true); - // TODO: Make color configurable - component.setColor(net.md_5.bungee.api.ChatColor.GOLD); + final ComponentBuilder lore = new ComponentBuilder(buttonLinkFormat.replaceAll("%link%", link)); + final TextComponent component = new TextComponent(buttonFormat.replaceAll("%button-name%", text)); component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link)); //noinspection deprecation component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, lore.create())); @@ -67,23 +74,19 @@ protected static void printCheckResultToConsole(UpdateCheckEvent event) { if (event.getResult() == UpdateCheckResult.RUNNING_LATEST_VERSION) { if (UpdateChecker.getInstance().isSuppressUpToDateMessage()) return; - plugin.getLogger().info(parsePlaceholders( "You are using the latest version of %plugin-name%.", instance)); + plugin.getLogger().info(parsePlaceholders(consoleUsingLatestMsg, instance)); return; } List lines = new ArrayList<>(); - lines.add(parsePlaceholders("There is a new version of %plugin-name% available!", instance)); - lines.add(" "); - lines.add(parsePlaceholders("Your version: &c%current-version%", instance)); - lines.add(parsePlaceholders("Latest version: &a%latest-version%", instance)); + for (String line : consoleNewVersionMsg.split("\n")) { + lines.add(parsePlaceholders(line, instance)); + } List downloadLinks = instance.getAppropriateDownloadLinks(); if (downloadLinks.size() > 0) { - lines.add(" "); - lines.add("Please update to the newest version."); - lines.add(" "); if (downloadLinks.size() == 1) { lines.add("Download:"); lines.add(" " + downloadLinks.get(0)); @@ -96,21 +99,22 @@ protected static void printCheckResultToConsole(UpdateCheckEvent event) { } } - printNiceBoxToConsole(plugin.getLogger(), lines); + if (consoleBorder) printNiceBoxToConsole(plugin.getLogger(), lines); + else lines.forEach(line -> plugin.getLogger().log(Level.WARNING, line)); } protected static void printCheckResultToPlayer(Player player, boolean showMessageWhenLatestVersion) { UpdateChecker instance = UpdateChecker.getInstance(); if (instance.getLastCheckResult() == UpdateCheckResult.NEW_VERSION_AVAILABLE) { - player.sendMessage(parsePlaceholders("&7There is a new version of &6%plugin-name% &7available.", instance)); + player.sendMessage(parsePlaceholders(ingameNewVersionMsg, instance)); sendLinks(player); - player.sendMessage(parsePlaceholders("&8Latest version: &a%latest-version% &8| Your version: &c%current-version%", instance)); + player.sendMessage(parsePlaceholders(ingameVersionChangesMsg, instance)); player.sendMessage(""); } else if (instance.getLastCheckResult() == UpdateCheckResult.UNKNOWN) { - player.sendMessage(parsePlaceholders("&6%plugin-name% &ccould not check for updates.", instance)); + player.sendMessage(parsePlaceholders(couldNotCheckForUpdatesMsg, instance)); } else { if (showMessageWhenLatestVersion) { - player.sendMessage(parsePlaceholders("&aYou are running the latest version of &6%plugin-name%", instance)); + player.sendMessage(parsePlaceholders(ingameUsingLatestMsg, instance)); } } } @@ -214,4 +218,39 @@ private static String translateHexColorCodes(String string) { } return net.md_5.bungee.api.ChatColor.translateAlternateColorCodes('&', string); } + + public static void setButtonFormat(String format) { + UpdateCheckerMessages.buttonFormat = format; + } + + public static void setButtonLinkFormat(String format) { + UpdateCheckerMessages.buttonLinkFormat = format; + } + + public static void setConsoleUsingLatestMsg(String message) { + UpdateCheckerMessages.consoleUsingLatestMsg = message; + } + public static void setConsoleNewVersionMsg(String message) { + UpdateCheckerMessages.consoleNewVersionMsg = message; + } + + public static void setConsoleBorder(boolean border) { + UpdateCheckerMessages.consoleBorder = border; + } + + public static void setIngameUsingLatestMsg(String message) { + UpdateCheckerMessages.ingameUsingLatestMsg = message; + } + + public static void setIngameNewVersionMsg(String message) { + UpdateCheckerMessages.ingameNewVersionMsg = message; + } + + public static void setIngameVersionChangesMsg(String message) { + UpdateCheckerMessages.ingameVersionChangesMsg = message; + } + + public static void setCouldNotCheckForUpdatesMsg(String message) { + UpdateCheckerMessages.couldNotCheckForUpdatesMsg = message; + } }