1
0
mirror of https://github.com/GeyserMC/Floodgate.git synced 2026-01-06 15:42:03 +00:00

Updated to global api v2

This commit is contained in:
Tim203
2021-11-07 15:46:48 +01:00
parent 2d2c38e120
commit 9f8bbf6e66
7 changed files with 38 additions and 75 deletions

View File

@@ -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;
});
}

View File

@@ -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
);
}
});
}

View File

@@ -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());
}

View File

@@ -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

View File

@@ -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<JsonArray> 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)) {

View File

@@ -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.";

View File

@@ -45,7 +45,7 @@
</issueManagement>
<properties>
<geyser.version>1.4.2-SNAPSHOT</geyser.version>
<geyser.version>1.4.3-SNAPSHOT</geyser.version>
<cumulus.version>1.0-SNAPSHOT</cumulus.version>
<spigot.version>1.13-R0.1-SNAPSHOT</spigot.version>
<fastutil.version>8.5.3</fastutil.version>