From 5db9a601b1f575648818b38e1a355a1a328ad782 Mon Sep 17 00:00:00 2001 From: NotMyFault Date: Sun, 17 Oct 2021 09:55:25 +0200 Subject: [PATCH] feat: (Re-) Add update notifications (#1361) * feat: (Re-) Add update notifications Fixes #1348 * Move update notification to seperate class - Add a couple of linebreaks to the chat messages --- .../bukkit/util/UpdateNotification.java | 83 +++++++++++++++++++ .../worldedit/bukkit/WorldEditListener.java | 2 + .../worldedit/bukkit/WorldEditPlugin.java | 3 + .../fastasyncworldedit/core/FaweVersion.java | 13 ++- .../core/configuration/Settings.java | 2 + .../src/main/resources/lang/strings.json | 3 +- 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/UpdateNotification.java diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/UpdateNotification.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/UpdateNotification.java new file mode 100644 index 000000000..c366c2a5d --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/util/UpdateNotification.java @@ -0,0 +1,83 @@ +package com.fastasyncworldedit.bukkit.util; + +import com.fastasyncworldedit.core.Fawe; +import com.fastasyncworldedit.core.FaweVersion; +import com.fastasyncworldedit.core.configuration.Caption; +import com.fastasyncworldedit.core.configuration.Settings; +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.internal.util.LogManagerCompat; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; +import org.apache.logging.log4j.Logger; +import org.w3c.dom.Document; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.net.URL; + +/** + * @hidden + */ +public class UpdateNotification { + + private static final Logger LOGGER = LogManagerCompat.getLogger(); + + private static boolean hasUpdate; + private static String faweVersion = ""; + + /** + * Check whether a new build with a higher build number than the current build is available. + */ + public static void doUpdateCheck() { + if (Settings.IMP.ENABLED_COMPONENTS.UPDATE_NOTIFICATIONS) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(new URL("https://ci.athion.net/job/FastAsyncWorldEdit-1.17/api/xml/").openStream()); + faweVersion = doc.getElementsByTagName("lastSuccessfulBuild").item(0).getFirstChild().getTextContent(); + FaweVersion faweVersion = Fawe.get().getVersion(); + if (faweVersion.build == 0) { + LOGGER.warn("You are using a snapshot or a custom version of FAWE. This is not an official build distributed " + + "via https://www.spigotmc.org/resources/13932/"); + return; + } + if (faweVersion.build < Integer.parseInt(UpdateNotification.faweVersion)) { + hasUpdate = true; + int versionDifference = Integer.parseInt(UpdateNotification.faweVersion) - faweVersion.build; + LOGGER.warn( + "An update for FastAsyncWorldEdit is available. You are {} build(s) out of date.\nYou are running " + + "version {}, the latest version is {}-{}.\nUpdate at https://www.spigotmc.org/resources/13932/", + versionDifference, + faweVersion.toString(), + faweVersion.getSimpleVersionName(), + UpdateNotification.faweVersion + ); + } + } catch (Exception e) { + LOGGER.error("Unable to check for updates. Skipping."); + } + + } + } + + /** + * Trigger an update notification based on captions. Useful to notify server administrators ingame. + * + * @param player The player to notify. + */ + public static void doUpdateNotification(Player player) { + if (Settings.IMP.ENABLED_COMPONENTS.UPDATE_NOTIFICATIONS) { + if (player.hasPermission("fawe.admin") && UpdateNotification.hasUpdate) { + FaweVersion faweVersion = Fawe.get().getVersion(); + int versionDifference = Integer.parseInt(UpdateNotification.faweVersion) - faweVersion.build; + player.print(Caption.of("fawe.info.update-available", versionDifference, faweVersion.toString(), + faweVersion.getSimpleVersionName() + "-" + UpdateNotification.faweVersion, + TextComponent + .of("https://www.spigotmc.org/resources/13932/") + .clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/13932/")) + )); + } + } + } + +} diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java index 235a13ed1..654d444d4 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java @@ -21,6 +21,7 @@ package com.sk89q.worldedit.bukkit; +import com.fastasyncworldedit.bukkit.util.UpdateNotification; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.entity.Player; @@ -90,6 +91,7 @@ public class WorldEditListener implements Listener { if ((session = WorldEdit.getInstance().getSessionManager().getIfPresent(player)) != null) { session.loadDefaults(player, true); } + UpdateNotification.doUpdateNotification(player); } //FAWE end diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index e5e27a357..43a597f4d 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -21,6 +21,7 @@ package com.sk89q.worldedit.bukkit; import com.fastasyncworldedit.bukkit.BukkitPermissionAttachmentManager; import com.fastasyncworldedit.bukkit.FaweBukkit; +import com.fastasyncworldedit.bukkit.util.UpdateNotification; import com.fastasyncworldedit.core.Fawe; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -223,6 +224,8 @@ public class WorldEditPlugin extends JavaPlugin { ServerLib.checkJavaLTS(); // Check if we are in a safe environment ServerLib.checkUnsafeForks(); + // Check if a new build is available + UpdateNotification.doUpdateCheck(); } private void setupPreWorldData() { diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweVersion.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweVersion.java index 4593b13da..591000c59 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweVersion.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweVersion.java @@ -2,6 +2,8 @@ package com.fastasyncworldedit.core; /** * An internal FAWE class not meant for public use. + * + * @hidden **/ public class FaweVersion { @@ -46,12 +48,19 @@ public class FaweVersion { @Override public String toString() { if (hash == 0 && build == 0) { - return "FastAsyncWorldEdit-1.17-NoVer-SNAPSHOT"; + return getSimpleVersionName() + "-NoVer-SNAPSHOT"; } else { - return "FastAsyncWorldEdit-1.17" + build; + return getSimpleVersionName() + "-" + build; } } + /** + * @return The qualified version name + */ + public String getSimpleVersionName() { + return "FastAsyncWorldEdit-1.17"; + } + public boolean isNewer(FaweVersion other) { return other.build < this.build; } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java index 8fdc7f469..eca812058 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java @@ -92,6 +92,8 @@ public class Settings extends Config { @Comment({"Show additional information in console. It helps us at IntellectualSites to find out more about an issue.", "Leave it off if you don't need it, it can spam your console."}) public boolean DEBUG = false; + @Comment({"Whether or not FAWE should notify you on startup about new versions available."}) + public boolean UPDATE_NOTIFICATIONS = true; } diff --git a/worldedit-core/src/main/resources/lang/strings.json b/worldedit-core/src/main/resources/lang/strings.json index 2c48f95cd..e6d76068d 100644 --- a/worldedit-core/src/main/resources/lang/strings.json +++ b/worldedit-core/src/main/resources/lang/strings.json @@ -15,7 +15,8 @@ "fawe.info.worldedit.bypassed": "Currently bypassing FAWE restriction.", "fawe.info.worldedit.restricted": "Your FAWE edits are now restricted.", "fawe.info.worldedit.oom.admin": "Possible options:\n - //fast\n - Do smaller edits\n - Allocate more memory\n - Disable `max-memory-percent`", - "fawe.info.temporarily-not-working": "Temporarily not working", + "fawe.info.temporarily-not-working": "Temporarily not working", + "fawe.info.update-available": "An update for FastAsyncWorldEdit is available. You are {0} build(s) out of date.\nYou are running version {1}, the latest version is {2}.\nUpdate at {3}", "fawe.web.generating.link": "Uploading {0}, please wait...", "fawe.web.generating.link.failed": "Failed to generate download link!",