Skip to content

Commit

Permalink
Added username lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
J3fftw1 committed Jun 27, 2023
1 parent ae8c857 commit e2b2585
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<UUID> 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<UUID> forUsername(Plugin plugin, String name) {
Validate.notNull(plugin, "The plugin instance must not be null!");
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<sonar.projectKey>baked-libs_dough</sonar.projectKey>
<sonar.organization>baked-libs</sonar.organization>
Expand Down

0 comments on commit e2b2585

Please sign in to comment.