playground/extras.rs
1//! Put stuff in here if you find that you have to write the same code for
2//! multiple playgrounds.
3
4use valence::prelude::*;
5
6/// Toggles client's game mode between survival and creative when they start
7/// sneaking.
8pub(crate) fn toggle_gamemode_on_sneak(
9 mut clients: Query<&mut GameMode>,
10 mut events: EventReader<SneakEvent>,
11) {
12 for event in events.read() {
13 if event.state == SneakState::Start {
14 if let Ok(mut mode) = clients.get_mut(event.client) {
15 *mode = match *mode {
16 GameMode::Survival => GameMode::Creative,
17 GameMode::Creative => GameMode::Survival,
18 _ => GameMode::Creative,
19 };
20 }
21 }
22 }
23}