From 34a793f41ff9aee1117c41a0ca416cb3e1bdf164 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Tue, 27 Apr 2021 18:40:54 +0200 Subject: [PATCH] You can now fetch xuid by gamertag and gamertag by xuid --- .../geysermc/floodgate/api/FloodgateApi.java | 37 ++++++++++++++ .../floodgate/api/SimpleFloodgateApi.java | 48 +++++++++++++++++++ .../geysermc/floodgate/util/Constants.java | 1 + 3 files changed, 86 insertions(+) diff --git a/api/src/main/java/org/geysermc/floodgate/api/FloodgateApi.java b/api/src/main/java/org/geysermc/floodgate/api/FloodgateApi.java index f506ee91..c6e31c55 100644 --- a/api/src/main/java/org/geysermc/floodgate/api/FloodgateApi.java +++ b/api/src/main/java/org/geysermc/floodgate/api/FloodgateApi.java @@ -27,6 +27,7 @@ package org.geysermc.floodgate.api; import java.util.Collection; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import org.geysermc.cumulus.Form; import org.geysermc.cumulus.util.FormBuilder; import org.geysermc.floodgate.api.link.PlayerLink; @@ -94,6 +95,42 @@ public interface FloodgateApi { boolean sendForm(UUID uuid, FormBuilder formBuilder); + /** + * Get the xuid of the user that has the given gamertag. + * + * @param gamertag the gamertag of the player + * @return the xuid of the player with the given gamertag, or null when there is no player with + * the given gamertag + */ + CompletableFuture getXuidFor(String gamertag); + + /** + * Get the xuid of the player that has the given gamertag. It does the same thing as {@link + * #getXuidFor(String)} except that this method will return the xuid in Floodgate uuid format + * instead of just a long + * + * @param gamertag the gamertag of the player + * @return the xuid of the player with the given gamertag, or null when there is no player with + * the given gamertag + */ + default CompletableFuture getUuidFor(String gamertag) { + return getXuidFor(gamertag).thenApply(xuid -> { + if (xuid == null) { + return null; + } + return createJavaPlayerId(xuid); + }); + } + + /** + * Get the gamertag of the user that has the given xuid. + * + * @param xuid the gamertag of the player + * @return the gamertag of the player with the given xuid, or null when there is not player with + * the given xuid + */ + CompletableFuture getGamertagFor(long xuid); + /** * Returns the instance that manages all the linking. */ diff --git a/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java b/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java index 196f5184..33e930cb 100644 --- a/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java +++ b/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java @@ -26,10 +26,12 @@ package org.geysermc.floodgate.api; import com.google.common.collect.ImmutableSet; +import com.google.gson.JsonObject; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import javax.annotation.Nullable; import lombok.RequiredArgsConstructor; import org.geysermc.cumulus.Form; @@ -39,6 +41,8 @@ import org.geysermc.floodgate.config.FloodgateConfigHolder; import org.geysermc.floodgate.player.FloodgatePlayerImpl; import org.geysermc.floodgate.pluginmessage.PluginMessageManager; import org.geysermc.floodgate.pluginmessage.channel.FormChannel; +import org.geysermc.floodgate.util.Constants; +import org.geysermc.floodgate.util.HttpUtils; import org.geysermc.floodgate.util.Utils; @RequiredArgsConstructor @@ -105,6 +109,50 @@ public class SimpleFloodgateApi implements FloodgateApi { return sendForm(uuid, formBuilder.build()); } + @Override + public CompletableFuture getXuidFor(String gamertag) { + if (gamertag == null || gamertag.isEmpty() || gamertag.length() > 16) { + return Utils.failedFuture(new IllegalStateException("Received an invalid gamertag")); + } + + return HttpUtils.asyncGet(Constants.GET_XUID_URL + gamertag) + .thenApply(result -> { + JsonObject response = result.getResponse(); + boolean success = response.get("success").getAsBoolean(); + + if (!success) { + throw new IllegalStateException(response.get("message").getAsString()); + } + + JsonObject data = response.getAsJsonObject("data"); + if (data.size() == 0) { + return null; + } + + return data.get("xuid").getAsLong(); + }); + } + + @Override + public CompletableFuture getGamertagFor(long xuid) { + return HttpUtils.asyncGet(Constants.GET_GAMERTAG_URL + xuid) + .thenApply(result -> { + JsonObject response = result.getResponse(); + boolean success = response.get("success").getAsBoolean(); + + if (!success) { + throw new IllegalStateException(response.get("message").getAsString()); + } + + JsonObject data = response.getAsJsonObject("data"); + if (data.size() == 0) { + return null; + } + + return data.get("gamertag").getAsString(); + }); + } + public FloodgatePlayer addPlayer(UUID uuid, FloodgatePlayer player) { return players.put(uuid, player); } diff --git a/common/src/main/java/org/geysermc/floodgate/util/Constants.java b/common/src/main/java/org/geysermc/floodgate/util/Constants.java index d1d7be22..c2308b62 100644 --- a/common/src/main/java/org/geysermc/floodgate/util/Constants.java +++ b/common/src/main/java/org/geysermc/floodgate/util/Constants.java @@ -32,6 +32,7 @@ public final class Constants { private static final String API_BASE_URL = "s://api.geysermc.org"; public static final String WEBSOCKET_URL = "ws" + API_BASE_URL + "/ws"; public static final String GET_XUID_URL = "http" + API_BASE_URL + "/v1/xbox/xuid/"; + public static final String GET_GAMERTAG_URL = "http" + API_BASE_URL + "/v1/xbox/gamertag/"; public static final String LINK_INFO_URL = "https://link.geysermc.org/";