| 1 | package com.mojang.realmsclient.util.task;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.dto.RealmsJoinInformation;
|
| 6 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 7 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 8 | import com.mojang.realmsclient.exception.RetryCallException;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsBrokenWorldScreen;
|
| 10 | import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
|
| 11 | import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoConnectTaskScreen;
|
| 12 | import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
|
| 13 | import com.mojang.realmsclient.gui.screens.RealmsPopups;
|
| 14 | import com.mojang.realmsclient.gui.screens.RealmsTermsScreen;
|
| 15 | import java.net.URL;
|
| 16 | import java.nio.charset.StandardCharsets;
|
| 17 | import java.util.Objects;
|
| 18 | import java.util.UUID;
|
| 19 | import java.util.concurrent.CancellationException;
|
| 20 | import java.util.concurrent.CompletableFuture;
|
| 21 | import java.util.concurrent.TimeoutException;
|
| 22 | import java.util.function.Function;
|
| 23 | import net.fabricmc.api.EnvType;
|
| 24 | import net.fabricmc.api.Environment;
|
| 25 | import net.minecraft.client.Minecraft;
|
| 26 | import net.minecraft.client.gui.components.PopupScreen;
|
| 27 | import net.minecraft.client.gui.screens.GenericMessageScreen;
|
| 28 | import net.minecraft.client.gui.screens.Screen;
|
| 29 | import net.minecraft.client.resources.server.DownloadedPackSource;
|
| 30 | import net.minecraft.network.chat.Component;
|
| 31 | import org.slf4j.Logger;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class GetServerDetailsTask extends LongRunningTask {
|
| 35 | private static final Component APPLYING_PACK_TEXT = Component.translatable("multiplayer.applyingPack");
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | private static final Component TITLE = Component.translatable("mco.connect.connecting");
|
| 38 | private final RealmsServer server;
|
| 39 | private final Screen lastScreen;
|
| 40 |
|
| 41 | public GetServerDetailsTask(final Screen lastScreen, final RealmsServer server) {
|
| 42 | this.lastScreen = lastScreen;
|
| 43 | this.server = server;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void run() {
|
| 48 | RealmsJoinInformation address;
|
| 49 | try {
|
| 50 | address = this.fetchServerAddress();
|
| 51 | } catch (CancellationException e) {
|
| 52 | LOGGER.info("User aborted connecting to realms");
|
| 53 | return;
|
| 54 | } catch (RealmsServiceException e) {
|
| 55 | switch (e.realmsError.errorCode()) {
|
| 56 | case 6002:
|
| 57 | setScreen(new RealmsTermsScreen(this.lastScreen, this.server));
|
| 58 | return;
|
| 59 | case 6006:
|
| 60 | boolean isOwner = Minecraft.getInstance().isLocalPlayer(this.server.ownerUUID);
|
| 61 | setScreen(
|
| 62 | isOwner
|
| 63 | ? new RealmsBrokenWorldScreen(this.lastScreen, this.server.id, this.server.isMinigameActive())
|
| 64 | : new RealmsGenericErrorScreen(
|
| 65 | Component.translatable("mco.brokenworld.nonowner.title"), Component.translatable("mco.brokenworld.nonowner.error"), this.lastScreen
|
| 66 | )
|
| 67 | );
|
| 68 | return;
|
| 69 | default:
|
| 70 | this.error(e);
|
| 71 | LOGGER.error("Couldn't connect to world", e);
|
| 72 | return;
|
| 73 | }
|
| 74 | } catch (TimeoutException e) {
|
| 75 | this.error(Component.translatable("mco.errorMessage.connectionFailure"));
|
| 76 | return;
|
| 77 | } catch (Exception e) {
|
| 78 | LOGGER.error("Couldn't connect to world", e);
|
| 79 | this.error(e);
|
| 80 | return;
|
| 81 | }
|
| 82 |
|
| 83 | if (address.address() == null) {
|
| 84 | this.error(Component.translatable("mco.errorMessage.connectionFailure"));
|
| 85 | } else {
|
| 86 | boolean requiresResourcePack = address.resourcePackUrl() != null && address.resourcePackHash() != null;
|
| 87 | Screen nextScreen = requiresResourcePack
|
| 88 | ? this.resourcePackDownloadConfirmationScreen(address, generatePackId(this.server), this::connectScreen)
|
| 89 | : this.connectScreen(address);
|
| 90 | setScreen(nextScreen);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | private static UUID generatePackId(final RealmsServer serverData) {
|
| 95 | return serverData.minigameName != null
|
| 96 | ? UUID.nameUUIDFromBytes(("minigame:" + serverData.minigameName).getBytes(StandardCharsets.UTF_8))
|
| 97 | : UUID.nameUUIDFromBytes(("realms:" + Objects.requireNonNullElse(serverData.name, "") + ":" + serverData.activeSlot).getBytes(StandardCharsets.UTF_8));
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public Component getTitle() {
|
| 102 | return TITLE;
|
| 103 | }
|
| 104 |
|
| 105 | private RealmsJoinInformation fetchServerAddress() throws RealmsServiceException, TimeoutException, CancellationException {
|
| 106 | RealmsClient client = RealmsClient.getOrCreate();
|
| 107 |
|
| 108 | for (int i = 0; i < 40; i++) {
|
| 109 | if (this.aborted()) {
|
| 110 | throw new CancellationException();
|
| 111 | }
|
| 112 |
|
| 113 | try {
|
| 114 | return client.join(this.server.id);
|
| 115 | } catch (RetryCallException e) {
|
| 116 | pause(e.delaySeconds);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | throw new TimeoutException();
|
| 121 | }
|
| 122 |
|
| 123 | public RealmsLongRunningMcoTaskScreen connectScreen(final RealmsJoinInformation address) {
|
| 124 | return new RealmsLongRunningMcoConnectTaskScreen(this.lastScreen, address, new ConnectTask(this.lastScreen, this.server, address));
|
| 125 | }
|
| 126 |
|
| 127 | private PopupScreen resourcePackDownloadConfirmationScreen(
|
| 128 | final RealmsJoinInformation address, final UUID packId, final Function<RealmsJoinInformation, Screen> onCompletionScreen
|
| 129 | ) {
|
| 130 | Component popupMessage = Component.translatable("mco.configure.world.resourcepack.question");
|
| 131 | return RealmsPopups.infoPopupScreen(this.lastScreen, popupMessage, popupScreen -> {
|
| 132 | setScreen(new GenericMessageScreen(APPLYING_PACK_TEXT));
|
| 133 | this.scheduleResourcePackDownload(address, packId).thenRun(() -> setScreen(onCompletionScreen.apply(address))).exceptionally(e -> {
|
| 134 | Minecraft.getInstance().getDownloadedPackSource().cleanupAfterDisconnect();
|
| 135 | LOGGER.error("Failed to download resource pack from {}", address, e);
|
| 136 | setScreen(new RealmsGenericErrorScreen(Component.translatable("mco.download.resourcePack.fail"), this.lastScreen));
|
| 137 | return null;
|
| 138 | });
|
| 139 | });
|
| 140 | }
|
| 141 |
|
| 142 | private CompletableFuture<?> scheduleResourcePackDownload(final RealmsJoinInformation address, final UUID packId) {
|
| 143 | try {
|
| 144 | if (address.resourcePackUrl() == null) {
|
| 145 | return CompletableFuture.failedFuture(new IllegalStateException("resourcePackUrl was null"));
|
| 146 | }
|
| 147 |
|
| 148 | if (address.resourcePackHash() == null) {
|
| 149 | return CompletableFuture.failedFuture(new IllegalStateException("resourcePackHash was null"));
|
| 150 | }
|
| 151 |
|
| 152 | DownloadedPackSource packSource = Minecraft.getInstance().getDownloadedPackSource();
|
| 153 | CompletableFuture<Void> result = packSource.waitForPackFeedback(packId);
|
| 154 | packSource.allowServerPacks();
|
| 155 | packSource.pushPack(packId, new URL(address.resourcePackUrl()), address.resourcePackHash());
|
| 156 | return result;
|
| 157 | } catch (Exception e) {
|
| 158 | return CompletableFuture.failedFuture(e);
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|