Skip to content

Commit d4597bf

Browse files
committed
Add user presence to show as online and ingame over xbox
1 parent d74ded3 commit d4597bf

6 files changed

Lines changed: 30 additions & 3 deletions

File tree

bootstrap/geyser/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/geyser/MCXboxBroadcastExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private void createSession() {
174174
try {
175175
logger.info("Setting up Xbox session...");
176176
sessionManager.createSession(sessionInfo);
177+
sessionManager.updatePresence();
177178
logger.info("Created Xbox session!");
178179
} catch (SessionCreationException | SessionUpdateException e) {
179180
logger.error("Failed to create xbox session!", e);
@@ -197,6 +198,7 @@ private void tick() {
197198
try {
198199
sessionInfo.setPlayers(this.geyserApi().onlineConnections().size());
199200
sessionManager.updateSession(sessionInfo);
201+
sessionManager.updatePresence();
200202
} catch (SessionUpdateException e) {
201203
logger.error("Failed to update session information!", e);
202204
}

bootstrap/geyser/src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ remote-port: auto
88

99
# The amount of time in seconds to update session information and
1010
# sync other data
11+
# Warning: This can be no lower than 20 due to xbox rate limits
1112
update-interval: 30
1213

1314
# Should Xbox Live friends automatically be whitelisted

bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private static void createSession() throws SessionCreationException, SessionUpda
9292
logger.info("Creating session...");
9393

9494
sessionManager.createSession(sessionInfo);
95+
sessionManager.updatePresence();
9596

9697
logger.info("Created session!");
9798

@@ -104,6 +105,7 @@ private static void createSession() throws SessionCreationException, SessionUpda
104105

105106
// Update the session
106107
sessionManager.updateSession(sessionInfo);
108+
sessionManager.updatePresence();
107109
logger.info("Updated session!");
108110
} catch (SessionUpdateException e) {
109111
logger.error("Failed to update session", e);

bootstrap/standalone/src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Core session settings
22
session:
33
# The amount of time in seconds to update session information
4+
# Warning: This can be no lower than 20 due to xbox rate limits
45
update-interval: 30
56

67
# Should we query the bedrock server to sync the session information

core/src/main/java/com/rtm516/mcxboxbroadcast/core/Constants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class Constants {
2222
public static final URI RTA_WEBSOCKET = URI.create("wss://rta.xboxlive.com/connect");
2323
public static final URI CREATE_HANDLE = URI.create("https://sessiondirectory.xboxlive.com/handles");
2424

25-
public static final String PEOPLE = "https://social.xboxlive.com/users/me/people";
25+
public static final String PEOPLE = "https://social.xboxlive.com/users/me/people/xuid(%s)";
26+
public static final String USER_PRESENCE = "https://userpresence.xboxlive.com/users/xuid(%s)/devices/current/titles/current";
2627
public static final URI FOLLOWERS = URI.create("https://peoplehub.xboxlive.com/users/me/people/followers");
2728
public static final URI SOCIAL = URI.create("https://peoplehub.xboxlive.com/users/me/people/social");
2829
/**

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public List<FollowerResponse.Person> getXboxFriends() throws XboxFriendsExceptio
358358
*/
359359
public boolean addXboxFriend(String xuid) {
360360
HttpRequest xboxFriendRequest = HttpRequest.newBuilder()
361-
.uri(URI.create(Constants.PEOPLE + "/xuid(" + xuid + ")"))
361+
.uri(URI.create(Constants.PEOPLE.formatted(xuid)))
362362
.header("Authorization", getTokenHeader())
363363
.PUT(HttpRequest.BodyPublishers.noBody())
364364
.build();
@@ -379,7 +379,7 @@ public boolean addXboxFriend(String xuid) {
379379
*/
380380
public boolean removeXboxFriend(String xuid) {
381381
HttpRequest xboxFriendRequest = HttpRequest.newBuilder()
382-
.uri(URI.create(Constants.PEOPLE + "/xuid(" + xuid + ")"))
382+
.uri(URI.create(Constants.PEOPLE.formatted(xuid)))
383383
.header("Authorization", getTokenHeader())
384384
.DELETE()
385385
.build();
@@ -466,4 +466,24 @@ public void dumpSession() {
466466

467467
logger.info("Dumped session responses to 'lastSessionResponse.json' and 'currentSessionResponse.json'");
468468
}
469+
470+
public void updatePresence() {
471+
HttpRequest updatePresenceRequest = HttpRequest.newBuilder()
472+
.uri(URI.create(Constants.USER_PRESENCE.formatted(getXboxToken().userXUID)))
473+
.header("Content-Type", "application/json")
474+
.header("Authorization", getTokenHeader())
475+
.header("x-xbl-contract-version", "3")
476+
.POST(HttpRequest.BodyPublishers.ofString("{\"state\": \"active\"}"))
477+
.build();
478+
479+
try {
480+
HttpResponse<Void> updatePresenceResponse = httpClient.send(updatePresenceRequest, HttpResponse.BodyHandlers.discarding());
481+
482+
if (updatePresenceResponse.statusCode() != 200) {
483+
logger.error("Failed to update presence, got status " + updatePresenceResponse.statusCode());
484+
}
485+
} catch (IOException | InterruptedException e) {
486+
throw new RuntimeException(e);
487+
}
488+
}
469489
}

0 commit comments

Comments
 (0)