Skip to main content

yog_api/
net.rs

1//! Sending over the network. To a player (server side) always; to the
2//! server (client side) only when the mod says so in `yog.toml`:
3//!
4//! ```toml
5//! [network]
6//! to_server = true
7//! ```
8//!
9//! Then it needs Yog on the server, and `yog build` builds it with this
10//! crate's `to-server` feature. A mod that sends without saying so doesn't
11//! compile: the channel it names isn't a [`ServerChannel`].
12//!
13//! ```compile_fail
14//! // Without `to-server`: "this mod sends to the server without saying so".
15//! yog_api::net::send_to_server("mymod:hello", vec![1u8]);
16//! ```
17
18pub use yog_event::net::{SendToPlayer, send_to_player};
19
20/// A channel the mod sends to the server on: any name, once the mod says
21/// it sends there (`[network] to_server = true`, the `to-server` feature).
22#[diagnostic::on_unimplemented(
23    message = "this mod sends to the server without saying so",
24    label = "sent to the server here",
25    note = "say it in yog.toml — `[network]` `to_server = true`: the mod then needs Yog on the server too"
26)]
27pub trait ServerChannel: yog_event::Arg<String> {}
28
29#[cfg(feature = "to-server")]
30impl ServerChannel for &str {}
31#[cfg(feature = "to-server")]
32impl ServerChannel for String {}
33
34/// Sends `payload` on `channel` to the server (client side): whether it
35/// went (a server without Yog has no channel to take it).
36pub fn send_to_server(channel: impl ServerChannel, payload: impl yog_event::Arg<Vec<u8>>) -> bool {
37    yog_event::net::send_to_server(channel, payload)
38}