From 25b7464b60a5883a2180426434e9bffb22a33e2a Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Sun, 3 Nov 2024 16:09:35 +0800 Subject: [PATCH] =?UTF-8?q?misc:=20=E8=B0=83=E6=95=B4UpdateHandler?= =?UTF-8?q?=E3=80=81=E4=BD=BFCatWatcher=E9=81=87=E5=88=B0=E6=9C=AA?= =?UTF-8?q?=E7=9F=A5=E5=8F=98=E7=A7=8D=E6=97=B6=E4=BB=8EValueIndex?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../watchers/types/CatWatcher.java | 4 ++-- .../morph/updates/UpdateHandler.java | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/datawatcher/watchers/types/CatWatcher.java b/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/datawatcher/watchers/types/CatWatcher.java index d614be46..96cb8bb1 100644 --- a/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/datawatcher/watchers/types/CatWatcher.java +++ b/src/main/java/xyz/nifeather/morph/backends/server/renderer/network/datawatcher/watchers/types/CatWatcher.java @@ -59,9 +59,9 @@ private Holder bukkitTypeToNmsHolder(Cat.Type bukkitType) } catch (Throwable t) { - logger.warn("Bukkit type '%s' is not in the registries, trying Tabby...".formatted(bukkitType)); + logger.warn("Bukkit type '%s' is not in the registries, trying default value...".formatted(bukkitType)); - return HolderUtils.getHolderOrThrow(CatVariant.TABBY, Registries.CAT_VARIANT); + return ValueIndex.CAT.CAT_VARIANT.defaultValue(); } } diff --git a/src/main/java/xyz/nifeather/morph/updates/UpdateHandler.java b/src/main/java/xyz/nifeather/morph/updates/UpdateHandler.java index bcb34db6..295221aa 100644 --- a/src/main/java/xyz/nifeather/morph/updates/UpdateHandler.java +++ b/src/main/java/xyz/nifeather/morph/updates/UpdateHandler.java @@ -21,10 +21,12 @@ import xyz.nifeather.morph.messages.UpdateStrings; import xyz.nifeather.morph.misc.permissions.CommonPermissions; -import java.net.URL; +import java.net.URI; +import java.net.URLDecoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.ArrayList; import java.util.Map; @@ -83,6 +85,8 @@ private void doCheckUpdate(boolean sendMessages, @Nullable Consumer var reqId = requestId.addAndGet(1); + HttpClient httpClient = null; + try { var urlString = "https://api.modrinth.com" @@ -90,25 +94,20 @@ private void doCheckUpdate(boolean sendMessages, @Nullable Consumer + "?" + "game_versions=[\"%s\"]"; - urlString = urlString.formatted(Bukkit.getMinecraftVersion()) - .replace("[", "%5B") // Make URI happy - .replace("]", "%5D") - .replace("\"", "%22"); - - var url = new URL(urlString).toURI(); + var uri = new URI(URLDecoder.decode(urlString, StandardCharsets.UTF_8)); var request = HttpRequest.newBuilder() .GET() - .uri(url) + .uri(uri) .timeout(Duration.ofSeconds(10)) .header("User-Agent", "feathermorph") .build(); - var client = HttpClient.newBuilder() + httpClient = HttpClient.newBuilder() .followRedirects(HttpClient.Redirect.ALWAYS) .build(); - var response = client.send(request, HttpResponse.BodyHandlers.ofString()); + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) { logger.error("Failed to check update: Server returned HTTP code {}", response.statusCode()); @@ -129,6 +128,11 @@ private void doCheckUpdate(boolean sendMessages, @Nullable Consumer if (onFinish != null) onFinish.accept(CheckResult.FAIL); } + finally + { + if (httpClient != null) + httpClient.close(); + } } private void onUpdateReqFail(Throwable e, int reqId, @Nullable Consumer onFinish)