Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented sneak tracking. #428

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion feather/common/src/entities/player.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use anyhow::bail;
use base::EntityKind;
use ecs::{EntityBuilder, SysResult};
use quill_common::{components::CreativeFlying, entities::Player};
use quill_common::{
components::{CreativeFlying, Sneaking},
entities::Player,
};

pub fn build_default(builder: &mut EntityBuilder) {
super::build_default(builder);
builder
.add(Player)
.add(CreativeFlying(false))
.add(Sneaking(false))
.add(EntityKind::Player);
}

Expand Down
6 changes: 5 additions & 1 deletion feather/server/src/packet_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use quill_common::components::Name;

use crate::{NetworkId, Server};

mod entity_action;
mod interaction;
pub mod inventory;
mod movement;
Expand Down Expand Up @@ -70,6 +71,10 @@ pub fn handle_packet(
movement::handle_player_abilities(game, player_id, packet)
}

ClientPlayPacket::EntityAction(packet) => {
entity_action::handle_entity_action(game, player_id, packet)
}

ClientPlayPacket::TeleportConfirm(_)
| ClientPlayPacket::QueryBlockNbt(_)
| ClientPlayPacket::SetDifficulty(_)
Expand All @@ -88,7 +93,6 @@ pub fn handle_packet(
| ClientPlayPacket::SteerBoat(_)
| ClientPlayPacket::PickItem(_)
| ClientPlayPacket::CraftRecipeRequest(_)
| ClientPlayPacket::EntityAction(_)
| ClientPlayPacket::SteerVehicle(_)
| ClientPlayPacket::SetDisplayedRecipe(_)
| ClientPlayPacket::SetRecipeBookState(_)
Expand Down
59 changes: 59 additions & 0 deletions feather/server/src/packet_handlers/entity_action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use common::Game;
use ecs::{Entity, SysResult};
use protocol::packets::client::{EntityAction, EntityActionKind};
use quill_common::{components::Sneaking, events::SneakEvent};

/// From [wiki](https://wiki.vg/Protocol#Entity_Action)
/// Sent by the client to indicate that it has performed certain actions:
/// *) sneaking (crouching),
/// *) sprinting,
/// *) exiting a bed,
/// *) jumping with a horse,
/// *) opening a horse's inventory while riding it.
///
pub fn handle_entity_action(game: &mut Game, player: Entity, packet: EntityAction) -> SysResult {
match packet.action_id {
EntityActionKind::StartSneaking => {
let is_sneaking = game.ecs.get_mut::<Sneaking>(player)?.0;
if !is_sneaking {
game.ecs
.insert_entity_event(player, SneakEvent::new(true))?;
game.ecs.get_mut::<Sneaking>(player)?.0 = true;
}
}
EntityActionKind::StopSneaking => {
let is_sneaking = game.ecs.get_mut::<Sneaking>(player)?.0;
if is_sneaking {
game.ecs
.insert_entity_event(player, SneakEvent::new(false))?;
game.ecs.get_mut::<Sneaking>(player)?.0 = false;
}
}
EntityActionKind::LeaveBed => {
//TODO issue #423
// Note that the leave bed packet is not sent if the server changes night to day
// 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::StartHorseJump => {
//TODO issue #423
}
EntityActionKind::StopJorseJump => {
//TODO issue #423
}
EntityActionKind::OpenHorseInventory => {
//TODO issue #423
}
EntityActionKind::StartElytraFlight => {
//TODO issue #423
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion feather/server/src/packet_handlers/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn update_client_position(server: &Server, player: EntityRef, pos: Position) ->
Ok(())
}

/// Handles the PlayerAbilities packet that signals, if the client wants to
/// Handles the PlayerAbilities packet that signals that the client wants to
/// start/stop flying (like in creative mode).
pub fn handle_player_abilities(
game: &mut Game,
Expand Down
4 changes: 4 additions & 0 deletions quill/common/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ host_component_enum! {
BlockInteractEvent = 1008,
CreativeFlying = 1009,
CreativeFlyingEvent = 1010,
Sneaking = 1011,
SneakEvent = 1012,


}
}
Expand Down Expand Up @@ -348,3 +351,4 @@ bincode_component_impl!(InteractEntityEvent);
bincode_component_impl!(BlockPlacementEvent);
bincode_component_impl!(BlockInteractEvent);
bincode_component_impl!(CreativeFlyingEvent);
bincode_component_impl!(SneakEvent);
7 changes: 6 additions & 1 deletion quill/common/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ impl Display for CustomName {
}
}

/// Whether an entity is flying (like in creative mode)
/// Whether an entity is flying (like in creative mode, so it does not reflect if the player is flying by other means)
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CreativeFlying(pub bool);

bincode_component_impl!(CreativeFlying);

/// Wheather an entity is sneaking, like in pressing shift.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Sneaking(pub bool);
bincode_component_impl!(Sneaking);
2 changes: 1 addition & 1 deletion quill/common/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ mod change;
mod interact_entity;

pub use block_interact::{BlockInteractEvent, BlockPlacementEvent};
pub use change::CreativeFlyingEvent;
pub use change::{CreativeFlyingEvent, SneakEvent};
pub use interact_entity::InteractEntityEvent;
18 changes: 18 additions & 0 deletions quill/common/src/events/change.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/*
All events in this file are triggerd when there is a change in a certain value.
*/

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreativeFlyingEvent {
pub is_flying: bool,
Expand All @@ -11,3 +16,16 @@ impl CreativeFlyingEvent {
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SneakEvent {
pub is_sneaking: bool,
}

impl SneakEvent {
pub fn new(changed_to: bool) -> Self {
Self {
is_sneaking: changed_to,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ This plugin observers the CreativeFlightEvent printing a msg when someone starts
flying.
*/

use quill::{events::CreativeFlyingEvent, Game, Plugin, Setup};
use quill::{
events::{CreativeFlyingEvent, SneakEvent},
Game, Plugin, Setup,
};

quill::plugin!(FlightPlugin);

Expand All @@ -12,6 +15,7 @@ struct FlightPlugin {}
impl Plugin for FlightPlugin {
fn enable(_game: &mut Game, setup: &mut Setup<Self>) -> Self {
setup.add_system(flight_observer_system);
setup.add_system(sneak_observer_system);
FlightPlugin {}
}

Expand All @@ -27,3 +31,13 @@ fn flight_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) {
}
}
}

fn sneak_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) {
for (player, change) in game.query::<&SneakEvent>() {
if change.is_sneaking {
player.send_message("Enjoy sneaking!");
} else {
player.send_message("How was it to be sneaking?");
}
}
}