Skip to content

Commit

Permalink
Allow choosing an update channel to receive notifications for
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Apr 13, 2024
1 parent fbc43b4 commit 4637949
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terraformersmc.modmenu.api;

public enum UpdateChannel {
ALPHA,
BETA,
RELEASE,
}
5 changes: 4 additions & 1 deletion src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jetbrains.annotations.Nullable;

public interface UpdateInfo {

/**
* @return If an update for the mod is available.
*/
Expand All @@ -23,4 +22,8 @@ default Text getUpdateMessage() {
*/
String getDownloadLink();

/**
* @return The update channel this update is available for.
*/
UpdateChannel getUpdateChannel();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<UpdateChannel> UPDATE_CHANNEL = new EnumConfigOption<>("update_channel", UpdateChannel.RELEASE);

public static SimpleOption<?>[] asOptions() {
ArrayList<SimpleOption<?>> options = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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));
});
}
});
Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -36,4 +38,8 @@ public String getVersionNumber() {
return versionNumber;
}

@Override
public UpdateChannel getUpdateChannel() {
return this.updateChannel;
}
}
6 changes: 5 additions & 1 deletion src/main/resources/assets/modmenu/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 4637949

Please sign in to comment.