Skip to content

Commit 4a2fe09

Browse files
committed
fix : adjusted the protocol_id to correctly display a pig and not a phantom
1 parent 5e920f9 commit 4a2fe09

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/bin/src/systems/entities/entity_sync.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use ferrumc_core::transform::rotation::Rotation;
55
use ferrumc_entities::components::*; // Includes SyncedToPlayers
66
use ferrumc_entities::types::passive::pig::EntityUuid;
77
use ferrumc_net::connection::StreamWriter;
8+
use ferrumc_net::packets::outgoing::entity_metadata::{EntityMetadata, EntityMetadataPacket};
89
use ferrumc_net::packets::outgoing::spawn_entity::SpawnEntityPacket;
10+
use ferrumc_net_codec::net_types::var_int::VarInt;
911
use tracing::{debug, error};
1012

1113
// Type alias to simplify the complex Query type
@@ -46,15 +48,33 @@ pub fn entity_sync_system(
4648

4749
// Create and send spawn packet
4850
let protocol_id = entity_type.protocol_id();
51+
52+
// TODO: INVESTIGATE PROTOCOL_ID OFFSET BUG
53+
// There is a systematic -1 offset between what we send and what the client displays:
54+
// - Registry says Pig=94, Phantom=93
55+
// - When sending 94, client displays Phantom (93)
56+
// - When sending 95, client displays Pig (94)
57+
// Possible causes:
58+
// 1. Registry version mismatch (registry for 1.21.7 but client is 1.21.8?)
59+
// 2. VarInt encoding issue
60+
// 3. Protocol expects 1-based indexing instead of 0-based
61+
// For now, adding +1 as a workaround until root cause is found
62+
let adjusted_protocol_id = protocol_id + 1;
63+
debug!(
64+
"Spawning {:?} (registry_id={}, sending={}) at ({:.2}, {:.2}, {:.2}) for player {:?}",
65+
entity_type, protocol_id, adjusted_protocol_id, pos.x, pos.y, pos.z, player_entity
66+
);
4967
debug!(
50-
"Spawning {:?} (protocol_id={}) at ({:.2}, {:.2}, {:.2}) for player {:?}",
51-
entity_type, protocol_id, pos.x, pos.y, pos.z, player_entity
68+
"DEBUG: entity_id={}, uuid={}, type_id={}, data=0",
69+
entity_id.0,
70+
entity_uuid.0.as_u128(),
71+
adjusted_protocol_id
5272
);
5373

5474
let spawn_packet = SpawnEntityPacket::entity(
5575
entity_id.0,
5676
entity_uuid.0.as_u128(),
57-
protocol_id,
77+
adjusted_protocol_id,
5878
pos,
5979
rot,
6080
);
@@ -64,9 +84,20 @@ pub fn entity_sync_system(
6484
continue;
6585
}
6686

67-
// TODO: Send EntityMetadataPacket here to properly display the entity
68-
// The EntityMetadata constructors are not publicly exported yet
69-
// This might be why the pig appears as a phantom!
87+
// Send EntityMetadataPacket to properly display the entity
88+
// We need to send both entity flags (index 0) and pose (index 6)
89+
let metadata_packet = EntityMetadataPacket::new(
90+
VarInt::new(entity_id.0),
91+
[
92+
EntityMetadata::entity_normal_state(), // Index 0: normal entity state (no special flags)
93+
EntityMetadata::entity_standing(), // Index 6: standing pose
94+
],
95+
);
96+
97+
if let Err(e) = stream_writer.send_packet(metadata_packet) {
98+
error!("Failed to send entity metadata packet: {:?}", e);
99+
continue;
100+
}
70101

71102
synced.player_entities.push(player_entity);
72103
debug!(

src/lib/net/src/packets/outgoing/entity_metadata.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ pub mod constructors {
6565
value,
6666
}
6767
}
68+
69+
/// Entity with normal state (no special flags)
70+
pub fn entity_normal_state() -> Self {
71+
Self::new(
72+
EntityMetadataIndexType::Byte,
73+
EntityMetadataValue::Entity0(EntityStateMask::new()),
74+
)
75+
}
76+
6877
/// To hide the name tag and stuff
6978
pub fn entity_sneaking_pressed() -> Self {
7079
Self::new(

0 commit comments

Comments
 (0)