diff --git a/CHANGELOG.md b/CHANGELOG.md index fce86fd9..96c9186d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Rename `has_authority` condition into `server_or_singleplayer`. Old name still works, but marked as deprecated. - Make `ReplicatedClients::new` public. ## [0.28.1] - 2024-09-04 diff --git a/src/client/events.rs b/src/client/events.rs index 0ea610b5..d5d2dc09 100644 --- a/src/client/events.rs +++ b/src/client/events.rs @@ -30,7 +30,7 @@ impl Plugin for ClientEventsPlugin { PostUpdate, ( Self::send.run_if(client_connected), - Self::resend_locally.run_if(has_authority), + Self::resend_locally.run_if(server_or_singleplayer), ) .chain() .in_set(ClientSet::Send), diff --git a/src/core/common_conditions.rs b/src/core/common_conditions.rs index 5768f65d..bfb58e3e 100644 --- a/src/core/common_conditions.rs +++ b/src/core/common_conditions.rs @@ -10,7 +10,16 @@ pub fn server_running(server: Option>) -> bool { /// Returns `true` if there is no client or if the existing client is disconnected. /// /// Can be used for systems that run both on the server and in singleplayer mode. +#[deprecated(note = "Use `server_or_singleplayer`")] pub fn has_authority(client: Option>) -> bool { + server_or_singleplayer(client) +} + +/// Returns `true` if there is no client or if the existing client is disconnected. +/// +/// Can be used instead of the regular [`server_running`] to seamlessly support +/// singleplayer or listen-server mode (where server is also a player). +pub fn server_or_singleplayer(client: Option>) -> bool { let Some(client) = client else { return true; }; diff --git a/src/lib.rs b/src/lib.rs index e0490c9e..5c8759ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,8 +135,8 @@ This module is also available from [`prelude`]. For example, to display a "connecting" message, you can use [`client_connecting`]. But for gameplay systems, you most likely want to run them in both server and single-player -sessions. For example, damage registration or procedural generation systems. Use [`has_authority`] -condition for those cases. +sessions. For example, damage registration or procedural generation systems. +Use [`server_or_singleplayer`] condition for those cases. If you want your systems to run only on frames when the server sends updates to clients, use [`ServerSet::Send`]. @@ -307,7 +307,7 @@ also as a client with ID [`ClientId::SERVER`]. So you can send such events even and [`FromClient`] will be emitted for them too. This way your game logic will work the same on client, listen server and in single-player session. -For systems that receive events attach [`has_authority`] condition to receive a message +For systems that receive events attach [`server_or_singleplayer`] condition to receive a message on non-client instances (server or single-player): ``` @@ -317,7 +317,7 @@ on non-client instances (server or single-player): # let mut app = App::new(); # app.add_plugins(RepliconPlugins); app.add_client_event::(ChannelKind::Ordered) - .add_systems(Update, (send_events, receive_events.run_if(has_authority))); + .add_systems(Update, (send_events, receive_events.run_if(server_or_singleplayer))); /// Sends an event from client or listen server. fn send_events(mut dummy_events: EventWriter) { @@ -383,7 +383,7 @@ from the send list): # let mut app = App::new(); # app.add_plugins(RepliconPlugins); app.add_server_event::(ChannelKind::Ordered) - .add_systems(Update, (send_events.run_if(has_authority), receive_events)); + .add_systems(Update, (send_events.run_if(server_or_singleplayer), receive_events)); /// Sends an event from server or single-player. fn send_events(mut dummy_events: EventWriter>) { diff --git a/src/parent_sync.rs b/src/parent_sync.rs index 5ae2a64a..d78f1a12 100644 --- a/src/parent_sync.rs +++ b/src/parent_sync.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; #[cfg(feature = "client")] use crate::client::ClientSet; -use crate::core::{common_conditions::has_authority, replication_rules::AppRuleExt}; +use crate::core::{common_conditions::*, replication_rules::AppRuleExt}; #[cfg(feature = "server")] use crate::server::ServerSet; @@ -39,7 +39,7 @@ impl Plugin for ParentSyncPlugin { app.add_systems( PostUpdate, (Self::store_changes, Self::store_removals) - .run_if(has_authority) + .run_if(server_or_singleplayer) .in_set(ServerSet::StoreHierarchy), ); } diff --git a/src/server/events.rs b/src/server/events.rs index bd775ef1..d3423563 100644 --- a/src/server/events.rs +++ b/src/server/events.rs @@ -28,7 +28,7 @@ impl Plugin for ServerEventsPlugin { PostUpdate, ( Self::send.run_if(server_running), - Self::resend_locally.run_if(has_authority), + Self::resend_locally.run_if(server_or_singleplayer), ) .chain() .after(ServerPlugin::send_replication)