From e755ac1d3c2bf612bc2cb3f10b64c43e7d2311dd Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:00:51 -0500 Subject: [PATCH 01/11] Added sprinting component to player --- feather/common/src/entities/player.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From a8d988fb623f6cea24053a3ace24a490bdb6df0e Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:02:00 -0500 Subject: [PATCH 02/11] Added sprint tracking --- .../src/packet_handlers/entity_action.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/feather/server/src/packet_handlers/entity_action.rs b/feather/server/src/packet_handlers/entity_action.rs index 281906da4..0d0520e15 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: @@ -36,10 +39,20 @@ pub fn handle_entity_action(game: &mut Game, player: Entity, packet: EntityActio // a notice that bed state might have changed. } EntityActionKind::StartSprinting => { - //TODO issue #423 + let is_sprinting = game.ecs.get_mut::(player)?.0; + if !is_sprinting { + game.ecs + .insert_entity_event(player, SprintEvent::new(true))?; + game.ecs.get_mut::(player)?.0 = true; + } } EntityActionKind::StopSprinting => { - //TODO issue #423 + let is_sprinting = game.ecs.get_mut::(player)?.0; + if is_sprinting { + game.ecs + .insert_entity_event(player, SprintEvent::new(false))?; + game.ecs.get_mut::(player)?.0 = false; + } } EntityActionKind::StartHorseJump => { //TODO issue #423 From 4bd184ce7bd9815c4ee521156c6d6516521338f8 Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:02:29 -0500 Subject: [PATCH 03/11] Added sprint component and event --- quill/common/src/component.rs | 3 +++ 1 file changed, 3 insertions(+) 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); From 5757f27f6d5340af45e667ef13b2060e892ab58b Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:02:54 -0500 Subject: [PATCH 04/11] Added sprint component --- quill/common/src/components.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 3d6cec3d8..5d99d4257 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); + +#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +/// A component on players that tracks if they are sprinting or not. +pub struct Sprinting(pub bool); +impl Sprinting { + pub fn new(value: bool) -> Self { + Sprinting(value) + } +} +bincode_component_impl!(Sprinting); From 5f4b7550b9ef76d65ebbc22d0c9a4090e240890c Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:03:36 -0500 Subject: [PATCH 05/11] Added sprint event import --- quill/common/src/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 568e442445ceae3a863926ad22606a1d6869db5e Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:03:53 -0500 Subject: [PATCH 06/11] Added sprint event --- quill/common/src/events/change.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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, + } + } +} From f828a2b37c0cbc261d4c2e23170c38d93d7c90cb Mon Sep 17 00:00:00 2001 From: linuxNeko Date: Mon, 7 Jun 2021 17:04:46 -0500 Subject: [PATCH 07/11] Sprint tracking in plugin example --- .../observe-creativemode-flight-event/src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) 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?"); + } + } +} From d17f360725bdbcbd94dc3bd97115b4b78b984de7 Mon Sep 17 00:00:00 2001 From: ThisNekoGuy Date: Tue, 8 Jun 2021 21:14:33 +0000 Subject: [PATCH 08/11] Reloacted a comment to a more appropriate postion --- quill/common/src/components.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 5d99d4257..f7ce6f137 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -110,8 +110,8 @@ bincode_component_impl!(CreativeFlying); pub struct Sneaking(pub bool); bincode_component_impl!(Sneaking); -#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] /// 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 { From 6e1359784a2488be1fce94e3c23756108d8f966c Mon Sep 17 00:00:00 2001 From: ThisNekoGuy Date: Tue, 8 Jun 2021 22:14:05 +0000 Subject: [PATCH 09/11] Refactored Sprinting Detection Refactored the sprint detection to reduce code duplication with the assistance of Defman --- .../server/src/packet_handlers/entity_action.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/feather/server/src/packet_handlers/entity_action.rs b/feather/server/src/packet_handlers/entity_action.rs index 0d0520e15..9d67131a2 100644 --- a/feather/server/src/packet_handlers/entity_action.rs +++ b/feather/server/src/packet_handlers/entity_action.rs @@ -38,20 +38,13 @@ 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 => { + EntityActionKind::StartSprinting | EntityActionKind::StopSprinting => { + let start_sprinting = matches!(EntityActionKind::StartSprinting, packet.action_id); let is_sprinting = game.ecs.get_mut::(player)?.0; - if !is_sprinting { + if is_sprinting != start_sprinting { game.ecs - .insert_entity_event(player, SprintEvent::new(true))?; - game.ecs.get_mut::(player)?.0 = true; - } - } - EntityActionKind::StopSprinting => { - let is_sprinting = game.ecs.get_mut::(player)?.0; - if is_sprinting { - game.ecs - .insert_entity_event(player, SprintEvent::new(false))?; - game.ecs.get_mut::(player)?.0 = false; + .insert_entity_event(player, SprintEvent::new(start_sprinting))?; + game.ecs.get_mut::(player)?.0 = start_sprinting; } } EntityActionKind::StartHorseJump => { From a00ecb6e51654dd6319e54edcf1cf2189ee1de83 Mon Sep 17 00:00:00 2001 From: ThisNekoGuy Date: Wed, 9 Jun 2021 02:48:28 +0000 Subject: [PATCH 10/11] Refactor Reverted Due to Problem The desired refactor didn't work because the method attempted couldn't receive the sprint state from the player --- .../server/src/packet_handlers/entity_action.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/feather/server/src/packet_handlers/entity_action.rs b/feather/server/src/packet_handlers/entity_action.rs index 9d67131a2..0d0520e15 100644 --- a/feather/server/src/packet_handlers/entity_action.rs +++ b/feather/server/src/packet_handlers/entity_action.rs @@ -38,13 +38,20 @@ 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 | EntityActionKind::StopSprinting => { - let start_sprinting = matches!(EntityActionKind::StartSprinting, packet.action_id); + EntityActionKind::StartSprinting => { let is_sprinting = game.ecs.get_mut::(player)?.0; - if is_sprinting != start_sprinting { + if !is_sprinting { game.ecs - .insert_entity_event(player, SprintEvent::new(start_sprinting))?; - game.ecs.get_mut::(player)?.0 = start_sprinting; + .insert_entity_event(player, SprintEvent::new(true))?; + game.ecs.get_mut::(player)?.0 = true; + } + } + EntityActionKind::StopSprinting => { + let is_sprinting = game.ecs.get_mut::(player)?.0; + if is_sprinting { + game.ecs + .insert_entity_event(player, SprintEvent::new(false))?; + game.ecs.get_mut::(player)?.0 = false; } } EntityActionKind::StartHorseJump => { From f9cc27eda80edaecc104d0da72620df2d955b9b9 Mon Sep 17 00:00:00 2001 From: ThisNekoGuy Date: Thu, 10 Jun 2021 19:48:26 +0000 Subject: [PATCH 11/11] Re-Refactor of Sprint Detection Turned out the macro actually expected the packet id and the `EntityActionKind` to be reversed --- .../server/src/packet_handlers/entity_action.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/feather/server/src/packet_handlers/entity_action.rs b/feather/server/src/packet_handlers/entity_action.rs index 0d0520e15..8de3210c2 100644 --- a/feather/server/src/packet_handlers/entity_action.rs +++ b/feather/server/src/packet_handlers/entity_action.rs @@ -38,20 +38,13 @@ 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 => { + 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 { + if is_sprinting != start_sprinting { game.ecs - .insert_entity_event(player, SprintEvent::new(true))?; - game.ecs.get_mut::(player)?.0 = true; - } - } - EntityActionKind::StopSprinting => { - let is_sprinting = game.ecs.get_mut::(player)?.0; - if is_sprinting { - game.ecs - .insert_entity_event(player, SprintEvent::new(false))?; - game.ecs.get_mut::(player)?.0 = false; + .insert_entity_event(player, SprintEvent::new(start_sprinting))?; + game.ecs.get_mut::(player)?.0 = start_sprinting; } } EntityActionKind::StartHorseJump => {