From a3c203ad3f1275d98596e39185e9c21d33791e85 Mon Sep 17 00:00:00 2001 From: LostLuma Date: Sat, 13 Apr 2024 06:49:05 +0200 Subject: [PATCH] Allow choosing an update channel to receive notifications for --- .../modmenu/api/UpdateChannel.java | 7 +++++++ .../terraformersmc/modmenu/api/UpdateInfo.java | 5 ++++- .../modmenu/config/ModMenuConfig.java | 2 ++ .../modmenu/util/UpdateCheckerUtil.java | 13 ++++++++++++- .../com/terraformersmc/modmenu/util/mod/Mod.java | 3 ++- .../modmenu/util/mod/ModrinthUpdateInfo.java | 16 +++++++++++----- .../resources/assets/modmenu/lang/en_us.json | 6 +++++- 7 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/terraformersmc/modmenu/api/UpdateChannel.java diff --git a/src/main/java/com/terraformersmc/modmenu/api/UpdateChannel.java b/src/main/java/com/terraformersmc/modmenu/api/UpdateChannel.java new file mode 100644 index 000000000..ffe800ec2 --- /dev/null +++ b/src/main/java/com/terraformersmc/modmenu/api/UpdateChannel.java @@ -0,0 +1,7 @@ +package com.terraformersmc.modmenu.api; + +public enum UpdateChannel { + ALPHA, + BETA, + RELEASE, +} diff --git a/src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java b/src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java index fee9c07b7..b5f40a4f0 100644 --- a/src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java +++ b/src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java @@ -4,7 +4,6 @@ import org.jetbrains.annotations.Nullable; public interface UpdateInfo { - /** * @return If an update for the mod is available. */ @@ -23,4 +22,8 @@ default Text getUpdateMessage() { */ String getDownloadLink(); + /** + * @return The update channel this update is available for. + */ + UpdateChannel getUpdateChannel(); } diff --git a/src/main/java/com/terraformersmc/modmenu/config/ModMenuConfig.java b/src/main/java/com/terraformersmc/modmenu/config/ModMenuConfig.java index bdeba118b..9ad11d111 100644 --- a/src/main/java/com/terraformersmc/modmenu/config/ModMenuConfig.java +++ b/src/main/java/com/terraformersmc/modmenu/config/ModMenuConfig.java @@ -1,6 +1,7 @@ package com.terraformersmc.modmenu.config; import com.google.gson.annotations.SerializedName; +import com.terraformersmc.modmenu.api.UpdateChannel; import com.terraformersmc.modmenu.config.option.BooleanConfigOption; import com.terraformersmc.modmenu.config.option.EnumConfigOption; import com.terraformersmc.modmenu.config.option.OptionConvertable; @@ -44,6 +45,7 @@ public class ModMenuConfig { public static final BooleanConfigOption UPDATE_CHECKER = new BooleanConfigOption("update_checker", true); public static final BooleanConfigOption BUTTON_UPDATE_BADGE = new BooleanConfigOption("button_update_badge", true); public static final BooleanConfigOption QUICK_CONFIGURE = new BooleanConfigOption("quick_configure", true); + public static final EnumConfigOption UPDATE_CHANNEL = new EnumConfigOption<>("update_channel", UpdateChannel.RELEASE); public static SimpleOption[] asOptions() { ArrayList> options = new ArrayList<>(); diff --git a/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java index 509c20600..fc39d28a7 100644 --- a/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java +++ b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java @@ -4,6 +4,7 @@ import com.google.gson.JsonParser; import com.google.gson.annotations.SerializedName; import com.terraformersmc.modmenu.ModMenu; +import com.terraformersmc.modmenu.api.UpdateChannel; import com.terraformersmc.modmenu.api.UpdateChecker; import com.terraformersmc.modmenu.config.ModMenuConfig; import com.terraformersmc.modmenu.util.mod.Mod; @@ -109,6 +110,7 @@ public static void checkForModrinthUpdates() { responseObject.asMap().forEach((lookupHash, versionJson) -> { var versionObj = versionJson.getAsJsonObject(); var projectId = versionObj.get("project_id").getAsString(); + var versionType = versionObj.get("version_type").getAsString(); var versionNumber = versionObj.get("version_number").getAsString(); var versionId = versionObj.get("id").getAsString(); var primaryFile = versionObj.get("files").getAsJsonArray().asList().stream() @@ -118,13 +120,14 @@ public static void checkForModrinthUpdates() { return; } + var updateChannel = UpdateCheckerUtil.getUpdateChannel(versionType); var versionHash = primaryFile.get().getAsJsonObject().get("hashes").getAsJsonObject().get("sha512").getAsString(); if (!Objects.equals(versionHash, lookupHash)) { // hashes different, there's an update. modHashes.get(lookupHash).forEach(mod -> { LOGGER.info("Update available for '{}@{}', (-> {})", mod.getId(), mod.getVersion(), versionNumber); - mod.setUpdateInfo(new ModrinthUpdateInfo(projectId, versionId, versionNumber)); + mod.setUpdateInfo(new ModrinthUpdateInfo(projectId, versionId, versionNumber, updateChannel)); }); } }); @@ -134,6 +137,14 @@ public static void checkForModrinthUpdates() { } } + private static UpdateChannel getUpdateChannel(String versionType) { + try { + return UpdateChannel.valueOf(versionType.toUpperCase(Locale.ROOT)); + } catch (IllegalArgumentException | NullPointerException e) { + return UpdateChannel.RELEASE; + } + } + public static void triggerV2DeprecatedToast() { if (modrinthApiV2Deprecated && ModMenuConfig.UPDATE_CHECKER.getValue()) { MinecraftClient.getInstance().getToastManager().add(new SystemToast( diff --git a/src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java b/src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java index 22c95d6a3..58e2ec4d2 100644 --- a/src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java +++ b/src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java @@ -114,7 +114,8 @@ default boolean hasUpdate() { if (updateInfo == null) { return false; } - return updateInfo.isUpdateAvailable(); + + return updateInfo.isUpdateAvailable() && updateInfo.getUpdateChannel().compareTo(ModMenuConfig.UPDATE_CHANNEL.getValue()) >= 0; } default @Nullable String getSha512Hash() throws IOException { diff --git a/src/main/java/com/terraformersmc/modmenu/util/mod/ModrinthUpdateInfo.java b/src/main/java/com/terraformersmc/modmenu/util/mod/ModrinthUpdateInfo.java index 9a17b5a46..f982211b0 100644 --- a/src/main/java/com/terraformersmc/modmenu/util/mod/ModrinthUpdateInfo.java +++ b/src/main/java/com/terraformersmc/modmenu/util/mod/ModrinthUpdateInfo.java @@ -1,17 +1,19 @@ package com.terraformersmc.modmenu.util.mod; +import com.terraformersmc.modmenu.api.UpdateChannel; import com.terraformersmc.modmenu.api.UpdateInfo; public class ModrinthUpdateInfo implements UpdateInfo { + protected final String projectId; + protected final String versionId; + protected final String versionNumber; + protected final UpdateChannel updateChannel; - protected String projectId; - protected String versionId; - protected String versionNumber; - - public ModrinthUpdateInfo(String projectId, String versionId, String versionNumber) { + public ModrinthUpdateInfo(String projectId, String versionId, String versionNumber, UpdateChannel updateChannel) { this.projectId = projectId; this.versionId = versionId; this.versionNumber = versionNumber; + this.updateChannel = updateChannel; } @Override @@ -36,4 +38,8 @@ public String getVersionNumber() { return versionNumber; } + @Override + public UpdateChannel getUpdateChannel() { + return this.updateChannel; + } } diff --git a/src/main/resources/assets/modmenu/lang/en_us.json b/src/main/resources/assets/modmenu/lang/en_us.json index 1c42d9fb8..0d6f80b03 100644 --- a/src/main/resources/assets/modmenu/lang/en_us.json +++ b/src/main/resources/assets/modmenu/lang/en_us.json @@ -164,5 +164,9 @@ "option.modmenu.button_update_badge.false": "Hidden", "option.modmenu.quick_configure": "Quick Configure", "option.modmenu.quick_configure.true": "Enabled", - "option.modmenu.quick_configure.false": "Disabled" + "option.modmenu.quick_configure.false": "Disabled", + "option.modmenu.update_channel": "Update Channel", + "option.modmenu.update_channel.alpha": "All", + "option.modmenu.update_channel.beta": "Release & Beta", + "option.modmenu.update_channel.release": "Release" }