diff --git a/src/main/java/net/raphimc/minecraftauth/responsehandler/exception/RealmsResponseException.java b/src/main/java/net/raphimc/minecraftauth/responsehandler/exception/RealmsResponseException.java index 4a747e4..980db1a 100644 --- a/src/main/java/net/raphimc/minecraftauth/responsehandler/exception/RealmsResponseException.java +++ b/src/main/java/net/raphimc/minecraftauth/responsehandler/exception/RealmsResponseException.java @@ -28,7 +28,7 @@ public class RealmsResponseException extends HttpResponseException { private final int realmsErrorCode; public RealmsResponseException(final int statusCode, final int realmsErrorCode, final String reasonPhrase) { - super(statusCode, reasonPhrase + ", java realms error code: " + realmsErrorCode); + super(statusCode, reasonPhrase + ", realms error code: " + realmsErrorCode); this.realmsErrorCode = realmsErrorCode; } diff --git a/src/main/java/net/raphimc/minecraftauth/service/realms/BedrockRealmsService.java b/src/main/java/net/raphimc/minecraftauth/service/realms/BedrockRealmsService.java index 922f1ef..ff33729 100644 --- a/src/main/java/net/raphimc/minecraftauth/service/realms/BedrockRealmsService.java +++ b/src/main/java/net/raphimc/minecraftauth/service/realms/BedrockRealmsService.java @@ -26,7 +26,9 @@ import net.raphimc.minecraftauth.step.xbl.StepXblXstsToken; import org.apache.http.HttpHeaders; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.message.AbstractHttpMessage; import java.util.concurrent.CompletableFuture; @@ -35,6 +37,8 @@ public class BedrockRealmsService extends AbstractRealmsService { public static final String JOIN_WORLD_URL = "https://pocket.realms.minecraft.net/worlds/$ID/join"; + public static final String ACCEPT_INVITE_URL = "https://pocket.realms.minecraft.net/invites/v1/link/accept/$CODE"; + public static final String DELETE_INVITE_URL = "https://pocket.realms.minecraft.net/invites/$ID"; private final StepXblXstsToken.XblXsts realmsXsts; private final String clientVersion; @@ -66,6 +70,35 @@ public String get() { }); } + public CompletableFuture acceptInvite(final String realmCode) { + return CompletableFuture.supplyAsync(new Supplier() { + @Override + @SneakyThrows + public RealmsWorld get() { + final HttpPost httpPost = new HttpPost(ACCEPT_INVITE_URL.replace("$CODE", realmCode)); + BedrockRealmsService.this.addRequestHeaders(httpPost); + final String response = BedrockRealmsService.this.httpClient.execute(httpPost, new RealmsResponseHandler()); + final JsonObject obj = JsonParser.parseString(response).getAsJsonObject(); + return RealmsWorld.fromJson(obj); + } + }); + } + + public CompletableFuture leaveInvitedRealm(final RealmsWorld realmsWorld) { + return CompletableFuture.runAsync(new Runnable() { + @Override + @SneakyThrows + public void run() { + final HttpDelete httpDelete = new HttpDelete(DELETE_INVITE_URL.replace("$ID", String.valueOf(realmsWorld.getId()))); + BedrockRealmsService.this.addRequestHeaders(httpDelete); + final String response = BedrockRealmsService.this.httpClient.execute(httpDelete, new RealmsResponseHandler()); + if (response != null) { + throw new IllegalStateException("Failed to delete invite: " + response); + } + } + }); + } + @Override protected void addRequestHeaders(final AbstractHttpMessage httpMessage) { httpMessage.addHeader(HttpHeaders.AUTHORIZATION, "XBL3.0 x=" + this.realmsXsts.getServiceToken()); diff --git a/src/main/java/net/raphimc/minecraftauth/service/realms/model/RealmsWorld.java b/src/main/java/net/raphimc/minecraftauth/service/realms/model/RealmsWorld.java index 255508b..613b583 100644 --- a/src/main/java/net/raphimc/minecraftauth/service/realms/model/RealmsWorld.java +++ b/src/main/java/net/raphimc/minecraftauth/service/realms/model/RealmsWorld.java @@ -40,7 +40,7 @@ public class RealmsWorld { public static RealmsWorld fromJson(final JsonObject json) { return new RealmsWorld( json.get("id").getAsLong(), - json.get("owner").getAsString(), + JsonUtil.getStringOr(json, "owner", ""), json.get("ownerUUID").getAsString(), json.get("name").getAsString(), json.get("motd").getAsString(),