From e7e390ebf50104de417b71c483c27fc79be1b81d Mon Sep 17 00:00:00 2001 From: OliverSchlueter Date: Tue, 17 Sep 2024 20:56:50 +0200 Subject: [PATCH] Add plugin updater --- build.gradle.kts | 12 ++++--- gradle.properties | 1 + .../java/de/oliver/fancynpcs/FancyNpcs.java | 20 +++++++++++ .../fancynpcs/commands/FancyNpcsCMD.java | 34 +++++++++++++++++-- .../listeners/PlayerJoinListener.java | 4 ++- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a0a40cbb..58235c8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,9 +27,9 @@ allprojects { repositories { mavenLocal() mavenCentral() - maven(url = "https://repo.papermc.io/repository/maven-public/") - maven(url = "https://repo.fancyplugins.de/releases") - maven(url = "https://repo.smrt-1.com/releases") + maven("https://repo.papermc.io/repository/maven-public/") + maven("https://repo.fancyplugins.de/releases") + maven("https://repo.lushplugins.org/releases/") } } @@ -46,8 +46,11 @@ dependencies { implementation(project(":implementation_1_19_4", configuration = "reobf")) implementation("de.oliver:FancyLib:${findProperty("fancyLibVersion")}") - compileOnly("me.dave:ChatColorHandler:${findProperty("chatcolorhandlerVersion")}") implementation("de.oliver.FancyAnalytics:api:${findProperty("fancyAnalyticsVersion")}") + + compileOnly("me.dave:ChatColorHandler:${findProperty("chatcolorhandlerVersion")}") // is shaded into the api + implementation("org.lushplugins.pluginupdater:PluginUpdater-API:${findProperty("pluginUpdaterVersion")}") + implementation("org.incendo:cloud-core:${findProperty("cloudCoreVersion")}") implementation("org.incendo:cloud-paper:${findProperty("cloudPaperVersion")}") implementation("org.incendo:cloud-annotations:${findProperty("cloudAnnotationsVersion")}") @@ -95,6 +98,7 @@ tasks { shadowJar { relocate("org.incendo", "de.oliver") + relocate("org.lushplugins", "de.oliver") archiveClassifier.set("") dependsOn(":api:shadowJar") } diff --git a/gradle.properties b/gradle.properties index b6fbdb82..a73da261 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,7 @@ fancyLibVersion=1.0.31 fancyAnalyticsVersion=0.0.8 plotsquaredVersion=7.2.0 chatcolorhandlerVersion=v2.5.3 +pluginUpdaterVersion=1.0.0 cloudCoreVersion=2.0.0-rc.2 cloudPaperVersion=2.0.0-beta.8 cloudAnnotationsVersion=2.0.0-rc.2 \ No newline at end of file diff --git a/src/main/java/de/oliver/fancynpcs/FancyNpcs.java b/src/main/java/de/oliver/fancynpcs/FancyNpcs.java index 61508e6c..0b83e56d 100644 --- a/src/main/java/de/oliver/fancynpcs/FancyNpcs.java +++ b/src/main/java/de/oliver/fancynpcs/FancyNpcs.java @@ -42,6 +42,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.lushplugins.pluginupdater.api.updater.Updater; import java.util.ArrayList; import java.util.List; @@ -58,6 +59,7 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin { public static final FeatureFlag PLAYER_NPCS_FEATURE_FLAG = new FeatureFlag("player-npcs", "Every player can only manage the npcs they have created", false); public static final FeatureFlag USE_FANCYANALYTICS_FEATURE_FLAG = new FeatureFlag("use-fancyanalytics", "Use FancyAnalytics to report plugin usage and errors", false); + public static final FeatureFlag ENABLE_PLUGIN_UPDATER_FEATURE_FLAG = new FeatureFlag("enable-plugin-updater", "Enables the plugin updater / notifier", false); private static FancyNpcs instance; private final ScheduledExecutorService npcThread; @@ -67,6 +69,7 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin { private final FeatureFlagConfig featureFlagConfig; private final VersionFetcher versionFetcher; private final FancyAnalyticsAPI fancyAnalytics; + private Updater updater; private CloudCommandManager commandManager; private TextConfig textConfig; private Translator translator; @@ -106,6 +109,7 @@ public void onLoad() { // Load feature flags featureFlagConfig.addFeatureFlag(PLAYER_NPCS_FEATURE_FLAG); featureFlagConfig.addFeatureFlag(USE_FANCYANALYTICS_FEATURE_FLAG); + featureFlagConfig.addFeatureFlag(ENABLE_PLUGIN_UPDATER_FEATURE_FLAG); featureFlagConfig.load(); String mcVersion = Bukkit.getMinecraftVersion(); @@ -193,6 +197,18 @@ Please update to the newest version (%s). getLogger().warning("--------------------------------------------------"); } + if (ENABLE_PLUGIN_UPDATER_FEATURE_FLAG.isEnabled()) { + updater = new Updater.Builder(instance) + .modrinth("fancynpcs", true) + .hangar("FancyNpcs") + .github("FancyMcPlugins/FancyNpcs") + .checkSchedule(config.isMuteVersionNotification() ? -1 : 5) + .notify(!config.isMuteVersionNotification()) + .notificationPermission("fancynpcs.admin") + .notificationMessage("A new %plugin% update is now available! %current_version% -> %latest_version% (click here to download)") + .build(); + } + // register bStats and sentry boolean isDevelopmentBuild = !versionConfig.getBuild().equalsIgnoreCase("undefined"); @@ -420,4 +436,8 @@ public boolean isUsingPlotSquared() { public JavaPlugin getPlugin() { return instance; } + + public Updater getUpdater() { + return updater; + } } diff --git a/src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java b/src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java index bfe9e5a5..d83cd8ec 100644 --- a/src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java +++ b/src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java @@ -10,6 +10,7 @@ import org.incendo.cloud.annotations.Command; import org.incendo.cloud.annotations.Permission; import org.jetbrains.annotations.NotNull; +import org.lushplugins.pluginupdater.api.updater.Updater; import java.time.ZoneId; import java.time.format.DateTimeFormatter; @@ -91,14 +92,43 @@ public void onFeatureFlags(final CommandSender sender) { .replace("id", FancyNpcs.USE_FANCYANALYTICS_FEATURE_FLAG.getName()) .replace("state", getTranslatedState(FancyNpcs.USE_FANCYANALYTICS_FEATURE_FLAG.isEnabled())) .send(sender); + translator.translate("fancynpcs_feature_flags_entry") + .replace("number", "3") + .replace("name", "Enable plugin updater") + .replace("id", FancyNpcs.ENABLE_PLUGIN_UPDATER_FEATURE_FLAG.getName()) + .replace("state", getTranslatedState(FancyNpcs.ENABLE_PLUGIN_UPDATER_FEATURE_FLAG.isEnabled())) + .send(sender); translator.translate("fancynpcs_feature_flags_footer") .replace("count", "2") - .replace("count_formatted", "· · 2") + .replace("count_formatted", "· · 3") .replace("total", String.valueOf(FancyNpcs.getInstance().getNpcManager().getAllNpcs().size())) - .replace("total_formatted", "· · 2") + .replace("total_formatted", "· · 3") .send(sender); } + @Command("fancynpcs update") + @Permission("fancynpcs.command.fancynpcs.update") + public void onUpdate(final CommandSender sender) { + Updater updater = plugin.getUpdater(); + + System.out.println("Is already downloaded: " + updater.isAlreadyDownloaded()); + System.out.println("Is update available: " + updater.isUpdateAvailable()); + if (updater.isAlreadyDownloaded() || !updater.isUpdateAvailable()) { + sender.sendMessage("It looks like there is no new update available!"); + return; + } + + sender.sendMessage("Attempting to update plugin..."); + + updater.attemptDownload().thenAccept(success -> { + if (success) { + sender.sendMessage("Successfully updated plugin, restart the server to apply changes!"); + } else { + sender.sendMessage("Failed to update plugin!"); + } + }); + } + // NOTE: Might need to be improved later down the line, should get work done for now. private @NotNull String getTranslatedState(final boolean bool) { return (bool) ? ((SimpleMessage) translator.translate("enabled")).getMessage() : ((SimpleMessage) translator.translate("disabled")).getMessage(); diff --git a/src/main/java/de/oliver/fancynpcs/listeners/PlayerJoinListener.java b/src/main/java/de/oliver/fancynpcs/listeners/PlayerJoinListener.java index bda772a6..605af6e6 100644 --- a/src/main/java/de/oliver/fancynpcs/listeners/PlayerJoinListener.java +++ b/src/main/java/de/oliver/fancynpcs/listeners/PlayerJoinListener.java @@ -32,7 +32,9 @@ public void onPlayerJoin(PlayerJoinEvent event) { FancyNpcs.getInstance().getVisibilityTracker().addJoinDelayPlayer(event.getPlayer().getUniqueId()); FancyNpcs.getInstance().getScheduler().runTaskLater(null, 20L * 2, () -> FancyNpcs.getInstance().getVisibilityTracker().removeJoinDelayPlayer(event.getPlayer().getUniqueId())); - if (!FancyNpcs.getInstance().getFancyNpcConfig().isMuteVersionNotification() && event.getPlayer().hasPermission("FancyNpcs.admin")) { + if (!FancyNpcs.ENABLE_PLUGIN_UPDATER_FEATURE_FLAG.isEnabled() && + !FancyNpcs.getInstance().getFancyNpcConfig().isMuteVersionNotification() && + event.getPlayer().hasPermission("FancyNpcs.admin")) { FancyNpcs.getInstance().getScheduler().runTaskAsynchronously( () -> FancyNpcs.getInstance().getVersionConfig().checkVersionAndDisplay(event.getPlayer(), true) );