diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 4f62d4a48..42cb12285 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -2,7 +2,7 @@ use anyhow::bail; use base::EntityKind; use ecs::{EntityBuilder, SysResult}; use quill_common::{ - components::{CreativeFlying, Sneaking}, + components::{CreativeFlying, Sneaking, Sprinting}, entities::Player, }; @@ -12,6 +12,7 @@ pub fn build_default(builder: &mut EntityBuilder) { .add(Player) .add(CreativeFlying(false)) .add(Sneaking(false)) + .add(Sprinting(false)) .add(EntityKind::Player); } diff --git a/feather/server/src/packet_handlers/entity_action.rs b/feather/server/src/packet_handlers/entity_action.rs index 281906da4..8de3210c2 100644 --- a/feather/server/src/packet_handlers/entity_action.rs +++ b/feather/server/src/packet_handlers/entity_action.rs @@ -1,7 +1,10 @@ use common::Game; use ecs::{Entity, SysResult}; use protocol::packets::client::{EntityAction, EntityActionKind}; -use quill_common::{components::Sneaking, events::SneakEvent}; +use quill_common::{ + components::{Sneaking, Sprinting}, + events::{SneakEvent, SprintEvent}, +}; /// From [wiki](https://wiki.vg/Protocol#Entity_Action) /// Sent by the client to indicate that it has performed certain actions: @@ -35,11 +38,14 @@ pub fn handle_entity_action(game: &mut Game, player: Entity, packet: EntityActio // and all players are kicked out of the bed. We have to seperatly send out // a notice that bed state might have changed. } - EntityActionKind::StartSprinting => { - //TODO issue #423 - } - EntityActionKind::StopSprinting => { - //TODO issue #423 + EntityActionKind::StartSprinting | EntityActionKind::StopSprinting => { + let start_sprinting = matches!(packet.action_id, EntityActionKind::StartSprinting); + let is_sprinting = game.ecs.get_mut::(player)?.0; + if is_sprinting != start_sprinting { + game.ecs + .insert_entity_event(player, SprintEvent::new(start_sprinting))?; + game.ecs.get_mut::(player)?.0 = start_sprinting; + } } EntityActionKind::StartHorseJump => { //TODO issue #423 diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index 9d80223ea..4db753ce3 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -188,6 +188,8 @@ host_component_enum! { CreativeFlyingEvent = 1010, Sneaking = 1011, SneakEvent = 1012, + Sprinting = 1013, + SprintEvent = 1014, } @@ -352,3 +354,4 @@ bincode_component_impl!(BlockPlacementEvent); bincode_component_impl!(BlockInteractEvent); bincode_component_impl!(CreativeFlyingEvent); bincode_component_impl!(SneakEvent); +bincode_component_impl!(SprintEvent); diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 3d6cec3d8..f7ce6f137 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -109,3 +109,13 @@ bincode_component_impl!(CreativeFlying); #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Sneaking(pub bool); bincode_component_impl!(Sneaking); + +/// A component on players that tracks if they are sprinting or not. +#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Sprinting(pub bool); +impl Sprinting { + pub fn new(value: bool) -> Self { + Sprinting(value) + } +} +bincode_component_impl!(Sprinting); diff --git a/quill/common/src/events.rs b/quill/common/src/events.rs index 4517ef7d6..734d572cb 100644 --- a/quill/common/src/events.rs +++ b/quill/common/src/events.rs @@ -3,5 +3,5 @@ mod change; mod interact_entity; pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; -pub use change::{CreativeFlyingEvent, SneakEvent}; +pub use change::{CreativeFlyingEvent, SneakEvent, SprintEvent}; pub use interact_entity::InteractEntityEvent; diff --git a/quill/common/src/events/change.rs b/quill/common/src/events/change.rs index ca4f1ad81..f6bca77ac 100644 --- a/quill/common/src/events/change.rs +++ b/quill/common/src/events/change.rs @@ -29,3 +29,16 @@ impl SneakEvent { } } } + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct SprintEvent { + pub is_sprinting: bool, +} + +impl SprintEvent { + pub fn new(changed_to: bool) -> Self { + Self { + is_sprinting: changed_to, + } + } +} diff --git a/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs index 151eac1a5..98d28c272 100644 --- a/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs +++ b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs @@ -4,6 +4,7 @@ flying. */ use quill::{ + components::Sprinting, events::{CreativeFlyingEvent, SneakEvent}, Game, Plugin, Setup, }; @@ -16,6 +17,7 @@ impl Plugin for FlightPlugin { fn enable(_game: &mut Game, setup: &mut Setup) -> Self { setup.add_system(flight_observer_system); setup.add_system(sneak_observer_system); + setup.add_system(sprinting_observer_system); FlightPlugin {} } @@ -41,3 +43,11 @@ fn sneak_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) { } } } + +fn sprinting_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) { + for (player, sprinting) in game.query::<&Sprinting>() { + if sprinting.0 { + player.send_message("Are you sprinting?"); + } + } +}