Пример #1
0
        private void HandlePlayerDigging(Client client, PlayerDiggingPacket packet)
        {
            if (!client.LoggedIn)
            {
                client.Dispose();
            }

            // TODO: Check if player is close enough to break the block; implement Start Digging and Drop statuses
            if (packet.DigStatus == 2 || Server.GameMode == 1) // Finished Digging
            {
                Nullable <Block> nb = Server.GetWorldManager().GetWorld(0).GetBlock(new WorldLocation(packet.DigX, (int)packet.DigY, packet.DigZ));
                if (nb != null)
                {
                    Block b = (Block)nb;

                    if (Location.Distance(new Location(b.Location.X, b.Location.Y, b.Location.Z), client.Player.Location) > 5)
                    {
                        return;
                    }

                    ItemEntity dropEntity = new ItemEntity(Server.TotalEntityCount++, b.GetBlockType());
                    dropEntity.Location = new Location(b.Location.X, b.Location.Y, b.Location.Z);
                    Server.GetWorldManager().GetWorld(0).AddEntity(dropEntity);

                    Server.BroadcastPacket(new PickupSpawnPacket(dropEntity.EntityID, (short)dropEntity.Type, 1, 0,
                                                                 (int)dropEntity.Location.X, (int)dropEntity.Location.Y, (int)dropEntity.Location.Z, 0, 0, 0));
                    b.SetBlockType(BlockType.Air);
                    Server.BroadcastPacket(new EntityPacket(dropEntity.EntityID));

                    Server.OnBlockChange(b);
                }
            }
        }
Пример #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);
        }