Skip to content

Commit

Permalink
Attempt to implement Mod Menu's update checker API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jun 18, 2024
1 parent 4b0789e commit ccbd207
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 49 deletions.
45 changes: 0 additions & 45 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.parsers.json.JsonReader;
import org.quiltmc.parsers.json.JsonWriter;
import org.semver4j.Semver;
import org.slf4j.Logger;

import java.io.BufferedReader;
Expand Down Expand Up @@ -809,49 +807,6 @@ public static <T> T httpGet(
}
}

public static CompletableFuture<Optional<String>> checkForUpdates() {
return CompletableFuture.<Optional<String>>supplyAsync(() -> {
try (CloseableHttpClient client = HttpClients.createMinimal()) {
final String latestVersion = httpGet(
client, "https://api.modrinth.com/v2/project/world-host/version",
builder -> builder
.addParameter("game_versions", "[\"" + getModVersion("minecraft") + "\"]")
.addParameter("loaders", "[\"" + MOD_LOADER + "\"]"),
input -> {
try (JsonReader reader = JsonReader.json(new InputStreamReader(input, StandardCharsets.UTF_8))) {
reader.beginArray();
if (!reader.hasNext()) {
return null;
}
reader.beginObject();
while (reader.hasNext()) {
final String key = reader.nextName();
if (!key.equals("version_number")) {
reader.skipValue();
continue;
}
return reader.nextString();
}
return null;
}
}
);
if (latestVersion == null) {
return Optional.empty();
}
if (new Semver(getModVersion(MOD_ID)).compareTo(new Semver(latestVersion)) >= 0) {
return Optional.empty();
}
return Optional.of(latestVersion);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}, Util.ioPool()).exceptionally(t -> {
LOGGER.error("Failed to check for updates", t);
return Optional.empty();
});
}

private static Path getGameDir() {
//#if FABRIC
return FabricLoader.getInstance().getGameDir();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.github.gaming32.worldhost;

import net.minecraft.Util;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.quiltmc.parsers.json.JsonReader;
import org.semver4j.Semver;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class WorldHostUpdateChecker {
public static final String MODRINTH_ID = "world-host";

public static CompletableFuture<Optional<String>> checkForUpdates() {
return CompletableFuture.<Optional<String>>supplyAsync(() -> {
try (CloseableHttpClient client = HttpClients.createMinimal()) {
final String latestVersion = WorldHost.httpGet(
client, "https://api.modrinth.com/v2/project/" + MODRINTH_ID + "/version",
builder -> builder
.addParameter("game_versions", "[\"" + WorldHost.getModVersion("minecraft") + "\"]")
.addParameter("loaders", "[\"" + WorldHost.MOD_LOADER + "\"]"),
input -> {
try (JsonReader reader = JsonReader.json(new InputStreamReader(input, StandardCharsets.UTF_8))) {
reader.beginArray();
if (!reader.hasNext()) {
return null;
}
reader.beginObject();
while (reader.hasNext()) {
final String key = reader.nextName();
if (!key.equals("version_number")) {
reader.skipValue();
continue;
}
return reader.nextString();
}
return null;
}
}
);
if (latestVersion == null) {
return Optional.empty();
}
if (new Semver(WorldHost.getModVersion(WorldHost.MOD_ID)).compareTo(new Semver(latestVersion)) >= 0) {
return Optional.empty();
}
return Optional.of(latestVersion);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}, Util.ioPool()).exceptionally(t -> {
WorldHost.LOGGER.error("Failed to check for updates", t);
return Optional.empty();
});
}

public static String formatUpdateLink(String version) {
return "https://modrinth.com/mod/" + MODRINTH_ID + "/version/" + version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,42 @@
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.github.gaming32.worldhost.gui.screen.WorldHostConfigScreen;

//#if MC >= 1.20.6
import com.terraformersmc.modmenu.api.UpdateChannel;
import com.terraformersmc.modmenu.api.UpdateChecker;
import com.terraformersmc.modmenu.api.UpdateInfo;
import io.github.gaming32.worldhost.WorldHostUpdateChecker;
//#endif

public class WorldHostModMenuCompat implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return WorldHostConfigScreen::new;
}

//#if MC >= 1.20.6
@Override
public UpdateChecker getUpdateChecker() {
return () -> WorldHostUpdateChecker.checkForUpdates()
.join()
.map(version -> new UpdateInfo() {
@Override
public boolean isUpdateAvailable() {
return true;
}

@Override
public String getDownloadLink() {
return WorldHostUpdateChecker.formatUpdateLink(version);
}

@Override
public UpdateChannel getUpdateChannel() {
return UpdateChannel.RELEASE;
}
})
.orElse(null);
}
//#endif
}
//#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.gaming32.worldhost.FriendsListUpdate;
import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.WorldHostUpdateChecker;
import io.github.gaming32.worldhost.gui.screen.AddFriendScreen;
import io.github.gaming32.worldhost.gui.screen.FriendsScreen;
import io.github.gaming32.worldhost.gui.screen.JoiningWorldHostScreen;
Expand Down Expand Up @@ -234,13 +235,12 @@ public void handle(ProtocolClient client) {
final var message = Components.translatable("world-host.outdated_world_host.desc", currentVersion, recommendedVersion);
WorldHost.LOGGER.info(message.getString());
if (!WorldHost.CONFIG.isShowOutdatedWorldHost()) return;
WorldHost.checkForUpdates().thenAcceptAsync(version -> {
WorldHostUpdateChecker.checkForUpdates().thenAcceptAsync(version -> {
if (version.isEmpty()) return;
final String updateLink = WorldHostUpdateChecker.formatUpdateLink(version.get());
WHToast.builder("world-host.outdated_world_host")
.description(message)
.clickAction(() -> Util.getPlatform().openUri(
"https://modrinth.com/mod/world-host/version/" + version.get()
))
.clickAction(() -> Util.getPlatform().openUri(updateLink))
.ticks(200)
.show();
}, Minecraft.getInstance());
Expand Down

0 comments on commit ccbd207

Please sign in to comment.