/// <summary> /// Do the Minecraft login. /// </summary> /// <returns>True if login successful</returns> public bool Login() { byte[] protocol_version = dataTypes.GetVarInt(protocolversion); string server_address = pForge.GetServerAddress(handler.GetServerHost()); byte[] server_port = BitConverter.GetBytes((ushort)handler.GetServerPort()); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(2); byte[] handshake_packet = dataTypes.ConcatBytes(protocol_version, dataTypes.GetString(server_address), server_port, next_state); SendPacket(0x00, handshake_packet); byte[] login_packet = dataTypes.GetString(handler.GetUsername()); SendPacket(0x00, login_packet); int packetID = -1; List <byte> packetData = new List <byte>(); while (true) { ReadNextPacket(ref packetID, packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); } else if (packetID == 0x01) //Encryption request { string serverID = dataTypes.ReadNextString(packetData); byte[] Serverkey = dataTypes.ReadNextByteArray(packetData); byte[] token = dataTypes.ReadNextByteArray(packetData); return(StartEncryption(handler.GetUserUUID(), handler.GetSessionID(), token, serverID, Serverkey)); } else if (packetID == 0x02) //Login successful { ConsoleIO.WriteLineFormatted("§8Server is in offline mode."); login_phase = false; if (!pForge.CompleteForgeHandshake()) { return(false); } StartUpdating(); return(true); //No need to check session or start encryption } else { HandlePacket(packetID, packetData); } } }
public bool Login() { if (Handshake(handler.GetUserUUID(), handler.GetUsername(), handler.GetSessionID(), handler.GetServerHost(), handler.GetServerPort())) { Send(new byte[] { 0xCD, 0 }); try { byte[] pid = new byte[1]; try { if (c.Connected) { Receive(pid, 0, 1, SocketFlags.None); while (pid[0] >= 0xC0 && pid[0] != 0xFF) //Skip some early packets or plugin messages { processPacket(pid[0]); Receive(pid, 0, 1, SocketFlags.None); } if (pid[0] == (byte)1) { readData(4); readNextString(); readData(5); StartUpdating(); return(true); //The Server accepted the request } else if (pid[0] == (byte)0xFF) { string reason = readNextString(); handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, reason); return(false); } } } catch (Exception e) { //Connection failed ConsoleIO.WriteLineFormatted("§8" + e.GetType().Name + ": " + e.Message); return(false); } } catch { handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, ""); return(false); } return(false); //Login was unsuccessful (received a kick...) } else { return(false); } }