|
| 1 | +package com.eternalcode.commons.updater.impl; |
| 2 | + |
| 3 | +import com.eternalcode.commons.updater.UpdateChecker; |
| 4 | +import com.eternalcode.commons.updater.UpdateResult; |
| 5 | +import com.eternalcode.commons.updater.Version; |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.JsonParseException; |
| 8 | +import com.google.gson.annotations.SerializedName; |
| 9 | +import com.google.gson.reflect.TypeToken; |
| 10 | +import java.net.URI; |
| 11 | +import java.net.http.HttpClient; |
| 12 | +import java.net.http.HttpRequest; |
| 13 | +import java.net.http.HttpResponse; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +public final class ModrinthUpdateChecker implements UpdateChecker { |
| 19 | + |
| 20 | + private static final String API_BASE_URL = "https://api.modrinth.com/v2"; |
| 21 | + private static final String MODRINTH_BASE_URL = "https://modrinth.com/plugin"; |
| 22 | + private static final String USER_AGENT = "UpdateChecker/1.0"; |
| 23 | + |
| 24 | + private static final Gson GSON = new Gson(); |
| 25 | + |
| 26 | + private final HttpClient client = HttpClient.newBuilder() |
| 27 | + .connectTimeout(Duration.ofSeconds(60)) |
| 28 | + .build(); |
| 29 | + |
| 30 | + @Override |
| 31 | + public UpdateResult check(String projectId, Version currentVersion) { |
| 32 | + if (projectId == null || projectId.isBlank()) { |
| 33 | + throw new IllegalArgumentException("Project ID cannot be null or empty"); |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + HttpRequest request = HttpRequest.newBuilder() |
| 38 | + .uri(URI.create(API_BASE_URL + "/project/" + projectId + "/version")) |
| 39 | + .header("User-Agent", USER_AGENT) |
| 40 | + .timeout(Duration.ofSeconds(30)) |
| 41 | + .build(); |
| 42 | + |
| 43 | + HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 44 | + if (response.statusCode() != 200) { |
| 45 | + return UpdateResult.empty(currentVersion); |
| 46 | + } |
| 47 | + |
| 48 | + return this.parseVersionResponse(response.body(), currentVersion, projectId); |
| 49 | + } |
| 50 | + catch (Exception exception) { |
| 51 | + throw new RuntimeException("Failed to check Modrinth updates for project: " + projectId, exception); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private UpdateResult parseVersionResponse(String json, Version currentVersion, String projectId) { |
| 56 | + try { |
| 57 | + List<ModrinthVersion> versions = GSON.fromJson(json, new TypeToken<>(){}); |
| 58 | + if (versions == null || versions.isEmpty()) { |
| 59 | + return UpdateResult.empty(currentVersion); |
| 60 | + } |
| 61 | + |
| 62 | + ModrinthVersion latestVersionData = versions.get(0); |
| 63 | + String versionNumber = latestVersionData.versionNumber(); |
| 64 | + if (versionNumber == null || versionNumber.trim().isEmpty()) { |
| 65 | + return UpdateResult.empty(currentVersion); |
| 66 | + } |
| 67 | + |
| 68 | + String releaseUrl = MODRINTH_BASE_URL + "/" + projectId + "/version/" + versionNumber; |
| 69 | + String downloadUrl = latestVersionData.files().stream() |
| 70 | + .map(modrinthFile -> modrinthFile.url()) |
| 71 | + .filter(obj -> Objects.nonNull(obj)) |
| 72 | + .findFirst() |
| 73 | + .orElse(releaseUrl); |
| 74 | + |
| 75 | + Version latestVersion = new Version(versionNumber); |
| 76 | + return new UpdateResult(currentVersion, latestVersion, downloadUrl, releaseUrl); |
| 77 | + } |
| 78 | + catch (JsonParseException exception) { |
| 79 | + return UpdateResult.empty(currentVersion); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private record ModrinthVersion(@SerializedName("version_number") String versionNumber, List<ModrinthFile> files) { |
| 84 | + } |
| 85 | + |
| 86 | + private record ModrinthFile(String url) { |
| 87 | + } |
| 88 | +} |
0 commit comments