diff --git a/src/main/java/net/asodev/islandutils/IslandUtils.java b/src/main/java/net/asodev/islandutils/IslandUtils.java index b4e6cf3..c5aa1cf 100644 --- a/src/main/java/net/asodev/islandutils/IslandUtils.java +++ b/src/main/java/net/asodev/islandutils/IslandUtils.java @@ -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; @@ -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; @@ -31,11 +33,14 @@ public void onInitialize() { FriendsInGame.init(); Optional 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(); } diff --git a/src/main/java/net/asodev/islandutils/util/updater/UpdateManager.java b/src/main/java/net/asodev/islandutils/util/updater/UpdateManager.java index 709516a..aa5c0f6 100644 --- a/src/main/java/net/asodev/islandutils/util/updater/UpdateManager.java +++ b/src/main/java/net/asodev/islandutils/util/updater/UpdateManager.java @@ -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; @@ -21,14 +31,22 @@ public UpdateManager() { gson = new Gson(); } - public CompletableFuture checkForUpdates() throws Exception { - CompletableFuture f = new CompletableFuture<>(); + public CompletableFuture> checkForUpdates() throws Exception { + CompletableFuture> 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 json = (List) gson.fromJson(res.body(), type); + f.complete(json); }); return f; @@ -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!"); } } diff --git a/src/main/java/net/asodev/islandutils/util/updater/schema/Asset.java b/src/main/java/net/asodev/islandutils/util/updater/schema/Asset.java deleted file mode 100644 index ef295b5..0000000 --- a/src/main/java/net/asodev/islandutils/util/updater/schema/Asset.java +++ /dev/null @@ -1,153 +0,0 @@ - -package net.asodev.islandutils.util.updater.schema; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -public class Asset { - - @SerializedName("url") - @Expose - private String url; - @SerializedName("id") - @Expose - private Integer id; - @SerializedName("node_id") - @Expose - private String nodeId; - @SerializedName("name") - @Expose - private String name; - @SerializedName("label") - @Expose - private Object label; - @SerializedName("uploader") - @Expose - private Uploader uploader; - @SerializedName("content_type") - @Expose - private String contentType; - @SerializedName("state") - @Expose - private String state; - @SerializedName("size") - @Expose - private Integer size; - @SerializedName("download_count") - @Expose - private Integer downloadCount; - @SerializedName("created_at") - @Expose - private String createdAt; - @SerializedName("updated_at") - @Expose - private String updatedAt; - @SerializedName("browser_download_url") - @Expose - private String browserDownloadUrl; - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Object getLabel() { - return label; - } - - public void setLabel(Object label) { - this.label = label; - } - - public Uploader getUploader() { - return uploader; - } - - public void setUploader(Uploader uploader) { - this.uploader = uploader; - } - - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - - public Integer getSize() { - return size; - } - - public void setSize(Integer size) { - this.size = size; - } - - public Integer getDownloadCount() { - return downloadCount; - } - - public void setDownloadCount(Integer downloadCount) { - this.downloadCount = downloadCount; - } - - public String getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(String createdAt) { - this.createdAt = createdAt; - } - - public String getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(String updatedAt) { - this.updatedAt = updatedAt; - } - - public String getBrowserDownloadUrl() { - return browserDownloadUrl; - } - - public void setBrowserDownloadUrl(String browserDownloadUrl) { - this.browserDownloadUrl = browserDownloadUrl; - } - -} diff --git a/src/main/java/net/asodev/islandutils/util/updater/schema/Author.java b/src/main/java/net/asodev/islandutils/util/updater/schema/Author.java deleted file mode 100644 index 9ce6865..0000000 --- a/src/main/java/net/asodev/islandutils/util/updater/schema/Author.java +++ /dev/null @@ -1,208 +0,0 @@ - -package net.asodev.islandutils.util.updater.schema; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -public class Author { - - @SerializedName("login") - @Expose - private String login; - @SerializedName("id") - @Expose - private Integer id; - @SerializedName("node_id") - @Expose - private String nodeId; - @SerializedName("avatar_url") - @Expose - private String avatarUrl; - @SerializedName("gravatar_id") - @Expose - private String gravatarId; - @SerializedName("url") - @Expose - private String url; - @SerializedName("html_url") - @Expose - private String htmlUrl; - @SerializedName("followers_url") - @Expose - private String followersUrl; - @SerializedName("following_url") - @Expose - private String followingUrl; - @SerializedName("gists_url") - @Expose - private String gistsUrl; - @SerializedName("starred_url") - @Expose - private String starredUrl; - @SerializedName("subscriptions_url") - @Expose - private String subscriptionsUrl; - @SerializedName("organizations_url") - @Expose - private String organizationsUrl; - @SerializedName("repos_url") - @Expose - private String reposUrl; - @SerializedName("events_url") - @Expose - private String eventsUrl; - @SerializedName("received_events_url") - @Expose - private String receivedEventsUrl; - @SerializedName("type") - @Expose - private String type; - @SerializedName("site_admin") - @Expose - private Boolean siteAdmin; - - public String getLogin() { - return login; - } - - public void setLogin(String login) { - this.login = login; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public String getAvatarUrl() { - return avatarUrl; - } - - public void setAvatarUrl(String avatarUrl) { - this.avatarUrl = avatarUrl; - } - - public String getGravatarId() { - return gravatarId; - } - - public void setGravatarId(String gravatarId) { - this.gravatarId = gravatarId; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getHtmlUrl() { - return htmlUrl; - } - - public void setHtmlUrl(String htmlUrl) { - this.htmlUrl = htmlUrl; - } - - public String getFollowersUrl() { - return followersUrl; - } - - public void setFollowersUrl(String followersUrl) { - this.followersUrl = followersUrl; - } - - public String getFollowingUrl() { - return followingUrl; - } - - public void setFollowingUrl(String followingUrl) { - this.followingUrl = followingUrl; - } - - public String getGistsUrl() { - return gistsUrl; - } - - public void setGistsUrl(String gistsUrl) { - this.gistsUrl = gistsUrl; - } - - public String getStarredUrl() { - return starredUrl; - } - - public void setStarredUrl(String starredUrl) { - this.starredUrl = starredUrl; - } - - public String getSubscriptionsUrl() { - return subscriptionsUrl; - } - - public void setSubscriptionsUrl(String subscriptionsUrl) { - this.subscriptionsUrl = subscriptionsUrl; - } - - public String getOrganizationsUrl() { - return organizationsUrl; - } - - public void setOrganizationsUrl(String organizationsUrl) { - this.organizationsUrl = organizationsUrl; - } - - public String getReposUrl() { - return reposUrl; - } - - public void setReposUrl(String reposUrl) { - this.reposUrl = reposUrl; - } - - public String getEventsUrl() { - return eventsUrl; - } - - public void setEventsUrl(String eventsUrl) { - this.eventsUrl = eventsUrl; - } - - public String getReceivedEventsUrl() { - return receivedEventsUrl; - } - - public void setReceivedEventsUrl(String receivedEventsUrl) { - this.receivedEventsUrl = receivedEventsUrl; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Boolean getSiteAdmin() { - return siteAdmin; - } - - public void setSiteAdmin(Boolean siteAdmin) { - this.siteAdmin = siteAdmin; - } - -} diff --git a/src/main/java/net/asodev/islandutils/util/updater/schema/GithubRelease.java b/src/main/java/net/asodev/islandutils/util/updater/schema/GithubRelease.java deleted file mode 100644 index f906af4..0000000 --- a/src/main/java/net/asodev/islandutils/util/updater/schema/GithubRelease.java +++ /dev/null @@ -1,210 +0,0 @@ - -package net.asodev.islandutils.util.updater.schema; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -import java.util.List; - -public class GithubRelease { - - @SerializedName("url") - @Expose - private String url; - @SerializedName("assets_url") - @Expose - private String assetsUrl; - @SerializedName("upload_url") - @Expose - private String uploadUrl; - @SerializedName("html_url") - @Expose - private String htmlUrl; - @SerializedName("id") - @Expose - private Integer id; - @SerializedName("author") - @Expose - private Author author; - @SerializedName("node_id") - @Expose - private String nodeId; - @SerializedName("tag_name") - @Expose - private String tagName; - @SerializedName("target_commitish") - @Expose - private String targetCommitish; - @SerializedName("name") - @Expose - private String name; - @SerializedName("draft") - @Expose - private Boolean draft; - @SerializedName("prerelease") - @Expose - private Boolean prerelease; - @SerializedName("created_at") - @Expose - private String createdAt; - @SerializedName("published_at") - @Expose - private String publishedAt; - @SerializedName("assets") - @Expose - private List assets = null; - @SerializedName("tarball_url") - @Expose - private String tarballUrl; - @SerializedName("zipball_url") - @Expose - private String zipballUrl; - @SerializedName("body") - @Expose - private String body; - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getAssetsUrl() { - return assetsUrl; - } - - public void setAssetsUrl(String assetsUrl) { - this.assetsUrl = assetsUrl; - } - - public String getUploadUrl() { - return uploadUrl; - } - - public void setUploadUrl(String uploadUrl) { - this.uploadUrl = uploadUrl; - } - - public String getHtmlUrl() { - return htmlUrl; - } - - public void setHtmlUrl(String htmlUrl) { - this.htmlUrl = htmlUrl; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public Author getAuthor() { - return author; - } - - public void setAuthor(Author author) { - this.author = author; - } - - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public String getTagName() { - return tagName; - } - - public void setTagName(String tagName) { - this.tagName = tagName; - } - - public String getTargetCommitish() { - return targetCommitish; - } - - public void setTargetCommitish(String targetCommitish) { - this.targetCommitish = targetCommitish; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Boolean getDraft() { - return draft; - } - - public void setDraft(Boolean draft) { - this.draft = draft; - } - - public Boolean getPrerelease() { - return prerelease; - } - - public void setPrerelease(Boolean prerelease) { - this.prerelease = prerelease; - } - - public String getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(String createdAt) { - this.createdAt = createdAt; - } - - public String getPublishedAt() { - return publishedAt; - } - - public void setPublishedAt(String publishedAt) { - this.publishedAt = publishedAt; - } - - public List getAssets() { - return assets; - } - - public void setAssets(List assets) { - this.assets = assets; - } - - public String getTarballUrl() { - return tarballUrl; - } - - public void setTarballUrl(String tarballUrl) { - this.tarballUrl = tarballUrl; - } - - public String getZipballUrl() { - return zipballUrl; - } - - public void setZipballUrl(String zipballUrl) { - this.zipballUrl = zipballUrl; - } - - public String getBody() { - return body; - } - - public void setBody(String body) { - this.body = body; - } - -} diff --git a/src/main/java/net/asodev/islandutils/util/updater/schema/ModrinthVersion.java b/src/main/java/net/asodev/islandutils/util/updater/schema/ModrinthVersion.java new file mode 100644 index 0000000..b6731f1 --- /dev/null +++ b/src/main/java/net/asodev/islandutils/util/updater/schema/ModrinthVersion.java @@ -0,0 +1,3 @@ +package net.asodev.islandutils.util.updater.schema; + +public record ModrinthVersion(String name, String version_number) {} diff --git a/src/main/java/net/asodev/islandutils/util/updater/schema/Uploader.java b/src/main/java/net/asodev/islandutils/util/updater/schema/Uploader.java deleted file mode 100644 index 7aadce8..0000000 --- a/src/main/java/net/asodev/islandutils/util/updater/schema/Uploader.java +++ /dev/null @@ -1,207 +0,0 @@ - -package net.asodev.islandutils.util.updater.schema; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; -public class Uploader { - - @SerializedName("login") - @Expose - private String login; - @SerializedName("id") - @Expose - private Integer id; - @SerializedName("node_id") - @Expose - private String nodeId; - @SerializedName("avatar_url") - @Expose - private String avatarUrl; - @SerializedName("gravatar_id") - @Expose - private String gravatarId; - @SerializedName("url") - @Expose - private String url; - @SerializedName("html_url") - @Expose - private String htmlUrl; - @SerializedName("followers_url") - @Expose - private String followersUrl; - @SerializedName("following_url") - @Expose - private String followingUrl; - @SerializedName("gists_url") - @Expose - private String gistsUrl; - @SerializedName("starred_url") - @Expose - private String starredUrl; - @SerializedName("subscriptions_url") - @Expose - private String subscriptionsUrl; - @SerializedName("organizations_url") - @Expose - private String organizationsUrl; - @SerializedName("repos_url") - @Expose - private String reposUrl; - @SerializedName("events_url") - @Expose - private String eventsUrl; - @SerializedName("received_events_url") - @Expose - private String receivedEventsUrl; - @SerializedName("type") - @Expose - private String type; - @SerializedName("site_admin") - @Expose - private Boolean siteAdmin; - - public String getLogin() { - return login; - } - - public void setLogin(String login) { - this.login = login; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public String getAvatarUrl() { - return avatarUrl; - } - - public void setAvatarUrl(String avatarUrl) { - this.avatarUrl = avatarUrl; - } - - public String getGravatarId() { - return gravatarId; - } - - public void setGravatarId(String gravatarId) { - this.gravatarId = gravatarId; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getHtmlUrl() { - return htmlUrl; - } - - public void setHtmlUrl(String htmlUrl) { - this.htmlUrl = htmlUrl; - } - - public String getFollowersUrl() { - return followersUrl; - } - - public void setFollowersUrl(String followersUrl) { - this.followersUrl = followersUrl; - } - - public String getFollowingUrl() { - return followingUrl; - } - - public void setFollowingUrl(String followingUrl) { - this.followingUrl = followingUrl; - } - - public String getGistsUrl() { - return gistsUrl; - } - - public void setGistsUrl(String gistsUrl) { - this.gistsUrl = gistsUrl; - } - - public String getStarredUrl() { - return starredUrl; - } - - public void setStarredUrl(String starredUrl) { - this.starredUrl = starredUrl; - } - - public String getSubscriptionsUrl() { - return subscriptionsUrl; - } - - public void setSubscriptionsUrl(String subscriptionsUrl) { - this.subscriptionsUrl = subscriptionsUrl; - } - - public String getOrganizationsUrl() { - return organizationsUrl; - } - - public void setOrganizationsUrl(String organizationsUrl) { - this.organizationsUrl = organizationsUrl; - } - - public String getReposUrl() { - return reposUrl; - } - - public void setReposUrl(String reposUrl) { - this.reposUrl = reposUrl; - } - - public String getEventsUrl() { - return eventsUrl; - } - - public void setEventsUrl(String eventsUrl) { - this.eventsUrl = eventsUrl; - } - - public String getReceivedEventsUrl() { - return receivedEventsUrl; - } - - public void setReceivedEventsUrl(String receivedEventsUrl) { - this.receivedEventsUrl = receivedEventsUrl; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Boolean getSiteAdmin() { - return siteAdmin; - } - - public void setSiteAdmin(Boolean siteAdmin) { - this.siteAdmin = siteAdmin; - } - -}