| 1 | package com.mojang.realmsclient.client;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 7 | import com.mojang.realmsclient.dto.BackupList;
|
| 8 | import com.mojang.realmsclient.dto.GuardedSerializer;
|
| 9 | import com.mojang.realmsclient.dto.InviteCodeList;
|
| 10 | import com.mojang.realmsclient.dto.InviteCodeRequestDto;
|
| 11 | import com.mojang.realmsclient.dto.Ops;
|
| 12 | import com.mojang.realmsclient.dto.OutboundPlayer;
|
| 13 | import com.mojang.realmsclient.dto.PendingInvite;
|
| 14 | import com.mojang.realmsclient.dto.PendingInvitesList;
|
| 15 | import com.mojang.realmsclient.dto.PingResult;
|
| 16 | import com.mojang.realmsclient.dto.PlayerInfo;
|
| 17 | import com.mojang.realmsclient.dto.PreferredRegionsDto;
|
| 18 | import com.mojang.realmsclient.dto.RealmTierConfigurationDto;
|
| 19 | import com.mojang.realmsclient.dto.RealmsConfigurationDto;
|
| 20 | import com.mojang.realmsclient.dto.RealmsDescriptionDto;
|
| 21 | import com.mojang.realmsclient.dto.RealmsJoinInformation;
|
| 22 | import com.mojang.realmsclient.dto.RealmsNews;
|
| 23 | import com.mojang.realmsclient.dto.RealmsNotification;
|
| 24 | import com.mojang.realmsclient.dto.RealmsRegion;
|
| 25 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 26 | import com.mojang.realmsclient.dto.RealmsServerList;
|
| 27 | import com.mojang.realmsclient.dto.RealmsServerPlayerLists;
|
| 28 | import com.mojang.realmsclient.dto.RealmsSetting;
|
| 29 | import com.mojang.realmsclient.dto.RealmsSlotUpdateDto;
|
| 30 | import com.mojang.realmsclient.dto.RealmsWorldOptions;
|
| 31 | import com.mojang.realmsclient.dto.RealmsWorldResetDto;
|
| 32 | import com.mojang.realmsclient.dto.RegionDataDto;
|
| 33 | import com.mojang.realmsclient.dto.RegionSelectionPreference;
|
| 34 | import com.mojang.realmsclient.dto.RegionSelectionPreferenceDto;
|
| 35 | import com.mojang.realmsclient.dto.Subscription;
|
| 36 | import com.mojang.realmsclient.dto.UploadInfo;
|
| 37 | import com.mojang.realmsclient.dto.WorldDownload;
|
| 38 | import com.mojang.realmsclient.dto.WorldTemplatePaginatedList;
|
| 39 | import com.mojang.realmsclient.exception.RealmsHttpException;
|
| 40 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 41 | import com.mojang.realmsclient.exception.RetryCallException;
|
| 42 | import com.mojang.realmsclient.util.UploadTokenCache;
|
| 43 | import com.mojang.util.UndashedUuid;
|
| 44 | import java.net.URI;
|
| 45 | import java.net.URISyntaxException;
|
| 46 | import java.time.Instant;
|
| 47 | import java.util.List;
|
| 48 | import java.util.Locale;
|
| 49 | import java.util.Optional;
|
| 50 | import java.util.Set;
|
| 51 | import java.util.UUID;
|
| 52 | import java.util.concurrent.CompletableFuture;
|
| 53 | import java.util.stream.Collectors;
|
| 54 | import net.fabricmc.api.EnvType;
|
| 55 | import net.minecraft.SharedConstants;
|
| 56 | import net.minecraft.WorldVersion;
|
| 57 | import net.minecraft.client.Minecraft;
|
| 58 | import net.minecraft.util.LenientJsonParser;
|
| 59 | import net.minecraft.util.Util;
|
| 60 | import org.jspecify.annotations.Nullable;
|
| 61 | import org.slf4j.Logger;
|
| 62 |
|
| 63 | @net.fabricmc.api.Environment(EnvType.CLIENT)
|
| 64 | public class RealmsClient {
|
| 65 | public static final RealmsClient.Environment ENVIRONMENT = Optional.ofNullable(System.getenv("realms.environment"))
|
| 66 | .or(() -> Optional.ofNullable(System.getProperty("realms.environment")))
|
| 67 | .flatMap(RealmsClient.Environment::byName)
|
| 68 | .orElse(RealmsClient.Environment.PRODUCTION);
|
| 69 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 70 | private static volatile @Nullable RealmsClient realmsClientInstance = null;
|
| 71 | private final CompletableFuture<Set<String>> featureFlags;
|
| 72 | private final String sessionId;
|
| 73 | private final String username;
|
| 74 | private final Minecraft minecraft;
|
| 75 | private static final String WORLDS_RESOURCE_PATH = "worlds";
|
| 76 | private static final String INVITES_RESOURCE_PATH = "invites";
|
| 77 | private static final String MCO_RESOURCE_PATH = "mco";
|
| 78 | private static final String SUBSCRIPTION_RESOURCE = "subscriptions";
|
| 79 | private static final String ACTIVITIES_RESOURCE = "activities";
|
| 80 | private static final String OPS_RESOURCE = "ops";
|
| 81 | private static final String REGIONS_RESOURCE = "regions/ping/stat";
|
| 82 | private static final String PREFERRED_REGION_RESOURCE = "regions/preferredRegions";
|
| 83 | private static final String TRIALS_RESOURCE = "trial";
|
| 84 | private static final String NOTIFICATIONS_RESOURCE = "notifications";
|
| 85 | private static final String FEATURE_FLAGS_RESOURCE = "feature/v1";
|
| 86 | private static final String INVITE_CODES_RESOURCE = "api/v1.0/invite/codes";
|
| 87 | private static final String ACCEPT_HEADER = "Accept";
|
| 88 | private static final String APPLICATION_JSON = "application/json";
|
| 89 | private static final String MOJANG_AUTH_HEADER = "x-mojangauth";
|
| 90 | private static final String REALMS_PLATFORM_HEADER = "X-Realms-Platform";
|
| 91 | private static final String PATH_INVITE_CODES_UPDATE = "/update";
|
| 92 | private static final String PATH_INVITE_CODES_DELETE = "/delete/$CODE";
|
| 93 | private static final String PATH_INVITE_CODES_USE = "/use";
|
| 94 | private static final String PATH_LIST_ALL_REALMS = "/listUserWorldsOfType/any";
|
| 95 | private static final String PATH_CREATE_SNAPSHOT_REALM = "/$PARENT_WORLD_ID/createPrereleaseRealm";
|
| 96 | private static final String PATH_SNAPSHOT_ELIGIBLE_REALMS = "/listPrereleaseEligibleWorlds";
|
| 97 | private static final String PATH_INITIALIZE = "/$WORLD_ID/initialize";
|
| 98 | private static final String PATH_GET_LIVESTATS = "/liveplayerlist";
|
| 99 | private static final String PATH_GET_SUBSCRIPTION = "/$WORLD_ID";
|
| 100 | private static final String PATH_OP = "/$WORLD_ID/$PROFILE_UUID";
|
| 101 | private static final String PATH_PUT_INTO_MINIGAMES_MODE = "/minigames/$MINIGAME_ID/$WORLD_ID";
|
| 102 | private static final String PATH_AVAILABLE = "/available";
|
| 103 | private static final String PATH_TEMPLATES = "/templates/$WORLD_TYPE";
|
| 104 | private static final String PATH_WORLD_JOIN = "/v1/$ID/join/pc";
|
| 105 | private static final String PATH_WORLD_GET = "/$ID";
|
| 106 | private static final String PATH_WORLD_INVITES = "/$WORLD_ID";
|
| 107 | private static final String PATH_WORLD_UNINVITE = "/$WORLD_ID/invite/$UUID";
|
| 108 | private static final String PATH_PENDING_INVITES = "/pending";
|
| 109 | private static final String PATH_ACCEPT_INVITE = "/accept/$INVITATION_ID";
|
| 110 | private static final String PATH_REJECT_INVITE = "/reject/$INVITATION_ID";
|
| 111 | private static final String PATH_UNINVITE_MYSELF = "/$WORLD_ID";
|
| 112 | private static final String PATH_WORLD_CONFIGURE = "/$WORLD_ID/configuration";
|
| 113 | private static final String PATH_WORLD_TIER_CONFIGURATION = "/$WORLD_ID/configuration/tier";
|
| 114 | private static final String PATH_WORLD_RENDER_DISTANCE = "/$WORLD_ID/configuration/renderDistance/$RENDER_DISTANCE";
|
| 115 | private static final String PATH_WORLD_SIM_DISTANCE = "/$WORLD_ID/configuration/simDistance/$SIM_DISTANCE";
|
| 116 | private static final String PATH_SLOT = "/$WORLD_ID/slot/$SLOT_ID";
|
| 117 | private static final String PATH_WORLD_OPEN = "/$WORLD_ID/open";
|
| 118 | private static final String PATH_WORLD_CLOSE = "/$WORLD_ID/close";
|
| 119 | private static final String PATH_WORLD_RESET = "/$WORLD_ID/reset";
|
| 120 | private static final String PATH_DELETE_WORLD = "/$WORLD_ID";
|
| 121 | private static final String PATH_WORLD_BACKUPS = "/$WORLD_ID/backups";
|
| 122 | private static final String PATH_WORLD_DOWNLOAD = "/$WORLD_ID/slot/$SLOT_ID/download";
|
| 123 | private static final String PATH_WORLD_UPLOAD = "/$WORLD_ID/backups/upload";
|
| 124 | private static final String PATH_CLIENT_COMPATIBLE = "/client/compatible";
|
| 125 | private static final String PATH_TOS_AGREED = "/tos/agreed";
|
| 126 | private static final String PATH_NEWS = "/v1/news";
|
| 127 | private static final String PATH_MARK_NOTIFICATIONS_SEEN = "/seen";
|
| 128 | private static final String PATH_DISMISS_NOTIFICATIONS = "/dismiss";
|
| 129 | private static final GuardedSerializer GSON = new GuardedSerializer();
|
| 130 |
|
| 131 | public static RealmsClient getOrCreate() {
|
| 132 | Minecraft minecraft = Minecraft.getInstance();
|
| 133 | return getOrCreate(minecraft);
|
| 134 | }
|
| 135 |
|
| 136 | public static RealmsClient getOrCreate(final Minecraft minecraft) {
|
| 137 | String username = minecraft.getUser().getName();
|
| 138 | String sessionId = minecraft.getUser().getSessionId();
|
| 139 | RealmsClient realmsClient = realmsClientInstance;
|
| 140 | if (realmsClient != null) {
|
| 141 | return realmsClient;
|
| 142 | }
|
| 143 |
|
| 144 | synchronized (RealmsClient.class) {
|
| 145 | RealmsClient rc = realmsClientInstance;
|
| 146 | if (rc != null) {
|
| 147 | return rc;
|
| 148 | }
|
| 149 |
|
| 150 | rc = new RealmsClient(sessionId, username, minecraft);
|
| 151 | realmsClientInstance = rc;
|
| 152 | return rc;
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | private RealmsClient(final String sessionId, final String username, final Minecraft minecraft) {
|
| 157 | this.sessionId = sessionId;
|
| 158 | this.username = username;
|
| 159 | this.minecraft = minecraft;
|
| 160 | RealmsClientConfig.setProxy(minecraft.getProxy());
|
| 161 | this.featureFlags = CompletableFuture.supplyAsync(this::fetchFeatureFlags, Util.nonCriticalIoPool());
|
| 162 | }
|
| 163 |
|
| 164 | public Set<String> getFeatureFlags() {
|
| 165 | return this.featureFlags.join();
|
| 166 | }
|
| 167 |
|
| 168 | private Set<String> fetchFeatureFlags() {
|
| 169 | if (Minecraft.getInstance().isOfflineDeveloperMode()) {
|
| 170 | return Set.of();
|
| 171 | }
|
| 172 |
|
| 173 | String asciiUrl = url("feature/v1", null, false);
|
| 174 |
|
| 175 | try {
|
| 176 | String returnJson = this.execute(Request.get(asciiUrl, 5000, 10000));
|
| 177 | JsonArray object = LenientJsonParser.parse(returnJson).getAsJsonArray();
|
| 178 | Set<String> featureFlags = object.asList().stream().map(JsonElement::getAsString).collect(Collectors.toSet());
|
| 179 | LOGGER.debug("Fetched Realms feature flags: {}", featureFlags);
|
| 180 | return featureFlags;
|
| 181 | } catch (RealmsServiceException e) {
|
| 182 | LOGGER.error("Failed to fetch Realms feature flags", e);
|
| 183 | } catch (Exception e) {
|
| 184 | LOGGER.error("Could not parse Realms feature flags", e);
|
| 185 | }
|
| 186 |
|
| 187 | return Set.of();
|
| 188 | }
|
| 189 |
|
| 190 | public RealmsServerList listRealms() throws RealmsServiceException {
|
| 191 | String asciiUrl = this.url("worlds");
|
| 192 | if (RealmsMainScreen.isSnapshot()) {
|
| 193 | asciiUrl = asciiUrl + "/listUserWorldsOfType/any";
|
| 194 | }
|
| 195 |
|
| 196 | String json = this.execute(Request.get(asciiUrl));
|
| 197 | return RealmsServerList.parse(GSON, json);
|
| 198 | }
|
| 199 |
|
| 200 | public List<RealmsServer> listSnapshotEligibleRealms() throws RealmsServiceException {
|
| 201 | String asciiUrl = this.url("worlds/listPrereleaseEligibleWorlds");
|
| 202 | String json = this.execute(Request.get(asciiUrl));
|
| 203 | return RealmsServerList.parse(GSON, json).servers();
|
| 204 | }
|
| 205 |
|
| 206 | public RealmsServer createSnapshotRealm(final Long parentId) throws RealmsServiceException {
|
| 207 | String parentIdString = String.valueOf(parentId);
|
| 208 | String url = this.url("worlds" + "/$PARENT_WORLD_ID/createPrereleaseRealm".replace("$PARENT_WORLD_ID", parentIdString));
|
| 209 | return RealmsServer.parse(GSON, this.execute(Request.post(url, parentIdString)));
|
| 210 | }
|
| 211 |
|
| 212 | public List<RealmsNotification> getNotifications() throws RealmsServiceException {
|
| 213 | String endpoint = this.url("notifications");
|
| 214 | String responseJson = this.execute(Request.get(endpoint));
|
| 215 | return RealmsNotification.parseList(responseJson);
|
| 216 | }
|
| 217 |
|
| 218 | private static JsonArray uuidListToJsonArray(final List<UUID> uuids) {
|
| 219 | JsonArray array = new JsonArray();
|
| 220 |
|
| 221 | for (UUID uuid : uuids) {
|
| 222 | if (uuid != null) {
|
| 223 | array.add(uuid.toString());
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | return array;
|
| 228 | }
|
| 229 |
|
| 230 | public void notificationsSeen(final List<UUID> notificationUuids) throws RealmsServiceException {
|
| 231 | String endpoint = this.url("notifications/seen");
|
| 232 | this.execute(Request.post(endpoint, GSON.toJson(uuidListToJsonArray(notificationUuids))));
|
| 233 | }
|
| 234 |
|
| 235 | public void notificationsDismiss(final List<UUID> notificationUuids) throws RealmsServiceException {
|
| 236 | String endpoint = this.url("notifications/dismiss");
|
| 237 | this.execute(Request.post(endpoint, GSON.toJson(uuidListToJsonArray(notificationUuids))));
|
| 238 | }
|
| 239 |
|
| 240 | public RealmsServer getOwnRealm(final long realmId) throws RealmsServiceException {
|
| 241 | String asciiUrl = this.url("worlds" + "/$ID".replace("$ID", String.valueOf(realmId)));
|
| 242 | String json = this.execute(Request.get(asciiUrl));
|
| 243 | return RealmsServer.parse(GSON, json);
|
| 244 | }
|
| 245 |
|
| 246 | public RealmTierConfigurationDto getRealmTierConfiguration(final long realmId) throws RealmsServiceException {
|
| 247 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/configuration/tier".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 248 | String json = this.execute(Request.get(asciiUrl));
|
| 249 |
|
| 250 | try {
|
| 251 | RealmTierConfigurationDto tierConfiguration = GSON.fromJson(json, RealmTierConfigurationDto.class);
|
| 252 | if (tierConfiguration != null && tierConfiguration.renderDistance() != null && tierConfiguration.simDistance() != null) {
|
| 253 | return tierConfiguration;
|
| 254 | }
|
| 255 |
|
| 256 | LOGGER.error("Could not parse Realm tier configuration: {}", json);
|
| 257 | } catch (Exception e) {
|
| 258 | LOGGER.error("Could not parse Realm tier configuration", e);
|
| 259 | }
|
| 260 |
|
| 261 | throw new RealmsServiceException(RealmsError.CustomError.configurationError());
|
| 262 | }
|
| 263 |
|
| 264 | public void updateRenderDistance(final long realmId, final int renderDistance) throws RealmsServiceException {
|
| 265 | String asciiUrl = this.url(
|
| 266 | "worlds"
|
| 267 | + "/$WORLD_ID/configuration/renderDistance/$RENDER_DISTANCE"
|
| 268 | .replace("$WORLD_ID", String.valueOf(realmId))
|
| 269 | .replace("$RENDER_DISTANCE", String.valueOf(renderDistance))
|
| 270 | );
|
| 271 | this.execute(Request.put(asciiUrl, ""));
|
| 272 | }
|
| 273 |
|
| 274 | public void updateSimulationDistance(final long realmId, final int simulationDistance) throws RealmsServiceException {
|
| 275 | String asciiUrl = this.url(
|
| 276 | "worlds"
|
| 277 | + "/$WORLD_ID/configuration/simDistance/$SIM_DISTANCE"
|
| 278 | .replace("$WORLD_ID", String.valueOf(realmId))
|
| 279 | .replace("$SIM_DISTANCE", String.valueOf(simulationDistance))
|
| 280 | );
|
| 281 | this.execute(Request.put(asciiUrl, ""));
|
| 282 | }
|
| 283 |
|
| 284 | public PreferredRegionsDto getPreferredRegionSelections() throws RealmsServiceException {
|
| 285 | String asciiUrl = this.url("regions/preferredRegions");
|
| 286 | String json = this.execute(Request.get(asciiUrl));
|
| 287 |
|
| 288 | try {
|
| 289 | PreferredRegionsDto preferredRegionsDto = GSON.fromJson(json, PreferredRegionsDto.class);
|
| 290 | if (preferredRegionsDto == null) {
|
| 291 | return PreferredRegionsDto.empty();
|
| 292 | }
|
| 293 |
|
| 294 | Set<RealmsRegion> regionsInResponse = preferredRegionsDto.regionData().stream().map(RegionDataDto::region).collect(Collectors.toSet());
|
| 295 |
|
| 296 | for (RealmsRegion region : RealmsRegion.values()) {
|
| 297 | if (region != RealmsRegion.INVALID_REGION && !regionsInResponse.contains(region)) {
|
| 298 | LOGGER.debug("No realms region matching {} in server response", region);
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | return preferredRegionsDto;
|
| 303 | } catch (Exception e) {
|
| 304 | LOGGER.error("Could not parse PreferredRegionSelections", e);
|
| 305 | return PreferredRegionsDto.empty();
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | public RealmsServerPlayerLists getLiveStats() throws RealmsServiceException {
|
| 310 | String asciiUrl = this.url("activities/liveplayerlist");
|
| 311 | String json = this.execute(Request.get(asciiUrl));
|
| 312 | return RealmsServerPlayerLists.parse(json);
|
| 313 | }
|
| 314 |
|
| 315 | public RealmsJoinInformation join(final long realmId) throws RealmsServiceException {
|
| 316 | String asciiUrl = this.url("worlds" + "/v1/$ID/join/pc".replace("$ID", realmId + ""));
|
| 317 | String json = this.execute(Request.get(asciiUrl, 5000, 30000));
|
| 318 | return RealmsJoinInformation.parse(GSON, json);
|
| 319 | }
|
| 320 |
|
| 321 | public void initializeRealm(final long realmId, final String name, final String motd) throws RealmsServiceException {
|
| 322 | RealmsDescriptionDto realmsDescription = new RealmsDescriptionDto(name, motd);
|
| 323 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/initialize".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 324 | String json = GSON.toJson(realmsDescription);
|
| 325 | this.execute(Request.post(asciiUrl, json, 5000, 10000));
|
| 326 | }
|
| 327 |
|
| 328 | public boolean hasParentalConsent() throws RealmsServiceException {
|
| 329 | String asciiUrl = this.url("mco/available");
|
| 330 | String json = this.execute(Request.get(asciiUrl));
|
| 331 | return Boolean.parseBoolean(json);
|
| 332 | }
|
| 333 |
|
| 334 | public RealmsClient.CompatibleVersionResponse clientCompatible() throws RealmsServiceException {
|
| 335 | String asciiUrl = this.url("mco/client/compatible");
|
| 336 | String response = this.execute(Request.get(asciiUrl));
|
| 337 |
|
| 338 | try {
|
| 339 | return RealmsClient.CompatibleVersionResponse.valueOf(response);
|
| 340 | } catch (IllegalArgumentException ignored) {
|
| 341 | throw new RealmsServiceException(RealmsError.CustomError.unknownCompatibilityResponse(response));
|
| 342 | }
|
| 343 | }
|
| 344 |
|
| 345 | public void uninvite(final long realmId, final UUID profileId) throws RealmsServiceException {
|
| 346 | String asciiUrl = this.url(
|
| 347 | "invites" + "/$WORLD_ID/invite/$UUID".replace("$WORLD_ID", String.valueOf(realmId)).replace("$UUID", UndashedUuid.toString(profileId))
|
| 348 | );
|
| 349 | this.execute(Request.delete(asciiUrl));
|
| 350 | }
|
| 351 |
|
| 352 | public void uninviteMyselfFrom(final long realmId) throws RealmsServiceException {
|
| 353 | String asciiUrl = this.url("invites" + "/$WORLD_ID".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 354 | this.execute(Request.delete(asciiUrl));
|
| 355 | }
|
| 356 |
|
| 357 | public List<PlayerInfo> invite(final long realmId, final String profileName) throws RealmsServiceException {
|
| 358 | OutboundPlayer playerInfo = new OutboundPlayer();
|
| 359 | playerInfo.name = profileName;
|
| 360 | String asciiUrl = this.url("invites" + "/$WORLD_ID".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 361 | String json = this.execute(Request.post(asciiUrl, GSON.toJson(playerInfo)));
|
| 362 | return RealmsServer.parse(GSON, json).players;
|
| 363 | }
|
| 364 |
|
| 365 | public InviteCodeList inviteCodes(final long realmId) throws RealmsServiceException {
|
| 366 | Request<?> request = this.inviteCodesRequest(realmId);
|
| 367 | String json = this.execute(request);
|
| 368 | return InviteCodeList.parse(json);
|
| 369 | }
|
| 370 |
|
| 371 | private Request<?> inviteCodesRequest(final long realmId) {
|
| 372 | String asciiUrl = frontendV2Url("api/v1.0/invite/codes", "realmId=" + realmId);
|
| 373 | return frontendV2Request(Request.get(asciiUrl), this.minecraft.getUser().getAccessToken());
|
| 374 | }
|
| 375 |
|
| 376 | private static Request<?> frontendV2Request(final Request<?> request, final String accessToken) {
|
| 377 | request.connection.setRequestProperty("x-mojangauth", accessToken);
|
| 378 | if (ENVIRONMENT == RealmsClient.Environment.LOCAL) {
|
| 379 | request.connection.setRequestProperty("X-Realms-Platform", "Java");
|
| 380 | }
|
| 381 |
|
| 382 | return request;
|
| 383 | }
|
| 384 |
|
| 385 | public void createInviteCode(final long realmId, final boolean enabled, final @Nullable Instant expiration) throws RealmsServiceException {
|
| 386 | InviteCodeRequestDto request = new InviteCodeRequestDto(null, realmId, enabled, expiration == null ? null : expiration.toEpochMilli());
|
| 387 | String asciiUrl = frontendV2Url("api/v1.0/invite/codes", null);
|
| 388 | this.execute(frontendV2Request(Request.post(asciiUrl, GSON.toJson(request)), this.minecraft.getUser().getAccessToken()));
|
| 389 | }
|
| 390 |
|
| 391 | public void updateInviteCode(final long realmId, final String code, final boolean enabled, final @Nullable Instant expiration) throws RealmsServiceException {
|
| 392 | InviteCodeRequestDto request = new InviteCodeRequestDto(code, realmId, enabled, expiration == null ? null : expiration.toEpochMilli());
|
| 393 | String asciiUrl = frontendV2Url("api/v1.0/invite/codes/update", null);
|
| 394 | this.execute(frontendV2Request(Request.post(asciiUrl, GSON.toJson(request)), this.minecraft.getUser().getAccessToken()));
|
| 395 | }
|
| 396 |
|
| 397 | public void deleteInviteCode(final String code) throws RealmsServiceException {
|
| 398 | String asciiUrl = frontendV2Url("api/v1.0/invite/codes" + "/delete/$CODE".replace("$CODE", code), null);
|
| 399 | this.execute(frontendV2Request(Request.delete(asciiUrl), this.minecraft.getUser().getAccessToken()));
|
| 400 | }
|
| 401 |
|
| 402 | public void redeemInviteCode(final String code) throws RealmsServiceException {
|
| 403 | String asciiUrl = frontendV2Url("api/v1.0/invite/codes/use", null) + "/" + code.trim();
|
| 404 | Request<?> request = frontendV2Request(Request.post(asciiUrl, ""), this.minecraft.getUser().getAccessToken());
|
| 405 | request.connection.setRequestProperty("Accept", "application/json");
|
| 406 | this.execute(request);
|
| 407 | }
|
| 408 |
|
| 409 | public BackupList backupsFor(final long realmId) throws RealmsServiceException {
|
| 410 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/backups".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 411 | String json = this.execute(Request.get(asciiUrl));
|
| 412 | return BackupList.parse(json);
|
| 413 | }
|
| 414 |
|
| 415 | public void updateConfiguration(
|
| 416 | final long realmId,
|
| 417 | final String name,
|
| 418 | final String description,
|
| 419 | final @Nullable RegionSelectionPreferenceDto regionSelectionPreference,
|
| 420 | final int slotId,
|
| 421 | final RealmsWorldOptions options,
|
| 422 | final List<RealmsSetting> settings
|
| 423 | ) throws RealmsServiceException {
|
| 424 | RegionSelectionPreferenceDto preferenceDto = regionSelectionPreference != null
|
| 425 | ? regionSelectionPreference
|
| 426 | : new RegionSelectionPreferenceDto(RegionSelectionPreference.DEFAULT_SELECTION, null);
|
| 427 | RealmsDescriptionDto realmsDescription = new RealmsDescriptionDto(name, description);
|
| 428 | RealmsSlotUpdateDto slotUpdateDto = new RealmsSlotUpdateDto(slotId, options, RealmsSetting.isHardcore(settings));
|
| 429 | RealmsConfigurationDto realmsConfiguration = new RealmsConfigurationDto(slotUpdateDto, settings, preferenceDto, realmsDescription);
|
| 430 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/configuration".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 431 | this.execute(Request.post(asciiUrl, GSON.toJson(realmsConfiguration)));
|
| 432 | }
|
| 433 |
|
| 434 | public void updateSlot(final long realmId, final int slotId, final RealmsWorldOptions options, final List<RealmsSetting> settings) throws RealmsServiceException {
|
| 435 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/slot/$SLOT_ID".replace("$WORLD_ID", String.valueOf(realmId)).replace("$SLOT_ID", String.valueOf(slotId)));
|
| 436 | String json = GSON.toJson(new RealmsSlotUpdateDto(slotId, options, RealmsSetting.isHardcore(settings)));
|
| 437 | this.execute(Request.post(asciiUrl, json));
|
| 438 | }
|
| 439 |
|
| 440 | public boolean switchSlot(final long realmId, final int slot) throws RealmsServiceException {
|
| 441 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/slot/$SLOT_ID".replace("$WORLD_ID", String.valueOf(realmId)).replace("$SLOT_ID", String.valueOf(slot)));
|
| 442 | String json = this.execute(Request.put(asciiUrl, ""));
|
| 443 | return Boolean.valueOf(json);
|
| 444 | }
|
| 445 |
|
| 446 | public void restoreWorld(final long realmId, final String backupId) throws RealmsServiceException {
|
| 447 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/backups".replace("$WORLD_ID", String.valueOf(realmId)), "backupId=" + backupId);
|
| 448 | this.execute(Request.put(asciiUrl, "", 40000, 600000));
|
| 449 | }
|
| 450 |
|
| 451 | public WorldTemplatePaginatedList fetchWorldTemplates(final int page, final int pageSize, final RealmsServer.WorldType type) throws RealmsServiceException {
|
| 452 | String asciiUrl = this.url(
|
| 453 | "worlds" + "/templates/$WORLD_TYPE".replace("$WORLD_TYPE", type.toString()), String.format(Locale.ROOT, "page=%d&pageSize=%d", page, pageSize)
|
| 454 | );
|
| 455 | String json = this.execute(Request.get(asciiUrl));
|
| 456 | return WorldTemplatePaginatedList.parse(json);
|
| 457 | }
|
| 458 |
|
| 459 | public Boolean putIntoMinigameMode(final long realmId, final String minigameId) throws RealmsServiceException {
|
| 460 | String path = "/minigames/$MINIGAME_ID/$WORLD_ID".replace("$MINIGAME_ID", minigameId).replace("$WORLD_ID", String.valueOf(realmId));
|
| 461 | String asciiUrl = this.url("worlds" + path);
|
| 462 | return Boolean.valueOf(this.execute(Request.put(asciiUrl, "")));
|
| 463 | }
|
| 464 |
|
| 465 | public Ops op(final long realmId, final UUID profileId) throws RealmsServiceException {
|
| 466 | String path = "/$WORLD_ID/$PROFILE_UUID".replace("$WORLD_ID", String.valueOf(realmId)).replace("$PROFILE_UUID", UndashedUuid.toString(profileId));
|
| 467 | String asciiUrl = this.url("ops" + path);
|
| 468 | return Ops.parse(this.execute(Request.post(asciiUrl, "")));
|
| 469 | }
|
| 470 |
|
| 471 | public Ops deop(final long realmId, final UUID profileId) throws RealmsServiceException {
|
| 472 | String path = "/$WORLD_ID/$PROFILE_UUID".replace("$WORLD_ID", String.valueOf(realmId)).replace("$PROFILE_UUID", UndashedUuid.toString(profileId));
|
| 473 | String asciiUrl = this.url("ops" + path);
|
| 474 | return Ops.parse(this.execute(Request.delete(asciiUrl)));
|
| 475 | }
|
| 476 |
|
| 477 | public Boolean open(final long realmId) throws RealmsServiceException {
|
| 478 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/open".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 479 | String json = this.execute(Request.put(asciiUrl, ""));
|
| 480 | return Boolean.valueOf(json);
|
| 481 | }
|
| 482 |
|
| 483 | public Boolean close(final long realmId) throws RealmsServiceException {
|
| 484 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/close".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 485 | String json = this.execute(Request.put(asciiUrl, ""));
|
| 486 | return Boolean.valueOf(json);
|
| 487 | }
|
| 488 |
|
| 489 | public Boolean resetWorldWithTemplate(final long realmId, final String worldTemplateId) throws RealmsServiceException {
|
| 490 | RealmsWorldResetDto worldReset = new RealmsWorldResetDto(null, Long.valueOf(worldTemplateId), -1, false, Set.of());
|
| 491 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/reset".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 492 | String json = this.execute(Request.post(asciiUrl, GSON.toJson(worldReset), 30000, 80000));
|
| 493 | return Boolean.valueOf(json);
|
| 494 | }
|
| 495 |
|
| 496 | public Subscription subscriptionFor(final long realmId) throws RealmsServiceException {
|
| 497 | String asciiUrl = this.url("subscriptions" + "/$WORLD_ID".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 498 | String json = this.execute(Request.get(asciiUrl));
|
| 499 | return Subscription.parse(json);
|
| 500 | }
|
| 501 |
|
| 502 | public int pendingInvitesCount() throws RealmsServiceException {
|
| 503 | return this.pendingInvites().pendingInvites().size();
|
| 504 | }
|
| 505 |
|
| 506 | public PendingInvitesList pendingInvites() throws RealmsServiceException {
|
| 507 | String asciiUrl = this.url("invites/pending");
|
| 508 | String json = this.execute(Request.get(asciiUrl));
|
| 509 | PendingInvitesList list = PendingInvitesList.parse(json);
|
| 510 | list.pendingInvites().removeIf(this::isBlocked);
|
| 511 | return list;
|
| 512 | }
|
| 513 |
|
| 514 | private boolean isBlocked(final PendingInvite invite) {
|
| 515 | return this.minecraft.getPlayerSocialManager().isBlocked(invite.realmOwnerUuid());
|
| 516 | }
|
| 517 |
|
| 518 | public void acceptInvitation(final String invitationId) throws RealmsServiceException {
|
| 519 | String asciiUrl = this.url("invites" + "/accept/$INVITATION_ID".replace("$INVITATION_ID", invitationId));
|
| 520 | this.execute(Request.put(asciiUrl, ""));
|
| 521 | }
|
| 522 |
|
| 523 | public WorldDownload requestDownloadInfo(final long realmId, final int slotId) throws RealmsServiceException {
|
| 524 | String asciiUrl = this.url(
|
| 525 | "worlds" + "/$WORLD_ID/slot/$SLOT_ID/download".replace("$WORLD_ID", String.valueOf(realmId)).replace("$SLOT_ID", String.valueOf(slotId))
|
| 526 | );
|
| 527 | String json = this.execute(Request.get(asciiUrl));
|
| 528 | return WorldDownload.parse(json);
|
| 529 | }
|
| 530 |
|
| 531 | public @Nullable UploadInfo requestUploadInfo(final long realmId) throws RealmsServiceException {
|
| 532 | String asciiUrl = this.url("worlds" + "/$WORLD_ID/backups/upload".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 533 | String uploadToken = UploadTokenCache.get(realmId);
|
| 534 | UploadInfo uploadInfo = UploadInfo.parse(this.execute(Request.put(asciiUrl, UploadInfo.createRequest(uploadToken))));
|
| 535 | if (uploadInfo != null) {
|
| 536 | UploadTokenCache.put(realmId, uploadInfo.token());
|
| 537 | }
|
| 538 |
|
| 539 | return uploadInfo;
|
| 540 | }
|
| 541 |
|
| 542 | public void rejectInvitation(final String invitationId) throws RealmsServiceException {
|
| 543 | String asciiUrl = this.url("invites" + "/reject/$INVITATION_ID".replace("$INVITATION_ID", invitationId));
|
| 544 | this.execute(Request.put(asciiUrl, ""));
|
| 545 | }
|
| 546 |
|
| 547 | public void agreeToTos() throws RealmsServiceException {
|
| 548 | String asciiUrl = this.url("mco/tos/agreed");
|
| 549 | this.execute(Request.post(asciiUrl, ""));
|
| 550 | }
|
| 551 |
|
| 552 | public RealmsNews getNews() throws RealmsServiceException {
|
| 553 | String asciiUrl = this.url("mco/v1/news");
|
| 554 | String returnJson = this.execute(Request.get(asciiUrl, 5000, 10000));
|
| 555 | return RealmsNews.parse(returnJson);
|
| 556 | }
|
| 557 |
|
| 558 | public void sendPingResults(final PingResult pingResult) throws RealmsServiceException {
|
| 559 | String asciiUrl = this.url("regions/ping/stat");
|
| 560 | this.execute(Request.post(asciiUrl, GSON.toJson(pingResult)));
|
| 561 | }
|
| 562 |
|
| 563 | public Boolean trialAvailable() throws RealmsServiceException {
|
| 564 | String asciiUrl = this.url("trial");
|
| 565 | String json = this.execute(Request.get(asciiUrl));
|
| 566 | return Boolean.valueOf(json);
|
| 567 | }
|
| 568 |
|
| 569 | public void deleteRealm(final long realmId) throws RealmsServiceException {
|
| 570 | String asciiUrl = this.url("worlds" + "/$WORLD_ID".replace("$WORLD_ID", String.valueOf(realmId)));
|
| 571 | this.execute(Request.delete(asciiUrl));
|
| 572 | }
|
| 573 |
|
| 574 | private String url(final String path) throws RealmsServiceException {
|
| 575 | return this.url(path, null);
|
| 576 | }
|
| 577 |
|
| 578 | private String url(final String path, final @Nullable String queryString) {
|
| 579 | return url(path, queryString, this.getFeatureFlags().contains("realms_in_aks"));
|
| 580 | }
|
| 581 |
|
| 582 | private static String url(final String path, final @Nullable String queryString, final boolean useAlternativeURL) {
|
| 583 | try {
|
| 584 | return new URI(ENVIRONMENT.protocol, useAlternativeURL ? ENVIRONMENT.alternativeUrl : ENVIRONMENT.baseUrl, "/" + path, queryString, null).toASCIIString();
|
| 585 | } catch (URISyntaxException e) {
|
| 586 | throw new IllegalArgumentException(path, e);
|
| 587 | }
|
| 588 | }
|
| 589 |
|
| 590 | private static String frontendV2Url(final String path, final @Nullable String queryString) {
|
| 591 | try {
|
| 592 | return new URI(ENVIRONMENT.protocol, ENVIRONMENT.frontendV2Url, "/" + path, queryString, null).toASCIIString();
|
| 593 | } catch (URISyntaxException e) {
|
| 594 | throw new IllegalArgumentException(path, e);
|
| 595 | }
|
| 596 | }
|
| 597 |
|
| 598 | private String execute(final Request<?> request) throws RealmsServiceException {
|
| 599 | request.cookie("sid", this.sessionId);
|
| 600 | request.cookie("user", this.username);
|
| 601 | WorldVersion version = SharedConstants.getCurrentVersion();
|
| 602 | request.cookie("version", version.name());
|
| 603 | request.addVersionHeaders(version.id(), version.protocolVersion(), RealmsMainScreen.isSnapshot());
|
| 604 |
|
| 605 | try {
|
| 606 | int responseCode = request.responseCode();
|
| 607 | if (responseCode != 503 && responseCode != 277) {
|
| 608 | String responseText = request.text();
|
| 609 | if (responseCode >= 200 && responseCode < 300) {
|
| 610 | return responseText;
|
| 611 | }
|
| 612 |
|
| 613 | if (responseCode == 401) {
|
| 614 | String authenticationHeader = request.getHeader("WWW-Authenticate");
|
| 615 | LOGGER.info("Could not authorize you against Realms server: {}", authenticationHeader);
|
| 616 | throw new RealmsServiceException(new RealmsError.AuthenticationError(authenticationHeader));
|
| 617 | }
|
| 618 |
|
| 619 | String contentType = request.connection.getContentType();
|
| 620 | if (contentType != null && contentType.startsWith("text/html")) {
|
| 621 | throw new RealmsServiceException(RealmsError.CustomError.htmlPayload(responseCode, responseText));
|
| 622 | }
|
| 623 |
|
| 624 | RealmsError error = RealmsError.parse(responseCode, responseText);
|
| 625 | throw new RealmsServiceException(error);
|
| 626 | } else {
|
| 627 | int pauseTime = request.getRetryAfterHeader();
|
| 628 | throw new RetryCallException(pauseTime, responseCode);
|
| 629 | }
|
| 630 | } catch (RealmsHttpException e) {
|
| 631 | throw new RealmsServiceException(RealmsError.CustomError.connectivityError(e));
|
| 632 | }
|
| 633 | }
|
| 634 |
|
| 635 | @net.fabricmc.api.Environment(EnvType.CLIENT)
|
| 636 | public enum CompatibleVersionResponse {
|
| 637 | COMPATIBLE,
|
| 638 | OUTDATED,
|
| 639 | OTHER;
|
| 640 | }
|
| 641 |
|
| 642 | @net.fabricmc.api.Environment(EnvType.CLIENT)
|
| 643 | public enum Environment {
|
| 644 | PRODUCTION("pc.realms.minecraft.net", "java.frontendlegacy.realms.minecraft-services.net", "java.frontend.realms.minecraft-services.net", "https"),
|
| 645 | STAGE(
|
| 646 | "pc-stage.realms.minecraft.net",
|
| 647 | "java.frontendlegacy.stage-c2a40e62.realms.minecraft-services.net",
|
| 648 | "java.frontend.stage-c2a40e62.realms.minecraft-services.net",
|
| 649 | "https"
|
| 650 | ),
|
| 651 | LOCAL("localhost:8080", "localhost:8080", "localhost:8080", "http");
|
| 652 |
|
| 653 | public final String baseUrl;
|
| 654 | public final String alternativeUrl;
|
| 655 | public final String frontendV2Url;
|
| 656 | public final String protocol;
|
| 657 |
|
| 658 | Environment(final String baseUrl, final String alternativeUrl, final String frontendV2Url, final String protocol) {
|
| 659 | this.baseUrl = baseUrl;
|
| 660 | this.alternativeUrl = alternativeUrl;
|
| 661 | this.frontendV2Url = frontendV2Url;
|
| 662 | this.protocol = protocol;
|
| 663 | }
|
| 664 |
|
| 665 | public static Optional<RealmsClient.Environment> byName(final String name) {
|
| 666 | return switch (name.toLowerCase(Locale.ROOT)) {
|
| 667 | case "production" -> Optional.of(PRODUCTION);
|
| 668 | case "local" -> Optional.of(LOCAL);
|
| 669 | case "stage", "staging" -> Optional.of(STAGE);
|
| 670 | default -> Optional.empty();
|
| 671 | };
|
| 672 | }
|
| 673 | }
|
| 674 | }
|
| 675 |
|