fancydialogs: Add version fetcher and config

This commit is contained in:
Oliver
2025-06-27 16:49:26 +02:00
parent 4a8d130235
commit 23efe9b951
6 changed files with 102 additions and 17 deletions

View File

@@ -2,11 +2,6 @@ Commit hash: %COMMIT_HASH%
Commit message: %COMMIT_MESSAGE%
Changes:
- Added default values for confirmation dialog
- Added support for the select input
- Marked Folia as supported
*(The last commit message does not always directly reflect the changes related to this version.)*
Keep in mind that this is a snapshot version. Snapshot versions are **not** meant to be used in production. They are for **testing purposes only** and may contain bugs or incomplete features. Use at your own risk.

View File

@@ -1 +1 @@
0.0.6
0.0.7

View File

@@ -125,8 +125,9 @@ tasks {
val props = mapOf(
"description" to project.description,
"version" to getFDVersion(),
"hash" to gitCommitHash.get(),
"build" to (System.getenv("BUILD_ID") ?: "").ifEmpty { "undefined" }
"commit_hash" to gitCommitHash.get(),
"channel" to (System.getenv("RELEASE_CHANNEL") ?: "").ifEmpty { "undefined" },
"platform" to (System.getenv("RELEASE_PLATFORM") ?: "").ifEmpty { "undefined" }
)
inputs.properties(props)

View File

@@ -25,11 +25,15 @@ import de.oliver.fancyanalytics.logger.LogLevel;
import de.oliver.fancyanalytics.logger.appender.Appender;
import de.oliver.fancyanalytics.logger.appender.ConsoleAppender;
import de.oliver.fancyanalytics.logger.appender.JsonAppender;
import de.oliver.fancylib.VersionConfig;
import de.oliver.fancylib.serverSoftware.ServerSoftware;
import de.oliver.fancylib.translations.Language;
import de.oliver.fancylib.translations.TextConfig;
import de.oliver.fancylib.translations.Translator;
import de.oliver.fancylib.versionFetcher.MasterVersionFetcher;
import de.oliver.fancylib.versionFetcher.VersionFetcher;
import de.oliver.fancysitula.api.utils.ServerVersion;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import revxrsal.commands.Lamp;
@@ -38,10 +42,9 @@ import revxrsal.commands.bukkit.actor.BukkitCommandActor;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.*;
import static java.util.concurrent.CompletableFuture.supplyAsync;
public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
@@ -49,6 +52,8 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
private final ExtendedFancyLogger fancyLogger;
private FancyDialogsConfig fdConfig;
private VersionFetcher versionFetcher;
private VersionConfig versionConfig;
private Translator translator;
private DialogRegistry dialogRegistry;
private DialogStorage dialogStorage;
@@ -98,6 +103,10 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
.findFirst().orElse(translator.getFallbackLanguage());
translator.setSelectedLanguage(selectedLanguage);
versionFetcher = new MasterVersionFetcher(getName());
versionConfig = new VersionConfig(this, versionFetcher);
versionConfig.load();
dialogStorage = new JsonDialogStorage();
Collection<DialogData> dialogData = dialogStorage.loadAll();
@@ -120,6 +129,25 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
@Override
public void onEnable() {
final ComparableVersion currentVersion = new ComparableVersion(versionConfig.getVersion());
supplyAsync(versionFetcher::fetchNewestVersion)
.thenApply(Objects::requireNonNull)
.whenComplete((newest, error) -> {
if (error != null || newest.compareTo(currentVersion) <= 0) {
return; // could not get the newest version or already on latest
}
fancyLogger.warn("You are not using the latest version of the FancyDialogs plugin.");
getLogger().warning("""
-------------------------------------------------------
You are not using the latest version of the FancyDialogs plugin.
Please update to the newest version (%s).
%s
-------------------------------------------------------
""".formatted(newest, versionFetcher.getDownloadUrl()));
});
if (!ServerSoftware.isPaper()) {
fancyLogger.warn("""
--------------------------------------------------
@@ -131,7 +159,7 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
}
String version = Bukkit.getMinecraftVersion();
if (ServerVersion.getByVersion(version).getProtocolVersion() < 771) {
if (ServerVersion.getByVersion(version).getProtocolVersion() < ServerVersion.v1_21_6.getProtocolVersion()) {
fancyLogger.error("""
--------------------------------------------------
FancyDialogs requires Minecraft version 1.21.6 or higher.
@@ -155,7 +183,6 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
Analytics analytics = new Analytics();
analytics.registerMetrics();
analytics.start();
fancyLogger.info("Successfully enabled FancyDialogs version %s".formatted(getDescription().getVersion()));
@@ -209,6 +236,10 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
return fdConfig;
}
public VersionConfig getVersionConfig() {
return versionConfig;
}
@Override
public DialogRegistry getDialogRegistry() {
return dialogRegistry;

View File

@@ -2,9 +2,15 @@ package com.fancyinnovations.fancydialogs.analytics;
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
import de.oliver.fancyanalytics.api.FancyAnalyticsAPI;
import de.oliver.fancyanalytics.api.events.Event;
import de.oliver.fancyanalytics.api.metrics.MetricSupplier;
import de.oliver.fancylib.VersionConfig;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class Analytics {
private final FancyAnalyticsAPI api;
@@ -14,12 +20,17 @@ public class Analytics {
api.getConfig().setDisableLogging(true);
}
public void registerMetrics() {
private void registerMetrics() {
api.registerMinecraftPluginMetrics(FancyDialogsPlugin.get());
api.getExceptionHandler().registerLogger(FancyDialogsPlugin.get().getLogger());
api.getExceptionHandler().registerLogger(Bukkit.getLogger());
api.getExceptionHandler().registerLogger(FancyDialogsPlugin.get().getFancyLogger());
api.registerStringMetric(new MetricSupplier<>("language", () -> FancyDialogsPlugin.get().getTranslator().getSelectedLanguage().getLanguageCode()));
api.registerStringMetric(new MetricSupplier<>("release_channel", () -> FancyDialogsPlugin.get().getVersionConfig().getChannel()));
api.registerStringMetric(new MetricSupplier<>("release_platform", () -> FancyDialogsPlugin.get().getVersionConfig().getPlatform()));
api.registerStringMetric(new MetricSupplier<>("server_size", () -> {
long onlinePlayers = Bukkit.getOnlinePlayers().size();
@@ -45,8 +56,51 @@ public class Analytics {
api.registerNumberMetric(new MetricSupplier<>("amount_dialogs", () -> (double) FancyDialogsPlugin.get().getDialogRegistry().getAll().size()));
}
public void start() {
api.initialize();
private void checkIfVersionUpdated() {
VersionConfig versionConfig = FancyDialogsPlugin.get().getVersionConfig();
String currentVersion = versionConfig.getVersion();
String lastVersion = "N/A";
File versionFile = new File(FancyDialogsPlugin.get().getDataFolder(), "version.yml");
if (!versionFile.exists()) {
try {
Files.write(versionFile.toPath(), currentVersion.getBytes());
} catch (IOException e) {
FancyDialogsPlugin.get().getFancyLogger().warn("Could not write version file.");
return;
}
} else {
try {
lastVersion = new String(Files.readAllBytes(versionFile.toPath()));
} catch (IOException e) {
FancyDialogsPlugin.get().getFancyLogger().warn("Could not read version file.");
return;
}
}
if (!lastVersion.equals(currentVersion)) {
FancyDialogsPlugin.get().getFancyLogger().info("Plugin has been updated from version " + lastVersion + " to " + currentVersion + ".");
api.sendEvent(
new Event("PluginVersionUpdated")
.withProperty("from", lastVersion)
.withProperty("to", currentVersion)
.withProperty("commit_hash", versionConfig.getCommitHash())
.withProperty("channel", versionConfig.getChannel())
.withProperty("platform", versionConfig.getPlatform())
);
try {
Files.write(versionFile.toPath(), currentVersion.getBytes());
} catch (IOException e) {
FancyDialogsPlugin.get().getFancyLogger().warn("Could not write version file.");
}
}
}
public void start() {
registerMetrics();
api.initialize();
checkIfVersionUpdated();
}
}

View File

@@ -0,0 +1,4 @@
version: $version
commit_hash: $commit_hash
platform: $platform
channel: $channel