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