steel_protocol/packets/game/c_level_event.rs
1use steel_macros::{ClientPacket, WriteTo};
2use steel_registry::packets::play::C_LEVEL_EVENT;
3use steel_utils::BlockPos;
4
5/// Sent to trigger level events (sounds, particles, animations) on the client.
6///
7/// Level events are predefined effects identified by an event type constant.
8/// The `data` field provides event-specific parameters (e.g., block state ID for
9/// block destruction particles).
10///
11/// See `steel_registry::level_events` for all available event type constants.
12#[derive(WriteTo, ClientPacket, Clone, Debug)]
13#[packet_id(Play = C_LEVEL_EVENT)]
14pub struct CLevelEvent {
15 /// The event type ID. Use constants from `steel_registry::level_events`.
16 pub event_type: i32,
17 /// The position where the event occurs.
18 pub pos: BlockPos,
19 /// Event-specific data (e.g., block state ID for `PARTICLES_DESTROY_BLOCK`).
20 pub data: i32,
21 /// If true, the event is sent to all players regardless of distance.
22 /// If false, only players within 64 blocks receive it.
23 pub global_event: bool,
24}
25
26impl CLevelEvent {
27 /// Creates a new level event packet.
28 #[must_use]
29 pub const fn new(event_type: i32, pos: BlockPos, data: i32, global_event: bool) -> Self {
30 Self {
31 event_type,
32 pos,
33 data,
34 global_event,
35 }
36 }
37
38 /// Creates a block destruction event with particles and sound.
39 ///
40 /// # Arguments
41 /// * `pos` - The position of the destroyed block
42 /// * `block_state_id` - The block state ID of the destroyed block
43 #[must_use]
44 pub const fn destroy_block(pos: BlockPos, block_state_id: u32) -> Self {
45 Self::new(
46 steel_registry::level_events::PARTICLES_DESTROY_BLOCK,
47 pos,
48 block_state_id as i32,
49 false,
50 )
51 }
52}