private void HandlePlayerPositionLook(Client client, PlayerPositionLookPacket packet) { // TODO: See above // Update player position & look Location newLoc = new Location(packet.X, packet.Y, packet.Z); // if (Location.Distance(client.Player.Location, newLoc) > 15 && client.Spawned) // client.Dispose(); byte xOff, yOff, zOff; xOff = (byte)(-1 * Math.Min((int)(client.Player.Location.X - newLoc.X) * 32, 128)); yOff = (byte)(-1 * Math.Min((int)(client.Player.Location.Y - newLoc.Y) * 32, 128)); zOff = (byte)(-1 * Math.Min((int)(client.Player.Location.Z - newLoc.Z) * 32, 128)); // TODO: Pitch & Yaw Server.BroadcastPacket(new EntityLookRelativeMovePacket(client.Player.EntityID, xOff, yOff, zOff, 0, 0), client); client.Player.Location = newLoc; client.Player.Yaw = packet.Yaw; client.Player.Pitch = packet.Pitch; if (!client.Spawned) { client.Spawned = true; Server.BroadcastPacket(new NamedEntitySpawnPacket(client.Player.EntityID, client.Player.Username, (int)client.Player.Location.X, (int)client.Player.Location.Y, (int)client.Player.Location.Z, 0, 0, 0), client); } }
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); }