Skip to content

Commit

Permalink
Rename has_authority condition into server_or_singleplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Sep 13, 2024
1 parent 1cd4a0a commit d07de1f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Expand Down
2 changes: 1 addition & 1 deletion src/client/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions src/core/common_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ pub fn server_running(server: Option<Res<RepliconServer>>) -> 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<Res<RepliconClient>>) -> 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<Res<RepliconClient>>) -> bool {
let Some(client) = client else {
return true;
};
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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):
```
Expand All @@ -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::<DummyEvent>(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<DummyEvent>) {
Expand Down Expand Up @@ -383,7 +383,7 @@ from the send list):
# let mut app = App::new();
# app.add_plugins(RepliconPlugins);
app.add_server_event::<DummyEvent>(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<ToClients<DummyEvent>>) {
Expand Down
4 changes: 2 additions & 2 deletions src/parent_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d07de1f

Please sign in to comment.