@@ -6,6 +6,8 @@ use ferrumc_entities::components::*; // Includes SyncedToPlayers
66use ferrumc_entities:: types:: passive:: pig:: EntityUuid ;
77use ferrumc_net:: connection:: StreamWriter ;
88use ferrumc_net:: packets:: outgoing:: spawn_entity:: SpawnEntityPacket ;
9+ use ferrumc_net:: packets:: outgoing:: entity_metadata:: { EntityMetadata , EntityMetadataPacket } ;
10+ use ferrumc_net_codec:: net_types:: var_int:: VarInt ;
911use tracing:: { debug, error} ;
1012
1113// Type alias to simplify the complex Query type
@@ -46,15 +48,31 @@ 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 , entity_uuid . 0 . as_u128 ( ) , adjusted_protocol_id
5270 ) ;
5371
5472 let spawn_packet = SpawnEntityPacket :: entity (
5573 entity_id. 0 ,
5674 entity_uuid. 0 . as_u128 ( ) ,
57- protocol_id ,
75+ adjusted_protocol_id ,
5876 pos,
5977 rot,
6078 ) ;
@@ -64,9 +82,20 @@ pub fn entity_sync_system(
6482 continue ;
6583 }
6684
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!
85+ // Send EntityMetadataPacket to properly display the entity
86+ // We need to send both entity flags (index 0) and pose (index 6)
87+ let metadata_packet = EntityMetadataPacket :: new (
88+ VarInt :: new ( entity_id. 0 ) ,
89+ [
90+ EntityMetadata :: entity_normal_state ( ) , // Index 0: normal entity state (no special flags)
91+ EntityMetadata :: entity_standing ( ) , // Index 6: standing pose
92+ ] ,
93+ ) ;
94+
95+ if let Err ( e) = stream_writer. send_packet ( metadata_packet) {
96+ error ! ( "Failed to send entity metadata packet: {:?}" , e) ;
97+ continue ;
98+ }
7099
71100 synced. player_entities . push ( player_entity) ;
72101 debug ! (
0 commit comments