private void HandlePlayerPosAndLook(PlayerPositionAndLookPacket packet)
 {
     Player.KnownPosition.X     = (float)packet.X;
     Player.KnownPosition.Y     = (float)(packet.Y + 1.62f);
     Player.KnownPosition.Z     = (float)packet.Z;
     Player.KnownPosition.Yaw   = packet.Yaw;
     Player.KnownPosition.Pitch = packet.Pitch;
 }
Esempio n. 2
0
    private void SendPlayerPositionAndLook(IMinecraftUser user, Position position)
    {
        using var packet = new PlayerPositionAndLookPacket();

        packet.WriteDouble(position.X); // x
        packet.WriteDouble(position.Y); // y
        packet.WriteDouble(position.Z); // z
        packet.WriteSingle(0);          // yaw
        packet.WriteSingle(0);          // pitch
        packet.WriteByte(0);
        //packet.WriteByte(0x01 | 0x02 | 0x04); // position flags (x|y|z)
        packet.WriteVarInt32(0); // teleport id

        user.Send(packet);
    }
Esempio n. 3
0
        public void SendPlayerPositionAndLook()
        {
            PlayerLocation loc = (PlayerLocation)KnownPosition.Clone();

            PlayerPositionAndLookPacket packet = new PlayerPositionAndLookPacket()
            {
                Flags      = 0,
                TeleportId = 0,
                X          = loc.X,
                Y          = loc.Y,
                Z          = loc.Z,
                Yaw        = loc.Yaw,
                Pitch      = loc.Pitch
            };

            Connection.SendPacket(packet);
        }
        public async Task HandleAsync(TcpClient sender)
        {
            _stream = sender.GetStream();
            _sender = sender;

            _stream.ReadVarInt();  // packet length
            var packetId = (byte)_stream.ReadByte();

            if (packetId != 0x00)
            {
                if (packetId == 0xFE)
                {
                    _logger.LogError("Received 0xFE Legacy Ping packet, the server must have sent the client incorrect data. Skipping.");
                }
                else
                {
                    _logger.LogError($"Received unknown packet with ID 0x{packetId:x2}. Skipping.");
                }
                Dispose();
                return;
            }
            var protocolVersion = _stream.ReadVarInt();
            var serverAddress   = _stream.ReadString();

            var serverPort         = _stream.Read <ushort>();
            var isRequestingStatus = _stream.ReadVarInt() == 1;

            _logger.LogDebug($"[Handshaking] New client connecting with protocol version {protocolVersion}, " +
                             $"using server address {serverAddress}:{serverPort}, " +
                             $"and {(isRequestingStatus ? "is requesting status information" : "is requesting to login")}.");

            _stream.ReadVarInt();
            _stream.ReadByte();

            if (isRequestingStatus)
            {
                _logger.LogDebug("[Status] Received status request.");
                var serverListPingResponsePacket = new ServerListPingResponsePacket(LibrisMinecraftServer.ServerVersion,
                                                                                    LibrisMinecraftServer.ProtocolVersion, 0, _server.MaximumPlayers, new List <PlayerListSampleEntry> {
                    new PlayerListSampleEntry("best_jessica", "abdc8af6-70ab-4930-ab47-c6fc4e618155")
                },
                                                                                    _server.Description, _server.Favicon?.GetMinecraftFaviconString());
                serverListPingResponsePacket.WriteToStream(_stream);

                try
                {
                    var latencyPacketLength = _stream.ReadVarInt();
                    var latencyPacketId     = _stream.ReadByte();

                    if (latencyPacketId != InboundPackets.ServerListLatencyPingPacketId)
                    {
                        _logger.LogInformation($"[Status] Closing socket. Client did not request latency detection.");
                        Dispose();
                        return;
                    }

                    var payload = _stream.Read <long>();

                    _writer.WritePacket(new ServerListPingPongPacket(payload));

                    _logger.LogDebug($"[Status] Closing socket.");
                    Dispose();
                }
                catch (EndOfStreamException)
                {
                    _logger.LogDebug($"[Status] Closing socket. Client did not request latency detection - received End of Stream. Perhaps the response data was corrupt?");
                    Dispose();
                }
            }
            else
            {
                var username = _stream.ReadString();
                _logger.LogDebug("[Login] Login request initiated from user " + username);

                // <Do authorization logic here>

                // Login succeeded
                new LoginSuccessPacket("abdc8af6-70ab-4930-ab47-c6fc4e618155", "best_jessica").WriteToStream(_stream);

                // Instruct client to join game
                var joinGamePacket = new JoinGamePacket(0, PlayerGamemode.Survival, Dimension.Overworld, WorldType.Default, 10);
                _writer.WritePacket(joinGamePacket);

                // Receive settings from client
                _stream.ReadVarInt();
                var clientSettingsId = _stream.ReadByte();
                if (clientSettingsId != InboundPackets.ClientSettingsPacketId)
                {
                    _logger.LogError($"[Login] Expected byte {InboundPackets.ClientSettingsPacketId} Client Settings, received 0x{clientSettingsId:x2} instead. Closing socket.");
                    Dispose();
                    return;
                }
                var locale                   = _stream.ReadString();
                var viewDistance             = _stream.Read <sbyte>();
                var chatMode                 = _stream.ReadVarInt();
                var chatColors               = _stream.Read <bool>();
                var displayedSkinPartBitMask = _stream.ReadByte();
                var mainHand                 = _stream.ReadVarInt();
                _logger.LogDebug("[Login] User " + username + " registered client settings with locale " + locale + ", view distance " + Convert.ToInt32(viewDistance) + ", and main hand " + mainHand);

                // AT SOME POINT, CHUNK SOME DATA HERE

                // Inform client of global spawn
                var spawnPositionPacket = new SpawnPositionPacket(25, 50, 2);
                _writer.WritePacket(spawnPositionPacket);

                // Send client the data of player
                double playerX    = 1.0;
                double playerY    = 1.0;
                double playerZ    = 1.0;
                float  yaw        = 1.0f;
                float  pitch      = 1.0f;
                byte   flags      = 0x00;
                int    teleportId = 5;

                var ppalPacket = new PlayerPositionAndLookPacket(playerX, playerY, playerZ, yaw, pitch, flags, teleportId);
                _writer.WritePacket(ppalPacket);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Gets a packet from the server and returns it.
        /// </summary>
        /// <returns>The received packet.</returns>
        public Packet GetPacket()
        {
            var packetID = (byte)_stream.ReadByte();

            //_log.Debug("Got packet ID: " + packetID); // Spammy debug message
            if (!Enum.IsDefined(typeof(PacketType), (int)packetID))
            {
                return(null);
            }
            var    type = (PacketType)packetID;
            Packet pack = null;

            switch (type)
            {
            case PacketType.KeepAlive:
                pack = new KeepAlivePacket(_tools.ReadInt32());
                break;

            case PacketType.LoginRequest:
                pack = new LoginRequestPacketSC
                {
                    EntityID    = _tools.ReadInt32(),
                    NotUsed     = _tools.ReadString(),
                    LevelType   = _tools.ReadString(),
                    Gamemode    = _tools.ReadInt32(),
                    Dimension   = _tools.ReadInt32(),
                    Difficulty  = _tools.ReadSignedByte(),
                    WorldHeight = _tools.ReadByte(),                             // Not Used
                    MaxPlayers  = _tools.ReadByte()
                };
                break;

            case PacketType.Handshake:
                pack = new HandshakePacketSC(_tools.ReadString());
                break;

            case PacketType.ChatMessage:
                pack = new ChatMessagePacket(_tools.ReadString());
                break;

            case PacketType.TimeUpdate:
                pack = new TimeUpdatePacket(_tools.ReadInt32());
                break;

            case PacketType.EntityEquipment:
                pack = new EntityEquipmentPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadInt16(), _tools.ReadInt16());
                break;

            case PacketType.SpawnPosition:
                pack = new SpawnPositionPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32());
                break;

            case PacketType.UseEntity:
                pack = new UseEntityPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadBoolean());
                break;

            case PacketType.UpdateHealth:
                pack = new UpdateHealthPacket(_tools.ReadInt16(), _tools.ReadInt16(), _tools.ReadSingle());
                break;

            case PacketType.Respawn:
                pack = new RespawnPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                         _tools.ReadInt16(), _tools.ReadString());
                break;

            case PacketType.Player:
                pack = new PlayerPacket(_tools.ReadBoolean());
                break;

            case PacketType.PlayerPosition:
                pack = new PlayerPositionPacket(_tools.ReadDouble(), _tools.ReadDouble(), _tools.ReadDouble(), _tools.ReadDouble(),
                                                _tools.ReadBoolean());
                break;

            case PacketType.PlayerLook:
                pack = new PlayerLookPacket(_tools.ReadSingle(), _tools.ReadSingle(), _tools.ReadBoolean());
                break;

            case PacketType.PlayerPositionAndLook:
                pack = new PlayerPositionAndLookPacket(_tools.ReadDouble(), _tools.ReadDouble(), _tools.ReadDouble(),
                                                       _tools.ReadDouble(), _tools.ReadSingle(), _tools.ReadSingle(),
                                                       _tools.ReadBoolean());
                break;

            case PacketType.PlayerDigging:
                pack = new PlayerDiggingPacket(_tools.ReadSignedByte(), _tools.ReadInt32(), _tools.ReadSignedByte(),
                                               _tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.PlayerBlockPlacement:
                pack = new PlayerBlockPlacementPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadInt32(),
                                                      _tools.ReadSignedByte(), _tools.ReadSlotData());
                break;

            case PacketType.UseBed:
                pack = new UseBedPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadInt32(), _tools.ReadSignedByte(),
                                        _tools.ReadInt32());
                break;

            case PacketType.Animation:
                pack = new AnimationPacket(_tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.NamedEntitySpawn:
                pack = new NamedEntitySpawnPacket(_tools.ReadInt32(), _tools.ReadString(), _tools.ReadInt32(), _tools.ReadInt32(),
                                                  _tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                                  _tools.ReadInt16());
                break;

            case PacketType.PickupSpawn:
                pack = new PickupSpawnPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadSignedByte(), _tools.ReadInt16(),
                                             _tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadSignedByte(),
                                             _tools.ReadSignedByte(), _tools.ReadSignedByte());
                break;

            case PacketType.CollectItem:
                pack = new CollectItemPacket(_tools.ReadInt32(), _tools.ReadInt32());
                break;

            case PacketType.AddObjectVehicle:
                pack = new AddObjectVehiclePacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadInt32(),
                                                  _tools.ReadInt32(), _tools.ReadInt32());
                var ftEid = _tools.ReadInt32(); ((AddObjectVehiclePacket)pack).FireballThrowerID = ftEid;
                if (ftEid > 0)
                {
                    var aovpPack = (AddObjectVehiclePacket)pack;
                    aovpPack.SpeedX = _tools.ReadInt16();
                    aovpPack.SpeedY = _tools.ReadInt16();
                    aovpPack.SpeedZ = _tools.ReadInt16();
                    pack            = aovpPack;
                }
                break;

            case PacketType.HoldingChange:
                pack = new HoldingChangePacket(_tools.ReadInt16());
                break;

            case PacketType.MobSpawn:
                pack = new MobSpawnPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadInt32(), _tools.ReadInt32(),
                                          _tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                          _tools.ReadSignedByte(), _tools.ReadEntityMetadata());
                break;

            case PacketType.EntityPainting:
                pack = new EntityPaintingPacket(_tools.ReadInt32(), _tools.ReadString(), _tools.ReadInt32(),
                                                _tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32());
                break;

            case PacketType.ExperienceOrb:
                pack = new ExperienceOrbPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32(),
                                               _tools.ReadInt32(), _tools.ReadInt16());
                break;

            case PacketType.EntityVelocity:
                pack = new EntityVelocityPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadInt16(), _tools.ReadInt16());
                break;

            case PacketType.DestroyEntity:
                pack = new DestroyEntityPacket(_tools.ReadInt32());
                break;

            case PacketType.Entity:
                pack = new EntityPacket(_tools.ReadInt32());
                break;

            case PacketType.EntityRelativeMove:
                pack = new EntityRelativeMovePacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                                    _tools.ReadSignedByte());
                break;

            case PacketType.EntityLook:
                pack = new EntityLookPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte());
                break;

            case PacketType.EntityLookAndRelativeMove:
                pack = new EntityLookAndRelativeMovePacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                                           _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                                           _tools.ReadSignedByte());
                break;

            case PacketType.EntityTeleport:
                pack = new EntityTeleportPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadInt32(),
                                                _tools.ReadSignedByte(), _tools.ReadSignedByte());
                break;

            case PacketType.EntityHeadLook:
                pack = new EntityHeadLookPacket(_tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.EntityStatus:
                pack = new EntityStatusPacket(_tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.AttachEntity:
                pack = new AttachEntityPacket(_tools.ReadInt32(), _tools.ReadInt32());
                break;

            case PacketType.EntityMetadata:
                var entityMetadataPacket = new EntityMetadataPacket(_tools.ReadInt32());

                var metaData = new List <sbyte>();
                var b        = true;
                while (b)
                {
                    var value = _tools.ReadSignedByte();
                    if (value == 127)
                    {
                        b = false;
                    }
                    metaData.Add(value);
                }
                entityMetadataPacket.Metadata = metaData.ToArray();

                break;

            case PacketType.EntityEffect:
                pack = new EntityEffectPacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadSignedByte(),
                                              _tools.ReadInt16());
                break;

            case PacketType.RemoveEntityEffect:
                pack = new RemoveEntityEffectPacket(_tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.Experience:
                pack = new ExperiencePacket(_tools.ReadSingle(), _tools.ReadInt16(), _tools.ReadInt16());
                break;

            case PacketType.PreChunk:
                pack = new PreChunkPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadBoolean());
                break;

            case PacketType.MapChunk:
                var mapChunkPacket = new MapChunkPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadInt32(),
                                                        _tools.ReadSignedByte(), _tools.ReadSignedByte(), _tools.ReadSignedByte());

                mapChunkPacket.CompressedSize = _tools.ReadInt32();
                mapChunkPacket.CompressedData = _tools.ReadSignedBytes(mapChunkPacket.CompressedSize);

                pack = mapChunkPacket;
                break;

            case PacketType.MultiBlockChange:
                var multiBlockChangePacket = new MultiBlockChangePacket(_tools.ReadInt32(), _tools.ReadInt32());

                var arraySize = _tools.ReadInt16();
                multiBlockChangePacket.ArraySize = arraySize;

                var coordinates = new short[arraySize, 3];
                for (short i = 0; i < arraySize; i++)
                {
                    coordinates[i, 0] = _tools.ReadInt16();
                    coordinates[i, 1] = _tools.ReadInt16();
                    coordinates[i, 2] = _tools.ReadInt16();
                }
                multiBlockChangePacket.Coordinates = coordinates;

                var types = new sbyte[arraySize];
                for (short i = 0; i < arraySize; i++)
                {
                    types[i] = _tools.ReadSignedByte();
                }
                multiBlockChangePacket.Types = types;

                var metadata = new sbyte[arraySize];
                for (short i = 0; i < arraySize; i++)
                {
                    metadata[i] = _tools.ReadSignedByte();
                }
                multiBlockChangePacket.Metadata = metadata;

                pack = multiBlockChangePacket;
                break;

            case PacketType.BlockChange:
                pack = new BlockChangePacket(_tools.ReadInt32(), _tools.ReadSignedByte(), _tools.ReadInt32(),
                                             _tools.ReadSignedByte(), _tools.ReadSignedByte());
                break;

            case PacketType.BlockAction:
                pack = new BlockActionPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadInt32(), _tools.ReadSignedByte(),
                                             _tools.ReadSignedByte());
                break;

            case PacketType.Explosion:
                var explosionPacket = new ExplosionPacket(_tools.ReadDouble(), _tools.ReadDouble(), _tools.ReadDouble(), _tools.ReadSingle());

                var recordCount = _tools.ReadInt32();
                explosionPacket.Count = recordCount;

                var records = new sbyte[recordCount, 3];
                for (var i = 0; i < recordCount; i++)
                {
                    records[i, 0] = _tools.ReadSignedByte();
                    records[i, 1] = _tools.ReadSignedByte();
                    records[i, 2] = _tools.ReadSignedByte();
                }
                explosionPacket.Records = records;

                pack = explosionPacket;
                break;

            case PacketType.SoundParticleEffect:
                pack = new SoundParticleEffectPacket(_tools.ReadInt32(), _tools.ReadInt32(), _tools.ReadSignedByte(),
                                                     _tools.ReadInt32(), _tools.ReadInt32());
                break;

            case PacketType.NewInvalidState:
                pack = new NewInvalidStatePacket(_tools.ReadSignedByte(), _tools.ReadSignedByte());
                break;

            case PacketType.Thunderbolt:
                pack = new ThunderboltPacket(_tools.ReadInt32(), _tools.ReadBoolean(), _tools.ReadInt32(), _tools.ReadInt32(),
                                             _tools.ReadInt32());
                break;

            case PacketType.OpenWindow:
                pack = new OpenWindowPacket(_tools.ReadSignedByte(), _tools.ReadSignedByte(), _tools.ReadString(),
                                            _tools.ReadSignedByte());
                break;

            case PacketType.CloseWindow:
                pack = new CloseWindowPacket(_tools.ReadSignedByte());
                break;

            case PacketType.SetSlot:
                pack = new SetSlotPacket(_tools.ReadSignedByte(), _tools.ReadInt16(), _tools.ReadSlotData());
                break;

            case PacketType.WindowItems:
                var windowItemsPacket = new WindowItemsPacket(_tools.ReadSignedByte());

                var count = _tools.ReadInt16();
                windowItemsPacket.Count = count;

                var slotData = new SlotData[count];
                for (short i = 0; i < count; i++)
                {
                    slotData[i] = _tools.ReadSlotData();
                }
                windowItemsPacket.SlotData = slotData;

                pack = windowItemsPacket;
                break;

            case PacketType.UpdateWindowProperty:
                pack = new UpdateWindowPropertyPacket(_tools.ReadSignedByte(), _tools.ReadInt16(), _tools.ReadInt16());
                break;

            case PacketType.Transaction:
                pack = new TransactionPacket(_tools.ReadSignedByte(), _tools.ReadInt16(), _tools.ReadBoolean());
                break;

            case PacketType.CreativeInventoryAction:
                pack = new CreativeInventoryActionPacket(_tools.ReadInt16(), _tools.ReadSlotData());
                break;

            case PacketType.UpdateSign:
                pack = new UpdateSignPacket(_tools.ReadInt32(), _tools.ReadInt16(), _tools.ReadInt32(), _tools.ReadString(),
                                            _tools.ReadString(), _tools.ReadString(), _tools.ReadString());
                break;

            case PacketType.ItemData:
                pack = new ItemDataPacket(_tools.ReadInt16(), _tools.ReadInt16());

                byte len = _tools.ReadByte();
                ((ItemDataPacket)pack).Length = len;
                ((ItemDataPacket)pack).Text   = _tools.ReadSignedBytes(len);

                break;

            case PacketType.IncrementStatistic:
                pack = new IncrementStatisticPacket(_tools.ReadInt32(), _tools.ReadSignedByte());
                break;

            case PacketType.PlayerListItem:
                pack = new PlayerListItemPacket(_tools.ReadString(), _tools.ReadBoolean(), _tools.ReadInt16());
                break;

            case PacketType.PluginMessage:
                pack = new PluginMessagePacket(_tools.ReadString());
                var length = _tools.ReadInt16(); ((PluginMessagePacket)pack).Length = length;
                ((PluginMessagePacket)pack).Data = _tools.ReadSignedBytes(length);
                break;

            case PacketType.DisconnectKick:
                pack = new DisconnectKickPacket(_tools.ReadString());
                break;
            }

            return(pack);
        }
Esempio n. 6
0
        /// <summary>
        /// Handles all packets received.
        /// </summary>
        /// <param name="sender">N/A (Not Used) (See MSDN)</param>
        /// <param name="e"><see cref="PacketEventArgs" /> containing the packet and other info.</param>
        private void PacketReceived(object sender, PacketEventArgs e)
        {
            Packet response;

            switch (e.Packet.Type)
            {
            case PacketType.KeepAlive:
                response = new KeepAlivePacket(((KeepAlivePacket)e.Packet).KeepAliveID);
                //_log.Debug("Client received KeepAlive request, sending KeepAlive packet...");
                _protocol.SendPacket(response);
                break;

            case PacketType.LoginRequest:
                ParseLoginRequestSC((LoginRequestPacketSC)e.Packet);
                break;

            case PacketType.ChatMessage:
                ReceiveMessage(((ChatMessagePacket)e.Packet).Message);
                break;

            case PacketType.SpawnPosition:
                var spPack = (SpawnPositionPacket)e.Packet;
                _log.Debug(string.Format("Received SpawnPosition packet: {0}, {1}, {2}. Updating world spawn...", spPack.X, spPack.Y, spPack.Z));
                _world.SetSpawn(spPack.X, spPack.Y, spPack.Z);
                _log.Debug("Spawn position set!");
                break;

            case PacketType.Player:
                var playerPack = (PlayerPacket)e.Packet;
                response = new PlayerPacket(playerPack.OnGround);
                _log.Debug("Updating player OnGround...");
                _player.SetOnGround(playerPack.OnGround);
                _log.Debug("Client received Player packet, responding with identical packet...");
                _protocol.SendPacket(response);
                break;

            case PacketType.PlayerPosition:
                var pPack = (PlayerPositionPacket)e.Packet;
                response = new PlayerPositionPacket(pPack.X, pPack.Y, pPack.Stance, pPack.Z, pPack.OnGround);
                _log.Debug("Updating player position...");
                _player.SetPosition(pPack.X, pPack.Y, pPack.Z);
                _player.SetStance(pPack.Stance);
                _player.SetOnGround(pPack.OnGround);
                _log.Debug("Client received PlayerPosition packet (0x0B), responding with identical packet...");
                _protocol.SendPacket(response);
                break;

            case PacketType.PlayerLook:
                var lPack = (PlayerLookPacket)e.Packet;
                response = new PlayerLookPacket(lPack.Yaw, lPack.Pitch, lPack.OnGround);
                _log.Debug("Updating player look...");
                _player.SetDirection(lPack.Yaw, lPack.Pitch);
                _player.SetOnGround(lPack.OnGround);
                _log.Debug("Client received PlayerLook packet (0x0C), responding with identical packet...");
                _protocol.SendPacket(response);
                break;

            case PacketType.PlayerPositionAndLook:
                var plPack = (PlayerPositionAndLookPacket)e.Packet;
                response = new PlayerPositionAndLookPacket
                {
                    X        = plPack.X,
                    Y        = plPack.Y,
                    Z        = plPack.Y,
                    Stance   = plPack.Stance,
                    Yaw      = plPack.Yaw,
                    Pitch    = plPack.Pitch,
                    OnGround = plPack.OnGround
                };
                _log.Debug("Updating player position and look...");
                _player.SetPosition(plPack.X, plPack.Y, plPack.Z);
                _player.SetDirection(plPack.Yaw, plPack.Pitch);
                _player.SetStance(plPack.Stance);
                _player.SetOnGround(plPack.OnGround);
                _log.Debug("Client received PlayerPositionAndLook packet (0x0D), responding with identical packet...");
                _protocol.SendPacket(response);
                break;

            case PacketType.DisconnectKick:
                _log.Debug("Client DISCONNECT or KICK with reason: " + ((DisconnectKickPacket)e.Packet).Reason);
                _listener.Stop();
                break;

            default:
                //_log.Warn("Received packet: " + e.Packet.Type + " but Client is not configured to respond to this packet!");
                break;
            }
        }
Esempio n. 7
0
        public void Handle(int id, MemoryStream ms)
        {
            if (_loginSucces)
            {
                IPacket packet = null;
                switch (id)
                {
                case BlockChangePacket.PACKET_ID:
                    packet = new BlockChangePacket(ms);
                    break;

                case ChatMessagePacket.PACKET_ID:
                    packet = new ChatMessagePacket(ms);
                    break;

                case UpdateHealthPacket.PACKET_ID:
                    packet = new UpdateHealthPacket(ms);
                    break;

                case ChunkDataPacket.PACKET_ID:
                    packet = new ChunkDataPacket(ms);
                    break;

                case DisconnectPacket.PACKET_ID:
                    packet = new DisconnectPacket(ms);
                    break;

                case KeepAlivePacket.PACKET_ID:
                    packet = new KeepAlivePacket(ms);
                    break;

                case PlayerAbilitiesPacket.PACKET_ID:
                    packet = new PlayerAbilitiesPacket(ms);
                    break;

                case PlayerPositionAndLookPacket.PACKET_ID:
                    packet = new PlayerPositionAndLookPacket(ms);
                    break;

                case SpawnExperienceOrbPacket.PACKET_ID:
                    packet = new SpawnExperienceOrbPacket(ms);
                    break;

                case SpawnGlobalEntityPacket.PACKET_ID:
                    packet = new SpawnGlobalEntityPacket(ms);
                    break;

                case SpawnObjectPacket.PACKET_ID:
                    packet = new SpawnObjectPacket(ms);
                    break;

                case UnloadChunkPacket.PACKET_ID:
                    packet = new UnloadChunkPacket(ms);
                    break;

                case SpawnPositionPacket.PACKET_ID:
                    packet = new SpawnPositionPacket(ms);
                    break;
                }

                if (packet != null && PlayPacketReceivedEvent != null)
                {
                    this.PlayPacketReceivedEvent(packet);
                }
            }
            else
            {
                IPacket packet = null;
                switch (id)
                {
                case Packet.Clientbound.Login.DisconnectPacket.PACKET_ID:
                    packet = new Packet.Clientbound.Login.DisconnectPacket(ms);
                    break;

                case Packet.Clientbound.Login.LoginSuccessPacket.PACKET_ID:
                    packet = new Packet.Clientbound.Login.LoginSuccessPacket(ms);
                    break;

                case Packet.Clientbound.Login.SetCompressionPacket.PACKET_ID:
                    packet = new Packet.Clientbound.Login.SetCompressionPacket(ms);
                    break;
                }

                if (packet != null && LoginPacketReceivedEvent != null)
                {
                    this.LoginPacketReceivedEvent(packet);
                }
            }
            ms.Dispose();
        }