| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.net.InetAddress;
|
| 5 | import java.net.UnknownHostException;
|
| 6 | import java.util.concurrent.atomic.AtomicInteger;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.GuiButton;
|
| 9 | import net.minecraft.client.gui.GuiDisconnected;
|
| 10 | import net.minecraft.client.gui.GuiScreen;
|
| 11 | import net.minecraft.client.network.NetHandlerLoginClient;
|
| 12 | import net.minecraft.client.resources.I18n;
|
| 13 | import net.minecraft.network.EnumConnectionState;
|
| 14 | import net.minecraft.network.NetworkManager;
|
| 15 | import net.minecraft.network.handshake.client.C00Handshake;
|
| 16 | import net.minecraft.network.login.client.C00PacketLoginStart;
|
| 17 | import net.minecraft.util.ChatComponentText;
|
| 18 | import net.minecraft.util.ChatComponentTranslation;
|
| 19 | import org.apache.logging.log4j.LogManager;
|
| 20 | import org.apache.logging.log4j.Logger;
|
| 21 |
|
| 22 | public class GuiConnecting extends GuiScreen
|
| 23 | {
|
| 24 | private static final AtomicInteger CONNECTION_ID = new AtomicInteger(0);
|
| 25 | private static final Logger logger = LogManager.getLogger();
|
| 26 | private NetworkManager networkManager;
|
| 27 | private boolean cancel;
|
| 28 | private final GuiScreen previousGuiScreen;
|
| 29 |
|
| 30 | public GuiConnecting(GuiScreen p_i1181_1_, Minecraft mcIn, ServerData p_i1181_3_)
|
| 31 | {
|
| 32 | this.mc = mcIn;
|
| 33 | this.previousGuiScreen = p_i1181_1_;
|
| 34 | ServerAddress serveraddress = ServerAddress.fromString(p_i1181_3_.serverIP);
|
| 35 | mcIn.loadWorld((WorldClient)null);
|
| 36 | mcIn.setServerData(p_i1181_3_);
|
| 37 | this.connect(serveraddress.getIP(), serveraddress.getPort());
|
| 38 | }
|
| 39 |
|
| 40 | public GuiConnecting(GuiScreen p_i1182_1_, Minecraft mcIn, String hostName, int port)
|
| 41 | {
|
| 42 | this.mc = mcIn;
|
| 43 | this.previousGuiScreen = p_i1182_1_;
|
| 44 | mcIn.loadWorld((WorldClient)null);
|
| 45 | this.connect(hostName, port);
|
| 46 | }
|
| 47 |
|
| 48 | private void connect(final String ip, final int port)
|
| 49 | {
|
| 50 | logger.info("Connecting to " + ip + ", " + port);
|
| 51 | (new Thread("Server Connector #" + CONNECTION_ID.incrementAndGet())
|
| 52 | {
|
| 53 | public void run()
|
| 54 | {
|
| 55 | InetAddress inetaddress = null;
|
| 56 |
|
| 57 | try
|
| 58 | {
|
| 59 | if (GuiConnecting.this.cancel)
|
| 60 | {
|
| 61 | return;
|
| 62 | }
|
| 63 |
|
| 64 | inetaddress = InetAddress.getByName(ip);
|
| 65 | GuiConnecting.this.networkManager = NetworkManager.createNetworkManagerAndConnect(inetaddress, port, GuiConnecting.this.mc.gameSettings.isUsingNativeTransport());
|
| 66 | GuiConnecting.this.networkManager.setNetHandler(new NetHandlerLoginClient(GuiConnecting.this.networkManager, GuiConnecting.this.mc, GuiConnecting.this.previousGuiScreen));
|
| 67 | GuiConnecting.this.networkManager.sendPacket(new C00Handshake(47, ip, port, EnumConnectionState.LOGIN));
|
| 68 | GuiConnecting.this.networkManager.sendPacket(new C00PacketLoginStart(GuiConnecting.this.mc.getSession().getProfile()));
|
| 69 | }
|
| 70 | catch (UnknownHostException unknownhostexception)
|
| 71 | {
|
| 72 | if (GuiConnecting.this.cancel)
|
| 73 | {
|
| 74 | return;
|
| 75 | }
|
| 76 |
|
| 77 | GuiConnecting.logger.error((String)"Couldn\'t connect to server", (Throwable)unknownhostexception);
|
| 78 | GuiConnecting.this.mc.displayGuiScreen(new GuiDisconnected(GuiConnecting.this.previousGuiScreen, "connect.failed", new ChatComponentTranslation("disconnect.genericReason", new Object[] {"Unknown host"})));
|
| 79 | }
|
| 80 | catch (Exception exception)
|
| 81 | {
|
| 82 | if (GuiConnecting.this.cancel)
|
| 83 | {
|
| 84 | return;
|
| 85 | }
|
| 86 |
|
| 87 | GuiConnecting.logger.error((String)"Couldn\'t connect to server", (Throwable)exception);
|
| 88 | String s = exception.toString();
|
| 89 |
|
| 90 | if (inetaddress != null)
|
| 91 | {
|
| 92 | String s1 = inetaddress.toString() + ":" + port;
|
| 93 | s = s.replaceAll(s1, "");
|
| 94 | }
|
| 95 |
|
| 96 | GuiConnecting.this.mc.displayGuiScreen(new GuiDisconnected(GuiConnecting.this.previousGuiScreen, "connect.failed", new ChatComponentTranslation("disconnect.genericReason", new Object[] {s})));
|
| 97 | }
|
| 98 | }
|
| 99 | }).start();
|
| 100 | }
|
| 101 |
|
| 102 | /**
|
| 103 | * Called from the main game loop to update the screen.
|
| 104 | */
|
| 105 | public void updateScreen()
|
| 106 | {
|
| 107 | if (this.networkManager != null)
|
| 108 | {
|
| 109 | if (this.networkManager.isChannelOpen())
|
| 110 | {
|
| 111 | this.networkManager.processReceivedPackets();
|
| 112 | }
|
| 113 | else
|
| 114 | {
|
| 115 | this.networkManager.checkDisconnected();
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
|
| 122 | * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
|
| 123 | */
|
| 124 | protected void keyTyped(char typedChar, int keyCode) throws IOException
|
| 125 | {
|
| 126 | }
|
| 127 |
|
| 128 | /**
|
| 129 | * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
|
| 130 | * window resizes, the buttonList is cleared beforehand.
|
| 131 | */
|
| 132 | public void initGui()
|
| 133 | {
|
| 134 | this.buttonList.clear();
|
| 135 | this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 120 + 12, I18n.format("gui.cancel", new Object[0])));
|
| 136 | }
|
| 137 |
|
| 138 | /**
|
| 139 | * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
|
| 140 | */
|
| 141 | protected void actionPerformed(GuiButton button) throws IOException
|
| 142 | {
|
| 143 | if (button.id == 0)
|
| 144 | {
|
| 145 | this.cancel = true;
|
| 146 |
|
| 147 | if (this.networkManager != null)
|
| 148 | {
|
| 149 | this.networkManager.closeChannel(new ChatComponentText("Aborted"));
|
| 150 | }
|
| 151 |
|
| 152 | this.mc.displayGuiScreen(this.previousGuiScreen);
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
|
| 158 | */
|
| 159 | public void drawScreen(int mouseX, int mouseY, float partialTicks)
|
| 160 | {
|
| 161 | this.drawDefaultBackground();
|
| 162 |
|
| 163 | if (this.networkManager == null)
|
| 164 | {
|
| 165 | this.drawCenteredString(this.fontRendererObj, I18n.format("connect.connecting", new Object[0]), this.width / 2, this.height / 2 - 50, 16777215);
|
| 166 | }
|
| 167 | else
|
| 168 | {
|
| 169 | this.drawCenteredString(this.fontRendererObj, I18n.format("connect.authorizing", new Object[0]), this.width / 2, this.height / 2 - 50, 16777215);
|
| 170 | }
|
| 171 |
|
| 172 | super.drawScreen(mouseX, mouseY, partialTicks);
|
| 173 | }
|
| 174 | }
|
| 175 |
|