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