From 35593efb0edb39c4df1239cff5fd4b493600dfd3 Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Thu, 25 Nov 2021 23:46:08 +0100 Subject: [PATCH] Adapt UpdateCheckerTask to download platform jar --- .../common/entities/http/GitHubAsset.java | 33 +++++++++++++++---- .../common/tasks/UpdateCheckerTask.java | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/entities/http/GitHubAsset.java b/Common/src/main/java/net/frankheijden/serverutils/common/entities/http/GitHubAsset.java index 3c7ce4a..811d3fe 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/entities/http/GitHubAsset.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/entities/http/GitHubAsset.java @@ -1,7 +1,11 @@ package net.frankheijden.serverutils.common.entities.http; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.Locale; +import java.util.function.Predicate; +import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; public class GitHubAsset { @@ -17,13 +21,30 @@ public class GitHubAsset { * Creates a new GitHubAsset from given release url. */ public static GitHubAsset from(JsonObject jsonObject) { + return from(jsonObject, name -> true); + } + + /** + * Creates a new GitHubAsset for given platfrom from given release url. + */ + public static GitHubAsset from(JsonObject jsonObject, ServerUtilsPlugin.Platform platform) { + return from(jsonObject, name -> name.toUpperCase(Locale.ENGLISH).contains(platform.name())); + } + + /** + * Creates a new GitHubAsset from given release url. + */ + public static GitHubAsset from(JsonObject jsonObject, Predicate namePredicate) { JsonArray assets = jsonObject.getAsJsonArray("assets"); - if (assets != null && assets.size() > 0) { - JsonObject assetJson = assets.get(0).getAsJsonObject(); - return new GitHubAsset( - assetJson.get("name").getAsString(), - assetJson.get("browser_download_url").getAsString() - ); + if (assets != null) { + for (JsonElement asset : assets) { + JsonObject assetJson = asset.getAsJsonObject(); + + String name = assetJson.get("name").getAsString(); + if (namePredicate.test(name)) { + return new GitHubAsset(name, assetJson.get("browser_download_url").getAsString()); + } + } } return null; } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java index 5986a03..e8c17e6 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java @@ -128,7 +128,7 @@ public class UpdateCheckerTask, P> im plugin.getLogger().log(Level.INFO, UPDATE_AVAILABLE, githubVersion); plugin.getLogger().log(Level.INFO, RELEASE_INFO, body); - GitHubAsset pluginAsset = GitHubAsset.from(pluginJson); + GitHubAsset pluginAsset = GitHubAsset.from(pluginJson, plugin.getPlatform()); if (!download || pluginAsset == null) { if (sender.isPlayer()) { Component component = plugin.getMessagesResource().get(MessageKey.UPDATE_AVAILABLE).toComponent(