| 1 | package com.mojang.realmsclient.util.task;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 5 | import com.mojang.realmsclient.client.RealmsClient;
|
| 6 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 7 | import com.mojang.realmsclient.exception.RetryCallException;
|
| 8 | import com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class OpenServerTask extends LongRunningTask {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private static final Component TITLE = Component.translatable("mco.configure.world.opening");
|
| 20 | private final RealmsServer serverData;
|
| 21 | private final Screen returnScreen;
|
| 22 | private final boolean join;
|
| 23 | private final Minecraft minecraft;
|
| 24 |
|
| 25 | public OpenServerTask(final RealmsServer realmsServer, final Screen returnScreen, final boolean join, final Minecraft minecraft) {
|
| 26 | this.serverData = realmsServer;
|
| 27 | this.returnScreen = returnScreen;
|
| 28 | this.join = join;
|
| 29 | this.minecraft = minecraft;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void run() {
|
| 34 | RealmsClient client = RealmsClient.getOrCreate();
|
| 35 |
|
| 36 | for (int i = 0; i < 25; i++) {
|
| 37 | if (this.aborted()) {
|
| 38 | return;
|
| 39 | }
|
| 40 |
|
| 41 | try {
|
| 42 | boolean openResult = client.open(this.serverData.id);
|
| 43 | if (openResult) {
|
| 44 | this.minecraft.execute(() -> {
|
| 45 | if (this.returnScreen instanceof RealmsConfigureWorldScreen screen) {
|
| 46 | screen.stateChanged();
|
| 47 | }
|
| 48 |
|
| 49 | this.serverData.state = RealmsServer.State.OPEN;
|
| 50 | if (this.join) {
|
| 51 | RealmsMainScreen.play(this.serverData, this.returnScreen);
|
| 52 | } else {
|
| 53 | this.minecraft.gui.setScreen(this.returnScreen);
|
| 54 | }
|
| 55 | });
|
| 56 | break;
|
| 57 | }
|
| 58 | } catch (RetryCallException e) {
|
| 59 | if (this.aborted()) {
|
| 60 | return;
|
| 61 | }
|
| 62 |
|
| 63 | pause(e.delaySeconds);
|
| 64 | } catch (Exception e) {
|
| 65 | if (this.aborted()) {
|
| 66 | return;
|
| 67 | }
|
| 68 |
|
| 69 | LOGGER.error("Failed to open server", e);
|
| 70 | this.error(e);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public Component getTitle() {
|
| 77 | return TITLE;
|
| 78 | }
|
| 79 | }
|
| 80 |
|