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
This commit is contained in:
NotMyFault 2021-10-17 09:55:25 +02:00 committed by GitHub
parent e5fbc4c971
commit 5db9a601b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 103 additions and 3 deletions

View File

@ -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/"))
));
}
}
}
}

View File

@ -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

View File

@ -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() {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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!",