Skip to content

Commit

Permalink
fix: only show new updates for your current version
Browse files Browse the repository at this point in the history
  • Loading branch information
AsoDesu committed Jul 28, 2024
1 parent 16d8a68 commit f6fb9b5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 791 deletions.
13 changes: 9 additions & 4 deletions src/main/java/net/asodev/islandutils/IslandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,7 +22,8 @@ public class IslandUtils implements ModInitializer {
public static UpdateManager updater;
public static ResourcePackUpdater packUpdater;

public static String version = "";
public static Version version;
public static String versionString;
public static AvailableUpdate availableUpdate;
private static boolean isPreRelease = false;

Expand All @@ -31,11 +33,14 @@ public void onInitialize() {
FriendsInGame.init();

Optional<ModContainer> container = FabricLoader.getInstance().getModContainer("islandutils");
container.ifPresent(modContainer -> version = modContainer.getMetadata().getVersion().getFriendlyString());
container.ifPresent(modContainer -> {
version = modContainer.getMetadata().getVersion();
versionString = version.getFriendlyString();
});

updater = new UpdateManager();
isPreRelease = version.contains("-pre") || FabricLoader.getInstance().isDevelopmentEnvironment();
if (!version.contains("-pre")) {
isPreRelease = versionString.contains("-pre") || FabricLoader.getInstance().isDevelopmentEnvironment();
if (!versionString.contains("-pre")) {
updater.runUpdateCheck();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package net.asodev.islandutils.util.updater;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.terraformersmc.modmenu.util.mod.Mod;
import net.asodev.islandutils.IslandUtils;
import net.asodev.islandutils.util.updater.schema.AvailableUpdate;
import net.asodev.islandutils.util.updater.schema.GithubRelease;
import net.asodev.islandutils.util.updater.schema.ModrinthVersion;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.VersionParsingException;
import net.minecraft.SharedConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class UpdateManager {

private static final Logger logger = LoggerFactory.getLogger(UpdateManager.class);
HttpClient client;
Gson gson;

Expand All @@ -21,14 +31,22 @@ public UpdateManager() {
gson = new Gson();
}

public CompletableFuture<GithubRelease> checkForUpdates() throws Exception {
CompletableFuture<GithubRelease> f = new CompletableFuture<>();
public CompletableFuture<List<ModrinthVersion>> checkForUpdates() throws Exception {
CompletableFuture<List<ModrinthVersion>> f = new CompletableFuture<>();
String version = "[\"" + SharedConstants.getCurrentVersion().getName() + "\"]";
String url = String.format(
"https://api.modrinth.com/v2/project/island-utils/version?game_versions=%s&loaders=%s",
URLEncoder.encode(version, StandardCharsets.UTF_8),
URLEncoder.encode("[\"fabric\"]", StandardCharsets.UTF_8)
);

URI updatorURI = new URI("https://api.github.com/repos/AsoDesu/IslandUtils/releases/latest");
URI updatorURI = new URI(url);
HttpRequest req = HttpRequest.newBuilder(updatorURI).GET().build();

client.sendAsync(req, HttpResponse.BodyHandlers.ofString()).thenAccept(res -> {
f.complete(gson.fromJson(res.body(), GithubRelease.class));
TypeToken<?> type = TypeToken.getParameterized(List.class, ModrinthVersion.class);
List<ModrinthVersion> json = (List<ModrinthVersion>) gson.fromJson(res.body(), type);
f.complete(json);
});

return f;
Expand All @@ -38,13 +56,26 @@ public void runUpdateCheck() {
try {
checkForUpdates().thenAccept(res -> {
if (res == null) return;
if (!IslandUtils.version.equals(res.getTagName())) {
ModrinthVersion newVersion = null;
for (ModrinthVersion version : res) {
try {
Version updateVersion = Version.parse(version.version_number());
if (IslandUtils.version.compareTo(updateVersion) < 0) {
newVersion = version;
break;
}
} catch (VersionParsingException e) {
logger.error("Unable to parse version: '" + version.version_number() + "'");
}
}
if (newVersion != null) {
String version = newVersion.version_number();
IslandUtils.availableUpdate =
new AvailableUpdate(res.getName(), res.getTagName(), "https://modrinth.com/mod/island-utils/version/" + res.getTagName());
new AvailableUpdate(newVersion.name(), version, "https://modrinth.com/mod/island-utils/version/" + version);
}
});
} catch (Exception e) {
System.err.println("Failed to get IslandUtils Update!");
logger.error("Failed to get IslandUtils Update!");
}
}

Expand Down
153 changes: 0 additions & 153 deletions src/main/java/net/asodev/islandutils/util/updater/schema/Asset.java

This file was deleted.

Loading

0 comments on commit f6fb9b5

Please sign in to comment.