Exemplo n.º 1
0
        private void HandleLogin(Client client, LoginRequestPacket packet)
        {
            if (client.LoggedIn)
            {
                return;
            }

            client.Player.EntityID = Server.TotalEntityCount++;
            client.Stream.WritePacket(new LoginResponsePacket(client.Player.EntityID, Server.ServerName,
                                                              Server.MapSeed, Server.GameMode, (byte)(Server.IsNether ? -1 : 0), (byte)Server.Difficulty, (byte)128, (byte)Server.MaxPlayers));
            client.Player.Username = packet.Username;
            client.LoggedIn        = true;
            client.SendInitialChunks();
            client.SendInitialPosition();
            client.SendInitialInventory();

            Server.BroadcastPacket(new ChatMessagePacket("", ChatColor.Yellow + packet.Username + " joined the game."));
            foreach (Client c in Server.GetClients())
            {
                if (!c.Equals(client))
                {
                    client.Stream.WritePacket(new NamedEntitySpawnPacket(c.Player.EntityID, c.Player.Username,
                                                                         (int)c.Player.Location.X, (int)c.Player.Location.Y, (int)c.Player.Location.Z, 0, 0, 0));
                }
            }
            Logger.Info("'" + packet.Username + "' joined the game. [Entity ID = " + client.Player.EntityID + "]");
        }
Exemplo n.º 2
0
        public Packet ReadPacket()
        {
            if (!Stream.CanRead)
            {
                throw new Exception("Cannot read packet from stream.");
            }

            Packet packet = null;

            try
            {
                PacketType type = (PacketType)Stream.ReadByte();
                switch (type)
                {
                case PacketType.KeepAlive:
                    packet = new KeepAlivePacket();
                    ((KeepAlivePacket)packet).KeepAliveID = Stream.ReadInt();
                    break;

                case PacketType.Login:
                    packet = new LoginRequestPacket();
                    ((LoginRequestPacket)packet).ProtocolVersion = Stream.ReadInt();
                    ((LoginRequestPacket)packet).Username        = Stream.ReadString16();
                    Stream.ReadLong();
                    Stream.ReadInt();
                    Stream.ReadByte();
                    Stream.ReadByte();
                    Stream.ReadByte();
                    Stream.ReadByte();
                    break;

                case PacketType.Handshake:
                    packet = new HandshakeRequestPacket();
                    ((HandshakeRequestPacket)packet).Username = Stream.ReadString16();
                    break;

                case PacketType.PlayerPosition:
                    packet = new PlayerPositionPacket();
                    ((PlayerPositionPacket)packet).X        = Stream.ReadDouble();  // X
                    ((PlayerPositionPacket)packet).Stance   = Stream.ReadDouble();  // Stance
                    ((PlayerPositionPacket)packet).Y        = Stream.ReadDouble();  // Y
                    ((PlayerPositionPacket)packet).Z        = Stream.ReadDouble();  // Z
                    ((PlayerPositionPacket)packet).OnGround = Stream.ReadBoolean(); // On-Ground
                    break;

                case PacketType.PlayerLook:
                    // TODO
                    Stream.Read(new byte[9], 0, 9);
                    break;

                case PacketType.Player:
                    // TODO
                    Stream.ReadBoolean();
                    break;

                case PacketType.PlayerPositionLook:
                    packet = new PlayerPositionLookPacket();
                    ((PlayerPositionLookPacket)packet).X        = Stream.ReadDouble();  // X
                    ((PlayerPositionLookPacket)packet).Stance   = Stream.ReadDouble();  // Stance
                    ((PlayerPositionLookPacket)packet).Y        = Stream.ReadDouble();  // Y
                    ((PlayerPositionLookPacket)packet).Z        = Stream.ReadDouble();  // Z
                    ((PlayerPositionLookPacket)packet).Yaw      = Stream.ReadFloat();   // Yaw
                    ((PlayerPositionLookPacket)packet).Pitch    = Stream.ReadFloat();   // Pitch
                    ((PlayerPositionLookPacket)packet).OnGround = Stream.ReadBoolean(); // On-Ground
                    break;

                case PacketType.Animation:
                    // TODO
                    Stream.ReadInt();
                    Stream.ReadByte();
                    break;

                case PacketType.EntityAction:
                    // TODO
                    Stream.ReadInt();
                    Stream.ReadByte();
                    break;

                case PacketType.PlayerDigging:
                    // TODO
                    packet = new PlayerDiggingPacket();
                    ((PlayerDiggingPacket)packet).DigStatus = (byte)Stream.ReadByte();     // Status (0 = Started, 2 = Finished, 4 = Drop Item)
                    ((PlayerDiggingPacket)packet).DigX      = Stream.ReadInt();
                    ((PlayerDiggingPacket)packet).DigY      = (byte)Stream.ReadByte();
                    ((PlayerDiggingPacket)packet).DigZ      = Stream.ReadInt();
                    ((PlayerDiggingPacket)packet).DigFace   = Stream.ReadByte();
                    break;

                case PacketType.ChatMessage:
                    packet = new ChatMessagePacket("", Stream.ReadString16());
                    break;

                case PacketType.HoldingChange:
                    // TODO
                    Stream.ReadShort();     // Slot ID
                    break;

                case PacketType.PlayerBlockPlacement:
                    // TODO
                    packet = new PlayerBlockPlacementPacket();

                    ((PlayerBlockPlacementPacket)packet).X         = Stream.ReadInt();            // X
                    ((PlayerBlockPlacementPacket)packet).Y         = (byte)Stream.ReadByte();     // Y
                    ((PlayerBlockPlacementPacket)packet).Z         = Stream.ReadInt();            // Z
                    ((PlayerBlockPlacementPacket)packet).Direction = (byte)Stream.ReadByte();     // Direction
                    if ((((PlayerBlockPlacementPacket)packet).BlockID = Stream.ReadShort()) >= 0) // Block or Item ID
                    {
                        ((PlayerBlockPlacementPacket)packet).Amount = (byte)Stream.ReadByte();    // Amount
                        ((PlayerBlockPlacementPacket)packet).Damage = Stream.ReadShort();         // Damage
                    }
                    break;

                case PacketType.WindowClick:
                    // TODO
                    Stream.ReadByte();            // Window ID
                    Stream.ReadShort();           // Slot
                    Stream.ReadByte();            // Right Click
                    Stream.ReadShort();           // Action Number
                    Stream.ReadBoolean();         // Shift
                    if (Stream.ReadShort() != -1) // Item ID
                    {
                        Stream.ReadByte();        // Item Count
                        Stream.ReadShort();       // Item Uses
                    }
                    break;

                case PacketType.Disconnect:
                    packet = new DisconnectPacket();
                    ((DisconnectPacket)packet).Reason = Stream.ReadString16();
                    break;

                default:
                    Logger.Debug("Unhandled data: " + type);
                    break;
                }
            }
            catch (Exception e)
            {
                Logger.Warn(e);
            }

            if (packet != null)
            {
                Logger.Debug("Received " + packet.Type + " packet");
            }

            return(packet);
        }