Example #1
0
 private void HandleChatMessage(Client client, ChatMessagePacket packet)
 {
     // TODO: Commands, spam filter, etc.
     packet.Username = client.Player.Username;
     Server.BroadcastPacket(packet);
     Logger.Chat(String.Format("<{0}> {1}", packet.Username, packet.Message));
 }
        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);
        }