| 1 | package net.minecraft.realms;
|
| 2 |
|
| 3 | import java.net.InetAddress;
|
| 4 | import java.net.UnknownHostException;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.network.NetHandlerLoginClient;
|
| 7 | import net.minecraft.network.EnumConnectionState;
|
| 8 | import net.minecraft.network.NetworkManager;
|
| 9 | import net.minecraft.network.handshake.client.C00Handshake;
|
| 10 | import net.minecraft.network.login.client.C00PacketLoginStart;
|
| 11 | import net.minecraft.util.ChatComponentTranslation;
|
| 12 | import org.apache.logging.log4j.LogManager;
|
| 13 | import org.apache.logging.log4j.Logger;
|
| 14 |
|
| 15 | public class RealmsConnect
|
| 16 | {
|
| 17 | private static final Logger LOGGER = LogManager.getLogger();
|
| 18 | private final RealmsScreen onlineScreen;
|
| 19 | private volatile boolean aborted = false;
|
| 20 | private NetworkManager connection;
|
| 21 |
|
| 22 | public RealmsConnect(RealmsScreen p_i1079_1_)
|
| 23 | {
|
| 24 | this.onlineScreen = p_i1079_1_;
|
| 25 | }
|
| 26 |
|
| 27 | public void connect(final String p_connect_1_, final int p_connect_2_)
|
| 28 | {
|
| 29 | Realms.setConnectedToRealms(true);
|
| 30 | (new Thread("Realms-connect-task")
|
| 31 | {
|
| 32 | public void run()
|
| 33 | {
|
| 34 | InetAddress inetaddress = null;
|
| 35 |
|
| 36 | try
|
| 37 | {
|
| 38 | inetaddress = InetAddress.getByName(p_connect_1_);
|
| 39 |
|
| 40 | if (RealmsConnect.this.aborted)
|
| 41 | {
|
| 42 | return;
|
| 43 | }
|
| 44 |
|
| 45 | RealmsConnect.this.connection = NetworkManager.createNetworkManagerAndConnect(inetaddress, p_connect_2_, Minecraft.getMinecraft().gameSettings.isUsingNativeTransport());
|
| 46 |
|
| 47 | if (RealmsConnect.this.aborted)
|
| 48 | {
|
| 49 | return;
|
| 50 | }
|
| 51 |
|
| 52 | RealmsConnect.this.connection.setNetHandler(new NetHandlerLoginClient(RealmsConnect.this.connection, Minecraft.getMinecraft(), RealmsConnect.this.onlineScreen.getProxy()));
|
| 53 |
|
| 54 | if (RealmsConnect.this.aborted)
|
| 55 | {
|
| 56 | return;
|
| 57 | }
|
| 58 |
|
| 59 | RealmsConnect.this.connection.sendPacket(new C00Handshake(47, p_connect_1_, p_connect_2_, EnumConnectionState.LOGIN));
|
| 60 |
|
| 61 | if (RealmsConnect.this.aborted)
|
| 62 | {
|
| 63 | return;
|
| 64 | }
|
| 65 |
|
| 66 | RealmsConnect.this.connection.sendPacket(new C00PacketLoginStart(Minecraft.getMinecraft().getSession().getProfile()));
|
| 67 | }
|
| 68 | catch (UnknownHostException unknownhostexception)
|
| 69 | {
|
| 70 | Realms.clearResourcePack();
|
| 71 |
|
| 72 | if (RealmsConnect.this.aborted)
|
| 73 | {
|
| 74 | return;
|
| 75 | }
|
| 76 |
|
| 77 | RealmsConnect.LOGGER.error((String)"Couldn\'t connect to world", (Throwable)unknownhostexception);
|
| 78 | Minecraft.getMinecraft().getResourcePackRepository().clearResourcePack();
|
| 79 | Realms.setScreen(new DisconnectedRealmsScreen(RealmsConnect.this.onlineScreen, "connect.failed", new ChatComponentTranslation("disconnect.genericReason", new Object[] {"Unknown host \'" + p_connect_1_ + "\'"})));
|
| 80 | }
|
| 81 | catch (Exception exception)
|
| 82 | {
|
| 83 | Realms.clearResourcePack();
|
| 84 |
|
| 85 | if (RealmsConnect.this.aborted)
|
| 86 | {
|
| 87 | return;
|
| 88 | }
|
| 89 |
|
| 90 | RealmsConnect.LOGGER.error((String)"Couldn\'t connect to world", (Throwable)exception);
|
| 91 | String s = exception.toString();
|
| 92 |
|
| 93 | if (inetaddress != null)
|
| 94 | {
|
| 95 | String s1 = inetaddress.toString() + ":" + p_connect_2_;
|
| 96 | s = s.replaceAll(s1, "");
|
| 97 | }
|
| 98 |
|
| 99 | Realms.setScreen(new DisconnectedRealmsScreen(RealmsConnect.this.onlineScreen, "connect.failed", new ChatComponentTranslation("disconnect.genericReason", new Object[] {s})));
|
| 100 | }
|
| 101 | }
|
| 102 | }).start();
|
| 103 | }
|
| 104 |
|
| 105 | public void abort()
|
| 106 | {
|
| 107 | this.aborted = true;
|
| 108 | }
|
| 109 |
|
| 110 | public void tick()
|
| 111 | {
|
| 112 | if (this.connection != null)
|
| 113 | {
|
| 114 | if (this.connection.isChannelOpen())
|
| 115 | {
|
| 116 | this.connection.processReceivedPackets();
|
| 117 | }
|
| 118 | else
|
| 119 | {
|
| 120 | this.connection.checkDisconnected();
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|