Skip to content

Commit

Permalink
Add plugin updater
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Sep 17, 2024
1 parent 67786d0 commit e7e390e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}
}

Expand All @@ -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")}")
Expand Down Expand Up @@ -95,6 +98,7 @@ tasks {

shadowJar {
relocate("org.incendo", "de.oliver")
relocate("org.lushplugins", "de.oliver")
archiveClassifier.set("")
dependsOn(":api:shadowJar")
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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("<color:#ffe27a>A new</color> <color:#e0c01b>%plugin%</color> <color:#ffe27a>update is now available!</color> <color:#e0c01b>%current_version%</color> <color:#ffe27a>-></color> <color:#e0c01b>%latest_version%</color> <color:#adadad>(<u><click:run_command:'/fancynpcs update'>click here to download</click></u>)</color>")
.build();
}

// register bStats and sentry
boolean isDevelopmentBuild = !versionConfig.getBuild().equalsIgnoreCase("undefined");

Expand Down Expand Up @@ -420,4 +436,8 @@ public boolean isUsingPlotSquared() {
public JavaPlugin getPlugin() {
return instance;
}

public Updater getUpdater() {
return updater;
}
}
34 changes: 32 additions & 2 deletions src/main/java/de/oliver/fancynpcs/commands/FancyNpcsCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down

0 comments on commit e7e390e

Please sign in to comment.