fix: switch to Java 11's HttpClient

This commit is contained in:
Pyrrha 2022-06-18 13:29:38 +10:00
parent 49d01ef615
commit df5d5a8927
No known key found for this signature in database
GPG key ID: 81D743E41C1C23C1

View file

@ -14,6 +14,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
import net.minecraft.util.WorldSavePath; import net.minecraft.util.WorldSavePath;
@ -27,8 +28,14 @@ import org.slf4j.LoggerFactory;
import org.spongepowered.include.com.google.common.base.Charsets; import org.spongepowered.include.com.google.common.base.Charsets;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -95,16 +102,26 @@ public class PlayerPronouns implements ModInitializer {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
if(config.enablePronounDBSync()) { if(config.enablePronounDBSync()) {
String pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s".formatted(handler.getPlayer().getUuid()); var pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s"
try(CloseableHttpClient client = HttpClients.createMinimal(); .formatted(handler.getPlayer().getUuid());
CloseableHttpResponse resp = client.execute(new HttpGet(pronounDbUrl))) { try {
var client = HttpClient.newBuilder()
if(resp.getStatusLine().getStatusCode() == 200) { .version(HttpClient.Version.HTTP_2)
JsonObject json = JsonHelper.deserialize(new String(resp.getEntity().getContent().readAllBytes(), Charsets.UTF_8)); .followRedirects(HttpClient.Redirect.NORMAL)
String pronouns = PRONOUNDB_ID_MAP.getOrDefault(json.get("pronouns").getAsString(), "ask"); .build();
var req = HttpRequest.newBuilder()
.uri(new URI(pronounDbUrl))
.GET()
.timeout(Duration.ofSeconds(10))
.build();
client.sendAsync(req, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(body -> {
var json = JsonHelper.deserialize(body);
var pronouns = PRONOUNDB_ID_MAP.getOrDefault(json.get("pronouns").getAsString(), "ask");
setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns)));
} }).join();
} catch (IOException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }