From 6fc5d07699d3c68a653821af3a6d6cf8fca8e719 Mon Sep 17 00:00:00 2001 From: p2r3 Date: Fri, 22 Aug 2025 13:41:19 +0300 Subject: [PATCH] support common miscellaneous packets --- include/packets.h | 1 + src/main.c | 26 ++++++++++++++++++++------ src/packets.c | 7 +++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/packets.h b/include/packets.h index 98db4dc..9fa5632 100644 --- a/include/packets.h +++ b/include/packets.h @@ -10,6 +10,7 @@ int cs_useItemOn (int client_fd); int cs_setPlayerPositionAndRotation (int client_fd, double *x, double *y, double *z, float *yaw, float *pitch, uint8_t *on_ground); int cs_setPlayerPosition (int client_fd, double *x, double *y, double *z, uint8_t *on_ground); int cs_setPlayerRotation (int client_fd, float *yaw, float *pitch, uint8_t *on_ground); +int cs_setPlayerMovementFlags (int client_fd, uint8_t *on_ground); int cs_setHeldItem (int client_fd); int cs_clickContainer (int client_fd); int cs_closeContainer (int client_fd); diff --git a/src/main.c b/src/main.c index 915daa8..1f80d5d 100644 --- a/src/main.c +++ b/src/main.c @@ -157,9 +157,19 @@ void handlePacket (int client_fd, int length, int packet_id) { } break; + case 0x1B: { + if (state == STATE_PLAY) { + // Serverbound keep-alive (ignored) + recv_all(client_fd, recv_buffer, 8, false); + return; + } + break; + } + case 0x1D: case 0x1E: case 0x1F: + case 0x20: if (state == STATE_PLAY) { double x, y, z; @@ -169,17 +179,12 @@ void handlePacket (int client_fd, int length, int packet_id) { // Read player position (and rotation) if (packet_id == 0x1D) cs_setPlayerPosition(client_fd, &x, &y, &z, &on_ground); else if (packet_id == 0x1F) cs_setPlayerRotation (client_fd, &yaw, &pitch, &on_ground); + else if (packet_id == 0x20) cs_setPlayerMovementFlags (client_fd, &on_ground); else cs_setPlayerPositionAndRotation(client_fd, &x, &y, &z, &yaw, &pitch, &on_ground); PlayerData *player; if (getPlayerData(client_fd, &player)) break; - // Update rotation in player data (if applicable) - if (packet_id != 0x1D) { - player->yaw = ((short)(yaw + 540) % 360 - 180) * 127 / 180; - player->pitch = pitch / 90.0f * 127.0f; - } - // Handle fall damage if (on_ground) { int8_t damage = player->grounded_y - player->y - 3; @@ -192,6 +197,15 @@ void handlePacket (int client_fd, int length, int packet_id) { player->grounded_y = player->y; } + // Don't continue if all we got were flags + if (packet_id == 0x20) return; + + // Update rotation in player data (if applicable) + if (packet_id != 0x1D) { + player->yaw = ((short)(yaw + 540) % 360 - 180) * 127 / 180; + player->pitch = pitch / 90.0f * 127.0f; + } + // Broadcast player position to other players #ifdef SCALE_MOVEMENT_UPDATES_TO_PLAYER_COUNT // If applicable, broadcast only every client_count-th movement update diff --git a/src/packets.c b/src/packets.c index 96aa18f..30d0637 100644 --- a/src/packets.c +++ b/src/packets.c @@ -651,6 +651,13 @@ int cs_setPlayerRotation (int client_fd, float *yaw, float *pitch, uint8_t *on_g return 0; } +int cs_setPlayerMovementFlags (int client_fd, uint8_t *on_ground) { + + *on_ground = readByte(client_fd) & 0x01; + + return 0; +} + // C->S Set Held Item (serverbound) int cs_setHeldItem (int client_fd) {