Skip to content

Commit

Permalink
feat: Add fly and speed commands to proof-of-concept module
Browse files Browse the repository at this point in the history
- 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
andrewgazelka committed Nov 9, 2024
1 parent c15552d commit 55b1fe6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 37 deletions.
13 changes: 13 additions & 0 deletions events/proof-of-concept/src/command.rs
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);
}
49 changes: 49 additions & 0 deletions events/proof-of-concept/src/command/fly.rs
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,
}
}
45 changes: 45 additions & 0 deletions events/proof-of-concept/src/command/speed.rs
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,
}
}
39 changes: 2 additions & 37 deletions events/proof-of-concept/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,7 @@ use crate::module::{chat::ChatModule, spawn::SpawnModule, stats::StatsModule};
#[derive(Component)]
pub struct ProofOfConceptModule;

#[derive(Parser, Debug)]
#[command(name = "speed")]
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 = fly_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 fly_speed_packet(amount: f32) -> PlayerAbilitiesS2c {
PlayerAbilitiesS2c {
flags: PlayerAbilitiesFlags::default()
.with_allow_flying(true)
.with_flying(true),
flying_speed: amount,
fov_modifier: 0.0,
}
}
mod command;

impl Module for ProofOfConceptModule {
fn module(world: &World) {
Expand All @@ -90,7 +55,7 @@ impl Module for ProofOfConceptModule {
world.import::<hyperion_clap::ClapCommandModule>();

world.get::<&mut CommandRegistry>(|registry| {
SpeedCommand::register(registry, world);
command::register(registry, world);
});

world.set(hyperion_utils::AppId {
Expand Down

0 comments on commit 55b1fe6

Please sign in to comment.