示例#1
0
        /// <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);
                }
            }
        }