From e2b25856839a0351f500e0633cf09bf6d61ee994 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Tue, 27 Jun 2023 15:19:17 +0200 Subject: [PATCH] Added username lookup --- .../bakedlibs/dough/skins/UUIDLookup.java | 59 ++++++++++++++++++- pom.xml | 4 +- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/UUIDLookup.java b/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/UUIDLookup.java index 5705a0be..f52426cb 100644 --- a/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/UUIDLookup.java +++ b/dough-skins/src/main/java/io/github/bakedlibs/dough/skins/UUIDLookup.java @@ -2,8 +2,13 @@ import java.io.InputStreamReader; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; +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.UUID; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; @@ -12,13 +17,12 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; -import org.apache.commons.lang.Validate; -import org.bukkit.plugin.Plugin; - import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.lang.Validate; +import org.bukkit.plugin.Plugin; import io.github.bakedlibs.dough.common.DoughLogger; @@ -31,6 +35,55 @@ public class UUIDLookup { private UUIDLookup() {} + /** + * Returns the {@link CompletableFuture} with the {@link UUID } + * + * @param plugin plugin invoking this function + * @param name username of the player + * @return {@link CompletableFuture} with the {@link UUID } + */ + @ParametersAreNonnullByDefault + public static @Nonnull CompletableFuture getUuidFromUsername(Plugin plugin, String name) { + Validate.notNull(plugin, "The plugin instance must not be null!"); + Validate.notNull(name, "The username cannot be null!"); + + if (!NAME_PATTERN.matcher(name).matches()) { + throw new IllegalArgumentException("\"" + name + "\" is not a valid Minecraft Username!"); + } + + String targetUrl = "https://playerdb.co/api/player/minecraft/" + name; + + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(targetUrl)) + .timeout(Duration.ofMinutes(2)) + .header("user-agent", "Mozilla/5.0 Dough (+https://github.com/baked-libs/dough)") + .GET() + .build(); + + return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenApply(s -> JSON_PARSER.parse(s).getAsJsonObject()) + .thenApply(jsonObject -> { + if (jsonObject.get("code").getAsString().equals("player.found")) { + JsonObject data = jsonObject.getAsJsonObject("data"); + JsonObject player = data.getAsJsonObject("player"); + return UUID.fromString(player.get("id").getAsString()); + } else { + return null; + } + }); + } + + /** + * Returns the {@link CompletableFuture} with the {@link UUID } + * + * @param plugin plugin invoking this function + * @param name username of the player + * @return {@link CompletableFuture} with the {@link UUID } + * @deprecated This has been Deprecated since 1.3.1 now use {@link #getUuidFromUsername(Plugin, String)} + */ + @Deprecated(since = "1.3.1") @ParametersAreNonnullByDefault public static @Nonnull CompletableFuture forUsername(Plugin plugin, String name) { Validate.notNull(plugin, "The plugin instance must not be null!"); diff --git a/pom.xml b/pom.xml index 8440563a..39ecb686 100644 --- a/pom.xml +++ b/pom.xml @@ -16,8 +16,8 @@ UTF-8 - 1.8 - 1.8 + 11 + 11 baked-libs_dough baked-libs