valence/crates/valence_client/src/interact_item.rs
Ryan Johnson c5557e744d
Move packets out of valence_core. (#335)
## 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.
2023-05-29 01:36:11 -07:00

50 lines
1.4 KiB
Rust

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_core::hand::Hand;
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{packet_id, Decode, Encode, Packet};
use crate::action::ActionSequence;
use crate::event_loop::{EventLoopSchedule, EventLoopSet, PacketEvent};
pub(super) fn build(app: &mut App) {
app.add_event::<InteractItemEvent>().add_system(
handle_player_interact_item
.in_schedule(EventLoopSchedule)
.in_base_set(EventLoopSet::PreUpdate),
);
}
#[derive(Copy, Clone, Debug)]
pub struct InteractItemEvent {
pub client: Entity,
pub hand: Hand,
pub sequence: i32,
}
fn handle_player_interact_item(
mut packets: EventReader<PacketEvent>,
mut clients: Query<&mut ActionSequence>,
mut events: EventWriter<InteractItemEvent>,
) {
for packet in packets.iter() {
if let Some(pkt) = packet.decode::<PlayerInteractItemC2s>() {
if let Ok(mut action_seq) = clients.get_mut(packet.client) {
action_seq.update(pkt.sequence.0);
}
events.send(InteractItemEvent {
client: packet.client,
hand: pkt.hand,
sequence: pkt.sequence.0,
});
}
}
}
#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::PLAYER_INTERACT_ITEM_C2S)]
pub struct PlayerInteractItemC2s {
pub hand: Hand,
pub sequence: VarInt,
}