From 9f8bbf6e661d8c83da5cc0b70409f9d0ff28442e Mon Sep 17 00:00:00 2001 From: Tim203 Date: Sun, 7 Nov 2021 15:46:48 +0100 Subject: [PATCH] Updated to global api v2 --- .../floodgate/api/SimpleFloodgateApi.java | 23 +++++--------- .../floodgate/command/WhitelistCommand.java | 13 ++++---- .../floodgate/link/GlobalPlayerLinking.java | 27 +++++++--------- .../floodgate/module/CommonModule.java | 6 +--- .../geysermc/floodgate/news/NewsChecker.java | 31 +++++-------------- .../geysermc/floodgate/util/Constants.java | 11 +++---- pom.xml | 2 +- 7 files changed, 38 insertions(+), 75 deletions(-) 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 4ef41101..359e75f2 100644 --- a/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java +++ b/common/src/main/java/org/geysermc/floodgate/api/SimpleFloodgateApi.java @@ -26,6 +26,7 @@ package org.geysermc.floodgate.api; import com.google.common.collect.ImmutableSet; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.util.Collection; import java.util.HashMap; @@ -126,18 +127,13 @@ public class SimpleFloodgateApi implements FloodgateApi { return HttpUtils.asyncGet(Constants.GET_XUID_URL + gamertag) .thenApply(result -> { JsonObject response = result.getResponse(); - boolean success = response.get("success").getAsBoolean(); - if (!success) { + if (!result.isCodeOk()) { throw new IllegalStateException(response.get("message").getAsString()); } - JsonObject data = response.getAsJsonObject("data"); - if (data.size() == 0) { - return null; - } - - return data.get("xuid").getAsLong(); + JsonElement xuid = response.get("xuid"); + return xuid != null ? xuid.getAsLong() : null; }); } @@ -146,18 +142,13 @@ public class SimpleFloodgateApi implements FloodgateApi { return HttpUtils.asyncGet(Constants.GET_GAMERTAG_URL + xuid) .thenApply(result -> { JsonObject response = result.getResponse(); - boolean success = response.get("success").getAsBoolean(); - if (!success) { + if (!result.isCodeOk()) { throw new IllegalStateException(response.get("message").getAsString()); } - JsonObject data = response.getAsJsonObject("data"); - if (data.size() == 0) { - return null; - } - - return data.get("gamertag").getAsString(); + JsonElement gamertag = response.get("gamertag"); + return gamertag != null ? gamertag.getAsString() : null; }); } diff --git a/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java b/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java index 21d31615..3d61596b 100644 --- a/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java +++ b/common/src/main/java/org/geysermc/floodgate/command/WhitelistCommand.java @@ -136,18 +136,16 @@ public class WhitelistCommand implements FloodgateCommand { } JsonObject response = result.getResponse(); - boolean success = response.get("success").getAsBoolean(); - if (!success) { + if (!result.isCodeOk()) { sender.sendMessage(Message.UNEXPECTED_ERROR); logger.error( "Got an error from requesting the xuid of a Bedrock player: {}", - response.get("message").getAsString()); - return; + response.get("message").getAsString() + ); } - JsonObject data = response.getAsJsonObject("data"); - JsonElement xuidElement = data.get("xuid"); + JsonElement xuidElement = response.get("xuid"); if (xuidElement == null) { sender.sendMessage(Message.USER_NOT_FOUND); @@ -175,7 +173,8 @@ public class WhitelistCommand implements FloodgateCommand { } catch (Exception exception) { logger.error( "An unexpected error happened while executing the whitelist command", - exception); + exception + ); } }); } diff --git a/common/src/main/java/org/geysermc/floodgate/link/GlobalPlayerLinking.java b/common/src/main/java/org/geysermc/floodgate/link/GlobalPlayerLinking.java index 65288ec9..301bc18c 100644 --- a/common/src/main/java/org/geysermc/floodgate/link/GlobalPlayerLinking.java +++ b/common/src/main/java/org/geysermc/floodgate/link/GlobalPlayerLinking.java @@ -96,21 +96,19 @@ public class GlobalPlayerLinking extends CommonPlayerLink { DefaultHttpResponse response = HttpUtils.get(GET_BEDROCK_LINK + bedrockId.getLeastSignificantBits()); - // the global api is most likely down + // either the global api is down or it failed to return link if (!response.isCodeOk()) { + if (response.getResponse() != null) { + getLogger().error( + "Failed to request link for {}: {}", + bedrockId.getLeastSignificantBits(), + response.getResponse().get("message").getAsString() + ); + } return null; } - // both on code != 200 and fails with 200 'success' will be false - if (!response.getResponse().get("success").getAsBoolean()) { - getLogger().error( - "Failed to request link for {}: {}", - bedrockId.getLeastSignificantBits(), - response.getResponse().get("message").getAsString()); - return null; - } - - JsonObject data = response.getResponse().getAsJsonObject("data"); + JsonObject data = response.getResponse(); JsonElement javaName = data.get("java_name"); // javaName will be null when the player isn't linked @@ -148,8 +146,7 @@ public class GlobalPlayerLinking extends CommonPlayerLink { DefaultHttpResponse response = HttpUtils.get(GET_BEDROCK_LINK + bedrockId.getLeastSignificantBits()); - // both on http != 200 and fails with 200 success will be false - if (!response.getResponse().get("success").getAsBoolean()) { + if (!response.isCodeOk()) { getLogger().error( "Failed to request link for {}: {}", bedrockId.getLeastSignificantBits(), @@ -157,10 +154,8 @@ public class GlobalPlayerLinking extends CommonPlayerLink { return false; } - JsonObject data = response.getResponse().getAsJsonObject("data"); - // no link if data is empty, otherwise the player is linked - return data.entrySet().size() != 0; + return response.getResponse().entrySet().size() != 0; }, getExecutorService()); } diff --git a/common/src/main/java/org/geysermc/floodgate/module/CommonModule.java b/common/src/main/java/org/geysermc/floodgate/module/CommonModule.java index 32c7b518..cc1f7b42 100644 --- a/common/src/main/java/org/geysermc/floodgate/module/CommonModule.java +++ b/common/src/main/java/org/geysermc/floodgate/module/CommonModule.java @@ -34,11 +34,9 @@ import java.nio.file.Path; import lombok.RequiredArgsConstructor; import org.geysermc.floodgate.addon.data.HandshakeHandlersImpl; import org.geysermc.floodgate.api.FloodgateApi; -import org.geysermc.floodgate.api.InstanceHolder; import org.geysermc.floodgate.api.SimpleFloodgateApi; import org.geysermc.floodgate.api.handshake.HandshakeHandlers; import org.geysermc.floodgate.api.inject.PlatformInjector; -import org.geysermc.floodgate.api.link.PlayerLink; import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.api.packet.PacketHandlers; import org.geysermc.floodgate.api.player.FloodgatePlayer; @@ -186,8 +184,6 @@ public class CommonModule extends AbstractModule { CommandUtil commandUtil, FloodgateLogger logger, GitProperties properties) { - // will be loaded after enabling, so we can use the link instance in InstanceHolder - PlayerLink link = InstanceHolder.getPlayerLink(); String branch = properties.getProperty("git.branch"); String build = properties.getProperty("git.build.number"); @@ -195,7 +191,7 @@ public class CommonModule extends AbstractModule { if (build != null) { buildNumber = Integer.parseInt(build); } - return new NewsChecker(link, commandUtil, logger, branch, buildNumber); + return new NewsChecker(commandUtil, logger, branch, buildNumber); } @Provides diff --git a/common/src/main/java/org/geysermc/floodgate/news/NewsChecker.java b/common/src/main/java/org/geysermc/floodgate/news/NewsChecker.java index 7237a8b7..282ffe7e 100644 --- a/common/src/main/java/org/geysermc/floodgate/news/NewsChecker.java +++ b/common/src/main/java/org/geysermc/floodgate/news/NewsChecker.java @@ -35,7 +35,6 @@ import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import org.geysermc.floodgate.api.link.PlayerLink; import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.news.data.AnnouncementData; import org.geysermc.floodgate.news.data.BuildSpecificData; @@ -48,7 +47,6 @@ import org.geysermc.floodgate.util.Permissions; public class NewsChecker { private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); - private final PlayerLink link; private final CommandUtil commandUtil; private final FloodgateLogger logger; @@ -58,13 +56,7 @@ public class NewsChecker { private boolean firstCheck; - public NewsChecker( - PlayerLink link, - CommandUtil commandUtil, - FloodgateLogger logger, - String branch, - int build) { - this.link = link; + public NewsChecker(CommandUtil commandUtil, FloodgateLogger logger, String branch, int build) { this.commandUtil = commandUtil; this.logger = logger; this.branch = branch; @@ -80,12 +72,17 @@ public class NewsChecker { } private void checkNews() { + // todo also check news for the downloaded database types + HttpResponse response = - HttpUtils.getSilent(Constants.NEWS_OVERVIEW_URL, JsonArray.class); + HttpUtils.getSilent( + Constants.NEWS_OVERVIEW_URL + Constants.NEWS_PROJECT_NAME, + JsonArray.class); + JsonArray array = response.getResponse(); // silent mode doesn't throw exceptions, so array will be null on failure - if (array == null) { + if (array == null || !response.isCodeOk()) { return; } @@ -168,18 +165,6 @@ public class NewsChecker { return; } - if (!item.isGlobal() && !Constants.NEWS_PROJECT_NAME.equals(item.getProject())) { - // only non-integrated database types have a name - if (link.getName() == null) { - return; - } - - String fullDatabaseName = Constants.NEWS_PROJECT_NAME + '/' + link.getName(); - if (!fullDatabaseName.equals(item.getProject())) { - return; - } - } - switch (item.getType()) { case ANNOUNCEMENT: if (!item.getDataAs(AnnouncementData.class).isAffected(Constants.NEWS_PROJECT_NAME)) { 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 d10a5b48..23267e14 100644 --- a/common/src/main/java/org/geysermc/floodgate/util/Constants.java +++ b/common/src/main/java/org/geysermc/floodgate/util/Constants.java @@ -38,10 +38,10 @@ public final class Constants { public static final String HEALTH_URL = "http" + API_BASE_URL + "/health"; 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 NEWS_OVERVIEW_URL = "http" + API_BASE_URL + "/v1/news/"; - public static final String GET_BEDROCK_LINK = "http" + API_BASE_URL + "/v1/link/bedrock/"; + public static final String GET_XUID_URL = "http" + API_BASE_URL + "/v2/xbox/xuid/"; + public static final String GET_GAMERTAG_URL = "http" + API_BASE_URL + "/v2/xbox/gamertag/"; + public static final String NEWS_OVERVIEW_URL = "http" + API_BASE_URL + "/v2/news/"; + public static final String GET_BEDROCK_LINK = "http" + API_BASE_URL + "/v2/link/bedrock/"; public static final String LINK_INFO_URL = "https://link.geysermc.org/"; @@ -49,9 +49,6 @@ public final class Constants { public static final String NTP_SERVER = "time.cloudflare.com"; - public static final String TIMESTAMP_DENIED_MESSAGE = - "Something isn't right with this data." + - " Try logging in again or contact a server administrator if the issue persists."; public static final String INTERNAL_ERROR_MESSAGE = "An internal error happened while handling Floodgate data." + " Try logging in again or contact a server administrator if the issue persists."; diff --git a/pom.xml b/pom.xml index 46a0db17..fb9c9f7a 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ - 1.4.2-SNAPSHOT + 1.4.3-SNAPSHOT 1.0-SNAPSHOT 1.13-R0.1-SNAPSHOT 8.5.3