mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-27 05:56:33 +11:00
c5557e744d
## Description - Move all packets out of `valence_core` and into the places where they're actually used. This has a few benefits: - Avoids compiling code for packets that go unused when feature flags are disabled. - Code is distributed more uniformly across crates, improving compilation times. - Improves local reasoning when everything relevant to a module is defined in the same place. - Easier to share code between the packet consumer and the packet. - Tweak `Packet` macro syntax. - Update `syn` to 2.0. - Reorganize some code in `valence_client` (needs further work). - Impl `WritePacket` for `Instance`. - Remove packet enums such as `S2cPlayPacket` and `C2sPlayPacket`. - Replace `assert_packet_count` and `assert_packet_order` macros with non-macro methods. To prevent this PR from getting out of hand, I've disabled the packet inspector and stresser until they have been rewritten to account for these changes.
58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use valence_core::protocol::raw::RawBytes;
|
|
use valence_core::protocol::{packet_id, Decode, Encode};
|
|
|
|
use super::*;
|
|
use crate::event_loop::{EventLoopSchedule, EventLoopSet, PacketEvent};
|
|
|
|
pub(super) fn build(app: &mut App) {
|
|
app.add_event::<CustomPayloadEvent>().add_system(
|
|
handle_custom_payload
|
|
.in_schedule(EventLoopSchedule)
|
|
.in_base_set(EventLoopSet::PreUpdate),
|
|
);
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct CustomPayloadEvent {
|
|
pub client: Entity,
|
|
pub channel: Ident<String>,
|
|
pub data: Box<[u8]>,
|
|
}
|
|
|
|
impl Client {
|
|
pub fn send_custom_payload(&mut self, channel: Ident<&str>, data: &[u8]) {
|
|
self.write_packet(&CustomPayloadS2c {
|
|
channel: channel.into(),
|
|
data: data.into(),
|
|
});
|
|
}
|
|
}
|
|
|
|
fn handle_custom_payload(
|
|
mut packets: EventReader<PacketEvent>,
|
|
mut events: EventWriter<CustomPayloadEvent>,
|
|
) {
|
|
for packet in packets.iter() {
|
|
if let Some(pkt) = packet.decode::<CustomPayloadC2s>() {
|
|
events.send(CustomPayloadEvent {
|
|
client: packet.client,
|
|
channel: pkt.channel.into(),
|
|
data: pkt.data.0.into(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Encode, Decode, Packet)]
|
|
#[packet(id = packet_id::CUSTOM_PAYLOAD_C2S)]
|
|
pub struct CustomPayloadC2s<'a> {
|
|
pub channel: Ident<Cow<'a, str>>,
|
|
pub data: RawBytes<'a>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Encode, Decode, Packet)]
|
|
#[packet(id = packet_id::CUSTOM_PAYLOAD_S2C)]
|
|
pub struct CustomPayloadS2c<'a> {
|
|
pub channel: Ident<Cow<'a, str>>,
|
|
pub data: RawBytes<'a>,
|
|
}
|