-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add fly and speed commands to proof-of-concept module
- Refactor command handling into dedicated `command` module - Implement `FlyCommand` to enable flying with default speed of ~11 m/s - Move existing `SpeedCommand` from lib.rs to dedicated file - Both commands use `PlayerAbilitiesS2c` packet to modify player capabilities - Register commands through new centralized `command::register` function
- Loading branch information
1 parent
c15552d
commit 55b1fe6
Showing
4 changed files
with
109 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use flecs_ecs::core::World; | ||
use hyperion_clap::hyperion_command::CommandRegistry; | ||
use hyperion_clap::MinecraftCommand; | ||
|
||
use crate::command::{fly::FlyCommand, speed::SpeedCommand}; | ||
|
||
mod fly; | ||
mod speed; | ||
|
||
pub fn register(registry: &mut CommandRegistry, world: &World) { | ||
SpeedCommand::register(registry, world); | ||
FlyCommand::register(registry, world); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use clap::Parser; | ||
use flecs_ecs::core::{Entity, EntityViewGet, World, WorldGet}; | ||
use hyperion::{ | ||
net::{Compose, DataBundle, NetworkStreamRef, agnostic}, | ||
system_registry::SystemId, | ||
valence_protocol::packets::play::{ | ||
PlayerAbilitiesS2c, player_abilities_s2c::PlayerAbilitiesFlags, | ||
}, | ||
}; | ||
use hyperion_clap::MinecraftCommand; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(name = "fly")] | ||
pub struct FlyCommand; | ||
|
||
impl MinecraftCommand for FlyCommand { | ||
fn execute(self, world: &World, caller: Entity) { | ||
let chat = agnostic::chat("§aFlying enabled"); | ||
|
||
world.get::<&Compose>(|compose| { | ||
caller | ||
.entity_view(world) | ||
.get::<&NetworkStreamRef>(|stream| { | ||
let packet = fly_packet(); | ||
|
||
let mut bundle = DataBundle::new(compose); | ||
bundle.add_packet(&packet, world).unwrap(); | ||
bundle.add_packet(&chat, world).unwrap(); | ||
|
||
bundle.send(world, *stream, SystemId(8)).unwrap(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
fn fly_packet() -> PlayerAbilitiesS2c { | ||
const SPEED_METER_PER_SECOND: f32 = 10.92; | ||
|
||
// guessing.. idk what the actual conversion is | ||
const SOME_CONVERSION: f32 = SPEED_METER_PER_SECOND / 70.0; | ||
|
||
PlayerAbilitiesS2c { | ||
flags: PlayerAbilitiesFlags::default() | ||
.with_flying(true) | ||
.with_allow_flying(true), | ||
flying_speed: SOME_CONVERSION, | ||
fov_modifier: 0.0, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use clap::Parser; | ||
use flecs_ecs::core::{Entity, EntityViewGet, World, WorldGet}; | ||
use hyperion::net::{agnostic, Compose, DataBundle, NetworkStreamRef}; | ||
use hyperion::system_registry::SystemId; | ||
use hyperion::valence_protocol::packets::play::player_abilities_s2c::PlayerAbilitiesFlags; | ||
use hyperion::valence_protocol::packets::play::PlayerAbilitiesS2c; | ||
use hyperion_clap::MinecraftCommand; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(name = "speed")] | ||
pub struct SpeedCommand { | ||
amount: f32, | ||
} | ||
|
||
|
||
impl MinecraftCommand for SpeedCommand { | ||
fn execute(self, world: &World, caller: Entity) { | ||
let msg = format!("Setting speed to {}", self.amount); | ||
let chat = agnostic::chat(msg); | ||
|
||
world.get::<&Compose>(|compose| { | ||
caller | ||
.entity_view(world) | ||
.get::<&NetworkStreamRef>(|stream| { | ||
let packet = speed_packet(self.amount); | ||
|
||
let mut bundle = DataBundle::new(compose); | ||
bundle.add_packet(&packet, world).unwrap(); | ||
bundle.add_packet(&chat, world).unwrap(); | ||
|
||
bundle.send(world, *stream, SystemId(8)).unwrap(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
fn speed_packet(amount: f32) -> PlayerAbilitiesS2c { | ||
PlayerAbilitiesS2c { | ||
flags: PlayerAbilitiesFlags::default() | ||
.with_flying(true) | ||
.with_allow_flying(true), | ||
flying_speed: amount, | ||
fov_modifier: 0.0, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters