Skip to content

Commit

Permalink
Added realms invite code support to BedrockRealmsService
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Nov 27, 2023
1 parent 2523ce0 commit 030ae66
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -66,6 +70,35 @@ public String get() {
});
}

public CompletableFuture<RealmsWorld> acceptInvite(final String realmCode) {
return CompletableFuture.supplyAsync(new Supplier<RealmsWorld>() {
@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<Void> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 030ae66

Please sign in to comment.