| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import io.netty.channel.ChannelFuture;
|
| 5 | import java.net.InetSocketAddress;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.concurrent.atomic.AtomicInteger;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.DefaultUncaughtExceptionHandler;
|
| 11 | import net.minecraft.client.GameNarrator;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.gui.components.Button;
|
| 15 | import net.minecraft.client.multiplayer.ClientHandshakePacketListenerImpl;
|
| 16 | import net.minecraft.client.multiplayer.LevelLoadTracker;
|
| 17 | import net.minecraft.client.multiplayer.ServerData;
|
| 18 | import net.minecraft.client.multiplayer.TransferState;
|
| 19 | import net.minecraft.client.multiplayer.chat.report.ReportEnvironment;
|
| 20 | import net.minecraft.client.multiplayer.resolver.ResolvedServerAddress;
|
| 21 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 22 | import net.minecraft.client.multiplayer.resolver.ServerNameResolver;
|
| 23 | import net.minecraft.client.quickplay.QuickPlay;
|
| 24 | import net.minecraft.client.quickplay.QuickPlayLog;
|
| 25 | import net.minecraft.client.resources.server.ServerPackManager;
|
| 26 | import net.minecraft.network.Connection;
|
| 27 | import net.minecraft.network.chat.CommonComponents;
|
| 28 | import net.minecraft.network.chat.Component;
|
| 29 | import net.minecraft.network.protocol.PacketFlow;
|
| 30 | import net.minecraft.network.protocol.login.LoginProtocols;
|
| 31 | import net.minecraft.network.protocol.login.ServerboundHelloPacket;
|
| 32 | import net.minecraft.server.network.EventLoopGroupHolder;
|
| 33 | import net.minecraft.util.Util;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 | import org.slf4j.Logger;
|
| 36 |
|
| 37 | @Environment(EnvType.CLIENT)
|
| 38 | public class ConnectScreen extends Screen {
|
| 39 | private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | private static final long NARRATION_DELAY_MS = 2000L;
|
| 42 | public static final Component ABORT_CONNECTION = Component.translatable("connect.aborted");
|
| 43 | public static final Component UNKNOWN_HOST_MESSAGE = Component.translatable("disconnect.genericReason", Component.translatable("disconnect.unknownHost"));
|
| 44 | private volatile @Nullable Connection connection;
|
| 45 | private @Nullable ChannelFuture channelFuture;
|
| 46 | private volatile boolean aborted;
|
| 47 | private final Screen parent;
|
| 48 | private Component status = Component.translatable("connect.connecting");
|
| 49 | private long lastNarration = -1L;
|
| 50 | private final Component connectFailedTitle;
|
| 51 |
|
| 52 | private ConnectScreen(final Screen parent, final Component connectFailedTitle) {
|
| 53 | super(GameNarrator.NO_TITLE);
|
| 54 | this.parent = parent;
|
| 55 | this.connectFailedTitle = connectFailedTitle;
|
| 56 | }
|
| 57 |
|
| 58 | public static void startConnecting(
|
| 59 | final Screen parent,
|
| 60 | final Minecraft minecraft,
|
| 61 | final ServerAddress hostAndPort,
|
| 62 | final ServerData data,
|
| 63 | final boolean isQuickPlay,
|
| 64 | final @Nullable TransferState transferState
|
| 65 | ) {
|
| 66 | if (minecraft.gui.screen() instanceof ConnectScreen) {
|
| 67 | LOGGER.error("Attempt to connect while already connecting");
|
| 68 | } else {
|
| 69 | Component connectFailedTitle;
|
| 70 | if (transferState != null) {
|
| 71 | connectFailedTitle = CommonComponents.TRANSFER_CONNECT_FAILED;
|
| 72 | } else if (isQuickPlay) {
|
| 73 | connectFailedTitle = QuickPlay.ERROR_TITLE;
|
| 74 | } else {
|
| 75 | connectFailedTitle = CommonComponents.CONNECT_FAILED;
|
| 76 | }
|
| 77 |
|
| 78 | ConnectScreen screen = new ConnectScreen(parent, connectFailedTitle);
|
| 79 | if (transferState != null) {
|
| 80 | screen.updateStatus(Component.translatable("connect.transferring"));
|
| 81 | }
|
| 82 |
|
| 83 | minecraft.disconnectWithProgressScreen(false);
|
| 84 | minecraft.prepareForMultiplayer();
|
| 85 | minecraft.updateReportEnvironment(ReportEnvironment.thirdParty(data.ip));
|
| 86 | minecraft.quickPlayLog().setWorldData(QuickPlayLog.Type.MULTIPLAYER, data.ip, data.name);
|
| 87 | minecraft.gui.setScreen(screen);
|
| 88 | screen.connect(minecraft, hostAndPort, data, transferState);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private void connect(final Minecraft minecraft, final ServerAddress hostAndPort, final ServerData server, final @Nullable TransferState transferState) {
|
| 93 | LOGGER.info("Connecting to {}, {}", hostAndPort.getHost(), hostAndPort.getPort());
|
| 94 | Thread thread = new Thread("Server Connector #" + UNIQUE_THREAD_ID.incrementAndGet()) {
|
| 95 | @Override
|
| 96 | public void run() {
|
| 97 | InetSocketAddress address = null;
|
| 98 |
|
| 99 | try {
|
| 100 | if (ConnectScreen.this.aborted) {
|
| 101 | return;
|
| 102 | }
|
| 103 |
|
| 104 | Optional<InetSocketAddress> resolvedAddress = ServerNameResolver.DEFAULT.resolveAddress(hostAndPort).map(ResolvedServerAddress::asInetSocketAddress);
|
| 105 | if (ConnectScreen.this.aborted) {
|
| 106 | return;
|
| 107 | }
|
| 108 |
|
| 109 | if (resolvedAddress.isEmpty()) {
|
| 110 | minecraft.execute(
|
| 111 | () -> minecraft.gui
|
| 112 | .setScreen(new DisconnectedScreen(ConnectScreen.this.parent, ConnectScreen.this.connectFailedTitle, ConnectScreen.UNKNOWN_HOST_MESSAGE))
|
| 113 | );
|
| 114 | return;
|
| 115 | }
|
| 116 |
|
| 117 | address = resolvedAddress.get();
|
| 118 | Connection pendingConnection;
|
| 119 | synchronized (ConnectScreen.this) {
|
| 120 | if (ConnectScreen.this.aborted) {
|
| 121 | return;
|
| 122 | }
|
| 123 |
|
| 124 | pendingConnection = new Connection(PacketFlow.CLIENTBOUND);
|
| 125 | pendingConnection.setBandwidthLogger(minecraft.getDebugOverlay().getBandwidthLogger());
|
| 126 | ConnectScreen.this.channelFuture = Connection.connect(address, EventLoopGroupHolder.remote(minecraft.options.useNativeTransport()), pendingConnection);
|
| 127 | }
|
| 128 |
|
| 129 | ConnectScreen.this.channelFuture.syncUninterruptibly();
|
| 130 | synchronized (ConnectScreen.this) {
|
| 131 | if (ConnectScreen.this.aborted) {
|
| 132 | pendingConnection.disconnect(ConnectScreen.ABORT_CONNECTION);
|
| 133 | return;
|
| 134 | }
|
| 135 |
|
| 136 | ConnectScreen.this.connection = pendingConnection;
|
| 137 | minecraft.getDownloadedPackSource().configureForServerControl(pendingConnection, convertPackStatus(server.getResourcePackStatus()));
|
| 138 | }
|
| 139 |
|
| 140 | ConnectScreen.this.connection
|
| 141 | .initiateServerboundPlayConnection(
|
| 142 | address.getHostName(),
|
| 143 | address.getPort(),
|
| 144 | LoginProtocols.SERVERBOUND,
|
| 145 | LoginProtocols.CLIENTBOUND,
|
| 146 | new ClientHandshakePacketListenerImpl(
|
| 147 | ConnectScreen.this.connection,
|
| 148 | minecraft,
|
| 149 | server,
|
| 150 | ConnectScreen.this.parent,
|
| 151 | false,
|
| 152 | null,
|
| 153 | ConnectScreen.this::updateStatus,
|
| 154 | new LevelLoadTracker(),
|
| 155 | transferState
|
| 156 | ),
|
| 157 | transferState != null
|
| 158 | );
|
| 159 | ConnectScreen.this.connection.send(new ServerboundHelloPacket(minecraft.getUser().getName(), minecraft.getUser().getProfileId()));
|
| 160 | } catch (Exception exception) {
|
| 161 | if (ConnectScreen.this.aborted) {
|
| 162 | return;
|
| 163 | }
|
| 164 |
|
| 165 | Exception cause;
|
| 166 | if (exception.getCause() instanceof Exception originalCause) {
|
| 167 | cause = originalCause;
|
| 168 | } else {
|
| 169 | cause = exception;
|
| 170 | }
|
| 171 |
|
| 172 | ConnectScreen.LOGGER.error("Couldn't connect to server", exception);
|
| 173 | String message = address == null
|
| 174 | ? cause.getMessage()
|
| 175 | : cause.getMessage().replaceAll(address.getHostName() + ":" + address.getPort(), "").replaceAll(address.toString(), "");
|
| 176 | minecraft.execute(
|
| 177 | () -> minecraft.gui
|
| 178 | .setScreen(
|
| 179 | new DisconnectedScreen(ConnectScreen.this.parent, ConnectScreen.this.connectFailedTitle, Component.translatable("disconnect.genericReason", message))
|
| 180 | )
|
| 181 | );
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | private static ServerPackManager.PackPromptStatus convertPackStatus(final ServerData.ServerPackStatus resourcePackStatus) {
|
| 186 | return switch (resourcePackStatus) {
|
| 187 | case ENABLED -> ServerPackManager.PackPromptStatus.ALLOWED;
|
| 188 | case DISABLED -> ServerPackManager.PackPromptStatus.DECLINED;
|
| 189 | case PROMPT -> ServerPackManager.PackPromptStatus.PENDING;
|
| 190 | };
|
| 191 | }
|
| 192 | };
|
| 193 | thread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
|
| 194 | thread.start();
|
| 195 | }
|
| 196 |
|
| 197 | private void updateStatus(final Component status) {
|
| 198 | this.status = status;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void tick() {
|
| 203 | if (this.connection != null) {
|
| 204 | if (this.connection.isConnected()) {
|
| 205 | this.connection.tick();
|
| 206 | } else {
|
| 207 | this.connection.handleDisconnection();
|
| 208 | }
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public boolean shouldCloseOnEsc() {
|
| 214 | return false;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | protected void init() {
|
| 219 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> {
|
| 220 | synchronized (this) {
|
| 221 | this.aborted = true;
|
| 222 | if (this.channelFuture != null) {
|
| 223 | this.channelFuture.cancel(true);
|
| 224 | this.channelFuture = null;
|
| 225 | }
|
| 226 |
|
| 227 | if (this.connection != null) {
|
| 228 | this.connection.disconnect(ABORT_CONNECTION);
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | this.minecraft.gui.setScreen(this.parent);
|
| 233 | }).bounds(this.width / 2 - 100, this.height / 4 + 120 + 12, 200, 20).build());
|
| 234 | }
|
| 235 |
|
| 236 | @Override
|
| 237 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 238 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 239 | long current = Util.getMillis();
|
| 240 | if (current - this.lastNarration > 2000L) {
|
| 241 | this.lastNarration = current;
|
| 242 | this.minecraft.getNarrator().saySystemNow(Component.translatable("narrator.joining"));
|
| 243 | }
|
| 244 |
|
| 245 | graphics.centeredText(this.font, this.status, this.width / 2, this.height / 2 - 50, -1);
|
| 246 | }
|
| 247 | }
|
| 248 |
|