1
- use std:: { borrow:: Cow , cell :: UnsafeCell } ;
1
+ use std:: borrow:: Cow ;
2
2
3
3
use anyhow:: Context ;
4
4
use flecs_ecs:: prelude:: * ;
5
5
use glam:: Vec3 ;
6
6
use hyperion_inventory:: PlayerInventory ;
7
7
use hyperion_utils:: EntityExt ;
8
8
use tracing:: { error, info_span} ;
9
- use valence_ident:: ident;
10
9
use valence_protocol:: {
11
- ByteAngle , GameMode , RawBytes , VarInt , Velocity ,
12
- game_mode:: OptGameMode ,
10
+ ByteAngle , RawBytes , VarInt , Velocity ,
13
11
packets:: { play, play:: entity_equipment_update_s2c:: EquipmentEntry } ,
14
12
} ;
15
13
16
14
use crate :: {
17
15
Prev ,
18
- egress:: metadata:: show_all,
19
- net:: { Compose , NetworkStreamRef , agnostic} ,
16
+ net:: { Compose , NetworkStreamRef } ,
20
17
simulation:: {
21
- EntityReaction , Health , Pitch , Position , Xp , Yaw ,
22
- animation:: ActiveAnimation ,
23
- metadata:: { EntityFlags , MetadataBuilder , Pose } ,
18
+ EntityReaction , Pitch , Position , Xp , Yaw , animation:: ActiveAnimation ,
19
+ metadata:: MetadataChanges ,
24
20
} ,
25
- storage:: ThreadLocal ,
26
21
system_registry:: { SYNC_ENTITY_POSITION , SystemId } ,
27
22
util:: TracingExt ,
28
23
} ;
@@ -34,13 +29,11 @@ impl Module for EntityStateSyncModule {
34
29
fn module ( world : & World ) {
35
30
let system_id = SYNC_ENTITY_POSITION ;
36
31
37
- let metadata: ThreadLocal < UnsafeCell < MetadataBuilder > > = ThreadLocal :: new_defaults ( ) ;
38
-
39
32
world
40
33
. system_named :: < (
41
34
& Compose , // (0)
42
35
& NetworkStreamRef , // (1)
43
- & mut Prev < Xp > , // (2)
36
+ & mut ( Prev , Xp ) , // (2)
44
37
& mut Xp , // (3)
45
38
) > ( "entity_xp_sync" )
46
39
. term_at ( 0u32 )
@@ -62,7 +55,7 @@ impl Module for EntityStateSyncModule {
62
55
let net = table. field_unchecked :: < NetworkStreamRef > ( 1 ) ;
63
56
let net = net. get ( ..) . unwrap ( ) ;
64
57
65
- let mut prev_xp = table. field_unchecked :: < Prev < Xp > > ( 2 ) ;
58
+ let mut prev_xp = table. field_unchecked :: < Xp > ( 2 ) ;
66
59
let prev_xp = prev_xp. get_mut ( ..) . unwrap ( ) ;
67
60
let prev_xp: & mut [ u16 ] =
68
61
core:: slice:: from_raw_parts_mut ( prev_xp. as_mut_ptr ( ) . cast ( ) , count) ;
@@ -104,6 +97,29 @@ impl Module for EntityStateSyncModule {
104
97
}
105
98
} ) ;
106
99
100
+ system ! ( "entity_metadata_sync" , world, & Compose ( $) , & mut MetadataChanges )
101
+ . multi_threaded ( )
102
+ . kind :: < flecs:: pipeline:: OnStore > ( )
103
+ . tracing_each_entity (
104
+ info_span ! ( "entity_metadata_sync" ) ,
105
+ move |entity, ( compose, metadata_changes) | {
106
+ let world = entity. world ( ) ;
107
+ let entity_id = VarInt ( entity. minecraft_id ( ) ) ;
108
+
109
+ let metadata = metadata_changes. get_and_clear ( ) ;
110
+
111
+ if let Some ( view) = metadata {
112
+ let pkt = play:: EntityTrackerUpdateS2c {
113
+ entity_id,
114
+ tracked_values : RawBytes ( & view) ,
115
+ } ;
116
+
117
+ // todo(perf): do so locally
118
+ compose. broadcast ( & pkt, system_id) . send ( & world) . unwrap ( ) ;
119
+ }
120
+ } ,
121
+ ) ;
122
+
107
123
system ! (
108
124
"entity_state_sync" ,
109
125
world,
@@ -115,12 +131,6 @@ impl Module for EntityStateSyncModule {
115
131
& mut ActiveAnimation ,
116
132
& mut PlayerInventory ,
117
133
& mut EntityReaction ,
118
- & mut Health ,
119
- & mut Prev <Health >,
120
- & mut EntityFlags ,
121
- & mut Prev <EntityFlags >,
122
- & mut Pose ,
123
- & mut Prev <Pose >
124
134
)
125
135
. multi_threaded ( )
126
136
. kind :: < flecs:: pipeline:: OnStore > ( )
@@ -136,12 +146,6 @@ impl Module for EntityStateSyncModule {
136
146
animation,
137
147
inventory,
138
148
reaction,
139
- health,
140
- Prev ( prev_health) ,
141
- entity_flags,
142
- Prev ( prev_entity_flags) ,
143
- pose,
144
- Prev ( prev_pose) ,
145
149
) | {
146
150
let mut run = || {
147
151
let entity_id = VarInt ( entity. minecraft_id ( ) ) ;
@@ -150,75 +154,67 @@ impl Module for EntityStateSyncModule {
150
154
let io = * io;
151
155
152
156
let world = entity. world ( ) ;
153
- let observer = unsafe { & mut * metadata. get ( & world) . get ( ) } ;
154
157
155
158
let chunk_pos = position. to_chunk ( ) ;
156
159
157
- let pose_updated = * prev_pose != * pose;
158
-
159
- if pose_updated {
160
- observer. encode ( * pose) ;
161
- * prev_pose = * pose;
162
- }
163
-
164
- let health_updated = * prev_health != * health;
165
-
166
- if health_updated {
167
- let to = * health;
168
- let from = * prev_health;
169
-
170
- observer. encode ( * health) ;
171
- * prev_health = * health;
172
-
173
- if to < from {
174
- let pkt = play:: EntityDamageS2c {
175
- entity_id,
176
- source_type_id : VarInt :: default ( ) ,
177
- source_cause_id : VarInt :: default ( ) ,
178
- source_direct_id : VarInt :: default ( ) ,
179
- source_pos : None ,
180
- } ;
181
-
182
- compose. broadcast_local ( & pkt, chunk_pos, system_id) . send ( & world) ?;
183
-
184
- let packet = agnostic:: sound (
185
- ident ! ( "minecraft:entity.player.hurt" ) ,
186
- * * position,
187
- ) . build ( ) ;
188
-
189
- compose. broadcast_local ( & packet, chunk_pos, system_id) . send ( & world) ?;
190
- }
191
-
192
- if * to == 0.0 {
193
- // send respawn packet
194
- let pkt = play:: PlayerRespawnS2c {
195
- dimension_type_name : ident ! ( "minecraft:overworld" ) . into ( ) ,
196
- dimension_name : ident ! ( "minecraft:overworld" ) . into ( ) ,
197
- hashed_seed : 0 ,
198
- game_mode : GameMode :: Survival ,
199
- previous_game_mode : OptGameMode :: default ( ) ,
200
- is_debug : false ,
201
- is_flat : false ,
202
- copy_metadata : false ,
203
- last_death_location : None ,
204
- portal_cooldown : VarInt :: default ( ) ,
205
- } ;
206
- // position.position = PLAYER_SPAWN_POSITION;
207
- compose. unicast ( & pkt, io, system_id, & world) ?;
208
-
209
- * * health = 20.0 ;
210
-
211
- let show_all = show_all ( entity. minecraft_id ( ) ) ;
212
- compose. unicast ( show_all. borrow_packet ( ) , io, system_id, & world) ?;
213
- }
214
- }
215
-
216
- let entity_flags_updated = * prev_entity_flags != * entity_flags;
217
-
218
- if entity_flags_updated {
219
- observer. encode ( * entity_flags) ;
220
- * prev_entity_flags = * entity_flags;
221
- }
160
+ // let health_updated = *prev_health != *health;
161
+ //
162
+ // if health_updated {
163
+ // let to = *health;
164
+ // let from = *prev_health;
165
+ //
166
+ // observer.encode(*health);
167
+ // *prev_health = *health;
168
+ //
169
+ // if to < from {
170
+ // let pkt = play::EntityDamageS2c {
171
+ // entity_id,
172
+ // source_type_id: VarInt::default(),
173
+ // source_cause_id: VarInt::default(),
174
+ // source_direct_id: VarInt::default(),
175
+ // source_pos: None,
176
+ // };
177
+ //
178
+ // compose.broadcast_local(&pkt, chunk_pos, system_id).send(&world)?;
179
+ //
180
+ // let packet = agnostic::sound(
181
+ // ident!("minecraft:entity.player.hurt"),
182
+ // **position,
183
+ // ).build();
184
+ //
185
+ // compose.broadcast_local(&packet, chunk_pos, system_id).send(&world)?;
186
+ // }
187
+ //
188
+ // if *to == 0.0 {
189
+ // // send respawn packet
190
+ // let pkt = play::PlayerRespawnS2c {
191
+ // dimension_type_name: ident!("minecraft:overworld").into(),
192
+ // dimension_name: ident!("minecraft:overworld").into(),
193
+ // hashed_seed: 0,
194
+ // game_mode: GameMode::Survival,
195
+ // previous_game_mode: OptGameMode::default(),
196
+ // is_debug: false,
197
+ // is_flat: false,
198
+ // copy_metadata: false,
199
+ // last_death_location: None,
200
+ // portal_cooldown: VarInt::default(),
201
+ // };
202
+ // // position.position = PLAYER_SPAWN_POSITION;
203
+ // compose.unicast(&pkt, io, system_id, &world)?;
204
+ //
205
+ // **health = 20.0;
206
+ //
207
+ // let show_all = show_all(entity.minecraft_id());
208
+ // compose.unicast(show_all.borrow_packet(), io, system_id, &world)?;
209
+ // }
210
+ // }
211
+
212
+ // let entity_flags_updated = *prev_entity_flags != *entity_flags;
213
+
214
+ // if entity_flags_updated {
215
+ // observer.encode(*entity_flags);
216
+ // *prev_entity_flags = *entity_flags;
217
+ // }
222
218
223
219
let pkt = play:: EntityPositionS2c {
224
220
entity_id,
@@ -269,16 +265,6 @@ impl Module for EntityStateSyncModule {
269
265
reaction. velocity = Vec3 :: ZERO ;
270
266
}
271
267
272
-
273
- if let Some ( view) = observer. get_and_clear ( ) {
274
- let pkt = play:: EntityTrackerUpdateS2c {
275
- entity_id,
276
- tracked_values : RawBytes ( & view) ,
277
- } ;
278
-
279
- compose. broadcast_local ( & pkt, chunk_pos, system_id) . send ( & world) ?;
280
- }
281
-
282
268
for pkt in animation. packets ( entity_id) {
283
269
compose
284
270
. broadcast_local ( & pkt, chunk_pos, system_id)
0 commit comments