Skip to content

Commit

Permalink
misc: 调整UpdateHandler、使CatWatcher遇到未知变种时从ValueIndex返回默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 3, 2024
1 parent 58ae5fd commit 25b7464
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private Holder<CatVariant> 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();
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/xyz/nifeather/morph/updates/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,32 +85,29 @@ private void doCheckUpdate(boolean sendMessages, @Nullable Consumer<CheckResult>

var reqId = requestId.addAndGet(1);

HttpClient httpClient = null;

try
{
var urlString = "https://api.modrinth.com"
+ "/v2/project/feathermorph/version"
+ "?"
+ "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());
Expand All @@ -129,6 +128,11 @@ private void doCheckUpdate(boolean sendMessages, @Nullable Consumer<CheckResult>
if (onFinish != null)
onFinish.accept(CheckResult.FAIL);
}
finally
{
if (httpClient != null)
httpClient.close();
}
}

private void onUpdateReqFail(Throwable e, int reqId, @Nullable Consumer<CheckResult> onFinish)
Expand Down

0 comments on commit 25b7464

Please sign in to comment.