| 1 | package net.minecraft.realms;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 5 | import java.net.InetSocketAddress;
|
| 6 | import java.util.Objects;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.screens.DisconnectedScreen;
|
| 11 | import net.minecraft.client.gui.screens.Screen;
|
| 12 | import net.minecraft.client.multiplayer.ClientHandshakePacketListenerImpl;
|
| 13 | import net.minecraft.client.multiplayer.LevelLoadTracker;
|
| 14 | import net.minecraft.client.multiplayer.chat.report.ReportEnvironment;
|
| 15 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 16 | import net.minecraft.client.quickplay.QuickPlayLog;
|
| 17 | import net.minecraft.client.resources.server.ServerPackManager;
|
| 18 | import net.minecraft.network.Connection;
|
| 19 | import net.minecraft.network.chat.CommonComponents;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.protocol.login.ServerboundHelloPacket;
|
| 22 | import net.minecraft.server.network.EventLoopGroupHolder;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class RealmsConnect {
|
| 28 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 29 | private final Screen onlineScreen;
|
| 30 | private volatile boolean aborted;
|
| 31 | private @Nullable Connection connection;
|
| 32 |
|
| 33 | public RealmsConnect(final Screen onlineScreen) {
|
| 34 | this.onlineScreen = onlineScreen;
|
| 35 | }
|
| 36 |
|
| 37 | public void connect(final RealmsServer server, final ServerAddress hostAndPort) {
|
| 38 | final Minecraft minecraft = Minecraft.getInstance();
|
| 39 | minecraft.prepareForMultiplayer();
|
| 40 | minecraft.getNarrator().saySystemNow(Component.translatable("mco.connect.success"));
|
| 41 | final String hostname = hostAndPort.getHost();
|
| 42 | final int port = hostAndPort.getPort();
|
| 43 | (new Thread("Realms-connect-task") {
|
| 44 | @Override
|
| 45 | public void run() {
|
| 46 | InetSocketAddress address = null;
|
| 47 |
|
| 48 | try {
|
| 49 | address = new InetSocketAddress(hostname, port);
|
| 50 | if (RealmsConnect.this.aborted) {
|
| 51 | return;
|
| 52 | }
|
| 53 |
|
| 54 | RealmsConnect.this.connection = Connection.connectToServer(
|
| 55 | address, EventLoopGroupHolder.remote(minecraft.options.useNativeTransport()), minecraft.getDebugOverlay().getBandwidthLogger()
|
| 56 | );
|
| 57 | if (RealmsConnect.this.aborted) {
|
| 58 | return;
|
| 59 | }
|
| 60 |
|
| 61 | ClientHandshakePacketListenerImpl clientHandshakePacketListener = new ClientHandshakePacketListenerImpl(
|
| 62 | RealmsConnect.this.connection,
|
| 63 | minecraft,
|
| 64 | server.toServerData(hostname),
|
| 65 | RealmsConnect.this.onlineScreen,
|
| 66 | false,
|
| 67 | null,
|
| 68 | status -> {},
|
| 69 | new LevelLoadTracker(),
|
| 70 | null
|
| 71 | );
|
| 72 | if (server.isMinigameActive()) {
|
| 73 | clientHandshakePacketListener.setMinigameName(server.minigameName);
|
| 74 | }
|
| 75 |
|
| 76 | if (RealmsConnect.this.aborted) {
|
| 77 | return;
|
| 78 | }
|
| 79 |
|
| 80 | RealmsConnect.this.connection.initiateServerboundPlayConnection(hostname, port, clientHandshakePacketListener);
|
| 81 | if (RealmsConnect.this.aborted) {
|
| 82 | return;
|
| 83 | }
|
| 84 |
|
| 85 | RealmsConnect.this.connection.send(new ServerboundHelloPacket(minecraft.getUser().getName(), minecraft.getUser().getProfileId()));
|
| 86 | minecraft.updateReportEnvironment(ReportEnvironment.realm(server));
|
| 87 | minecraft.quickPlayLog().setWorldData(QuickPlayLog.Type.REALMS, String.valueOf(server.id), Objects.requireNonNullElse(server.name, "unknown"));
|
| 88 | minecraft.getDownloadedPackSource().configureForServerControl(RealmsConnect.this.connection, ServerPackManager.PackPromptStatus.ALLOWED);
|
| 89 | } catch (Exception e) {
|
| 90 | minecraft.getDownloadedPackSource().cleanupAfterDisconnect();
|
| 91 | if (RealmsConnect.this.aborted) {
|
| 92 | return;
|
| 93 | }
|
| 94 |
|
| 95 | RealmsConnect.LOGGER.error("Couldn't connect to world", e);
|
| 96 | String message = e.toString();
|
| 97 | if (address != null) {
|
| 98 | String filter = address + ":" + port;
|
| 99 | message = message.replaceAll(filter, "");
|
| 100 | }
|
| 101 |
|
| 102 | DisconnectedScreen screen = new DisconnectedScreen(
|
| 103 | RealmsConnect.this.onlineScreen,
|
| 104 | Component.translatable("mco.connect.failed"),
|
| 105 | Component.translatable("disconnect.genericReason", message),
|
| 106 | CommonComponents.GUI_BACK
|
| 107 | );
|
| 108 | minecraft.execute(() -> minecraft.gui.setScreen(screen));
|
| 109 | }
|
| 110 | }
|
| 111 | })
|
| 112 | .start();
|
| 113 | }
|
| 114 |
|
| 115 | public void abort() {
|
| 116 | this.aborted = true;
|
| 117 | if (this.connection != null && this.connection.isConnected()) {
|
| 118 | this.connection.disconnect(Component.translatable("disconnect.genericReason"));
|
| 119 | this.connection.handleDisconnection();
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public void tick() {
|
| 124 | if (this.connection != null) {
|
| 125 | if (this.connection.isConnected()) {
|
| 126 | this.connection.tick();
|
| 127 | } else {
|
| 128 | this.connection.handleDisconnection();
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|