private void HandlePlayerBlockPlacement(Client client, PlayerBlockPlacementPacket packet) { // TODO: Check if player is close enough to place the block; check if player is placing on top of another block, etc. if (packet.Amount >= 0 && packet.BlockID > 0 && packet.BlockID <= 121) { int placeX = packet.X; int placeY = packet.Y; int placeZ = packet.Z; switch (packet.Direction) { case 0: placeY--; break; case 1: placeY++; break; case 2: placeZ--; break; case 3: placeZ++; break; case 4: placeX--; break; case 5: placeX++; break; } WorldLocation bLoc = new WorldLocation(placeX, placeY, placeZ); Nullable <Block> nb = Server.GetWorldManager().GetWorld(0).GetBlock(bLoc); if (nb != null) { Block b = (Block)nb; b.SetBlockType((BlockType)packet.BlockID); Server.OnBlockChange(b); } } }
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); }