|
1 | 1 | 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 | +}; |
3 | 6 | use hyperion::{
|
4 | 7 | net::{Compose, DataBundle, NetworkStreamRef, agnostic},
|
| 8 | + simulation::Player, |
5 | 9 | system_registry::SystemId,
|
6 | 10 | valence_protocol::packets::play::{
|
7 | 11 | PlayerAbilitiesS2c, player_abilities_s2c::PlayerAbilitiesFlags,
|
8 | 12 | },
|
9 | 13 | };
|
10 | 14 | use hyperion_clap::{CommandPermission, MinecraftCommand};
|
11 | 15 |
|
| 16 | +#[derive(Component)] |
| 17 | +#[meta] |
| 18 | +pub struct Flight { |
| 19 | + pub allow: bool, |
| 20 | +} |
| 21 | + |
12 | 22 | #[derive(Parser, CommandPermission, Debug)]
|
13 | 23 | #[command(name = "fly")]
|
14 | 24 | #[command_permission(group = "Moderator")]
|
15 | 25 | pub struct FlyCommand;
|
16 | 26 |
|
17 | 27 | impl MinecraftCommand for FlyCommand {
|
18 | 28 | fn execute(self, world: &World, caller: Entity) {
|
19 |
| - let chat = agnostic::chat("§aFlying enabled"); |
20 |
| - |
21 | 29 | world.get::<&Compose>(|compose| {
|
22 | 30 | caller
|
23 | 31 | .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); |
26 | 44 |
|
27 | 45 | let mut bundle = DataBundle::new(compose);
|
28 | 46 | bundle.add_packet(&packet, world).unwrap();
|
29 |
| - bundle.add_packet(&chat, world).unwrap(); |
| 47 | + bundle.add_packet(&chat_packet, world).unwrap(); |
30 | 48 |
|
31 | 49 | bundle.send(world, *stream, SystemId(8)).unwrap();
|
32 | 50 | });
|
33 | 51 | });
|
34 | 52 | }
|
| 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 | + } |
35 | 64 | }
|
36 | 65 |
|
37 |
| -fn fly_packet() -> PlayerAbilitiesS2c { |
| 66 | +fn fly_packet(allow_flight: bool) -> PlayerAbilitiesS2c { |
38 | 67 | const SPEED_METER_PER_SECOND: f32 = 10.92;
|
39 | 68 |
|
40 | 69 | // guessing.. idk what the actual conversion is
|
41 | 70 | const SOME_CONVERSION: f32 = SPEED_METER_PER_SECOND / 70.0;
|
42 | 71 |
|
43 | 72 | PlayerAbilitiesS2c {
|
44 | 73 | flags: PlayerAbilitiesFlags::default()
|
45 |
| - .with_flying(true) |
46 |
| - .with_allow_flying(true), |
| 74 | + .with_flying(allow_flight) |
| 75 | + .with_allow_flying(allow_flight), |
47 | 76 | flying_speed: SOME_CONVERSION,
|
48 | 77 | fov_modifier: 0.0,
|
49 | 78 | }
|
|
0 commit comments