11//! Packet forwarders for [`Difficulty`] resource.
22
3- use bevy:: ecs:: change_detection:: DetectChanges as _;
43use bevy:: ecs:: entity:: Entity ;
54use bevy:: ecs:: message:: MessageWriter ;
6- use bevy:: ecs:: query:: { Added , With } ;
7- use bevy:: ecs:: system:: { Query , Res } ;
5+ use bevy:: ecs:: query:: { Added , Changed , With } ;
6+ use bevy:: ecs:: system:: Query ;
7+ use meloncraft_entity:: position:: EntityPosition ;
88use meloncraft_packets:: ClientboundChangeDifficulty ;
99use meloncraft_player:: PlayerMarker ;
1010use meloncraft_server_info:: difficulty:: Difficulty ;
1111
1212pub fn send_difficulty_on_join (
13- new_player_q : Query < Entity , Added < PlayerMarker > > ,
14- difficulty : Res < Difficulty > ,
13+ new_player_q : Query < ( Entity , & EntityPosition ) , Added < PlayerMarker > > ,
14+ difficulty : Query < & Difficulty > ,
1515 mut change_difficulty_pw : MessageWriter < ClientboundChangeDifficulty > ,
1616) {
17- for client in new_player_q {
17+ for ( client, player_position) in new_player_q {
18+ let difficulty = difficulty. get ( player_position. world ) . unwrap ( ) ;
1819 change_difficulty_pw. write ( ClientboundChangeDifficulty {
1920 client,
2021 difficulty : * difficulty,
@@ -24,19 +25,20 @@ pub fn send_difficulty_on_join(
2425}
2526
2627pub fn send_difficulty_on_change (
27- player_q : Query < Entity , With < PlayerMarker > > ,
28- difficulty : Res < Difficulty > ,
28+ player_q : Query < ( Entity , & EntityPosition ) , With < PlayerMarker > > ,
29+ difficulty_q : Query < ( Entity , & Difficulty ) , Changed < Difficulty > > ,
2930 mut change_difficulty_pw : MessageWriter < ClientboundChangeDifficulty > ,
3031) {
31- if !difficulty. is_changed ( ) {
32- return ;
33- }
34-
35- for client in player_q {
36- change_difficulty_pw. write ( ClientboundChangeDifficulty {
37- client,
38- difficulty : * difficulty,
39- difficulty_locked : false ,
40- } ) ;
32+ for ( world, difficulty) in difficulty_q {
33+ for ( client, player_position) in player_q {
34+ if player_position. world != world {
35+ continue ;
36+ }
37+ change_difficulty_pw. write ( ClientboundChangeDifficulty {
38+ client,
39+ difficulty : * difficulty,
40+ difficulty_locked : false ,
41+ } ) ;
42+ }
4143 }
4244}
0 commit comments