Skip to content

Commit ce69550

Browse files
fix(commands): /fly properly toggles (#610)
Update the fly command to toggle flying state instead of only enabling it. Add proper component initialization for the Flight component: - Track flight state in Flight component - Register Flight component with meta for UI visibility - Automatically add Flight component to new Players - Update packet flags based on current flight state - Fixes #607 --------- Co-authored-by: Andrew Gazelka <andrew.gazelka@gmail.com>
1 parent 06221d2 commit ce69550

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

crates/hyperion-clap/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ use valence_protocol::{
2626
pub trait MinecraftCommand: Parser + CommandPermission {
2727
fn execute(self, world: &World, caller: Entity);
2828

29+
fn pre_register(_world: &World) {}
30+
2931
fn register(registry: &mut CommandRegistry, world: &World) {
32+
Self::pre_register(world);
33+
3034
let cmd = Self::command();
3135
let name = cmd.get_name();
3236

events/tag/src/command/fly.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,78 @@
11
use clap::Parser;
2-
use flecs_ecs::core::{Entity, EntityViewGet, World, WorldGet};
2+
use flecs_ecs::{
3+
core::{Entity, EntityViewGet, World, WorldGet, flecs},
4+
macros::Component,
5+
};
36
use hyperion::{
47
net::{Compose, DataBundle, NetworkStreamRef, agnostic},
8+
simulation::Player,
59
system_registry::SystemId,
610
valence_protocol::packets::play::{
711
PlayerAbilitiesS2c, player_abilities_s2c::PlayerAbilitiesFlags,
812
},
913
};
1014
use hyperion_clap::{CommandPermission, MinecraftCommand};
1115

16+
#[derive(Component)]
17+
#[meta]
18+
pub struct Flight {
19+
pub allow: bool,
20+
}
21+
1222
#[derive(Parser, CommandPermission, Debug)]
1323
#[command(name = "fly")]
1424
#[command_permission(group = "Moderator")]
1525
pub struct FlyCommand;
1626

1727
impl MinecraftCommand for FlyCommand {
1828
fn execute(self, world: &World, caller: Entity) {
19-
let chat = agnostic::chat("§aFlying enabled");
20-
2129
world.get::<&Compose>(|compose| {
2230
caller
2331
.entity_view(world)
24-
.get::<&NetworkStreamRef>(|stream| {
25-
let packet = fly_packet();
32+
.get::<(&mut Flight, &NetworkStreamRef)>(|(flight, stream)| {
33+
flight.allow = !flight.allow;
34+
35+
let allow_flight = flight.allow;
36+
37+
let chat_packet = if allow_flight {
38+
agnostic::chat("§aFlying enabled")
39+
} else {
40+
agnostic::chat("§cFlying disabled")
41+
};
42+
43+
let packet = fly_packet(allow_flight);
2644

2745
let mut bundle = DataBundle::new(compose);
2846
bundle.add_packet(&packet, world).unwrap();
29-
bundle.add_packet(&chat, world).unwrap();
47+
bundle.add_packet(&chat_packet, world).unwrap();
3048

3149
bundle.send(world, *stream, SystemId(8)).unwrap();
3250
});
3351
});
3452
}
53+
54+
fn pre_register(world: &World) {
55+
// register the component with meta meaning we can view the value in the flecs
56+
// explorer UI
57+
world.component::<Flight>().meta();
58+
59+
// whenever a Player component is added, we add the Flight component to them.
60+
world
61+
.component::<Player>()
62+
.add_trait::<(flecs::With, Flight)>();
63+
}
3564
}
3665

37-
fn fly_packet() -> PlayerAbilitiesS2c {
66+
fn fly_packet(allow_flight: bool) -> PlayerAbilitiesS2c {
3867
const SPEED_METER_PER_SECOND: f32 = 10.92;
3968

4069
// guessing.. idk what the actual conversion is
4170
const SOME_CONVERSION: f32 = SPEED_METER_PER_SECOND / 70.0;
4271

4372
PlayerAbilitiesS2c {
4473
flags: PlayerAbilitiesFlags::default()
45-
.with_flying(true)
46-
.with_allow_flying(true),
74+
.with_flying(allow_flight)
75+
.with_allow_flying(allow_flight),
4776
flying_speed: SOME_CONVERSION,
4877
fov_modifier: 0.0,
4978
}

0 commit comments

Comments
 (0)