diff --git a/Cargo.toml b/Cargo.toml index 1b7943220..3253e06c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ members = [ "quill/example-plugins/plugin-message", "quill/example-plugins/query-entities", "quill/example-plugins/simple", + "quill/example-plugins/observe-creativemode-flight-event", # Feather (common and server) "feather/utils", diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 6e8d5c878..49ef576c8 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -1,11 +1,14 @@ use anyhow::bail; use base::EntityKind; use ecs::{EntityBuilder, SysResult}; -use quill_common::entities::Player; +use quill_common::{components::CreativeFlying, entities::Player}; pub fn build_default(builder: &mut EntityBuilder) { super::build_default(builder); - builder.add(Player).add(EntityKind::Player); + builder + .add(Player) + .add(CreativeFlying(false)) + .add(EntityKind::Player); } /// The hotbar slot a player's cursor is currently on diff --git a/feather/server/src/packet_handlers.rs b/feather/server/src/packet_handlers.rs index 5b9288681..39cfd184d 100644 --- a/feather/server/src/packet_handlers.rs +++ b/feather/server/src/packet_handlers.rs @@ -66,6 +66,10 @@ pub fn handle_packet( ClientPlayPacket::ClientSettings(packet) => handle_client_settings(server, player, packet), + ClientPlayPacket::PlayerAbilities(packet) => { + movement::handle_player_abilities(game, player_id, packet) + } + ClientPlayPacket::TeleportConfirm(_) | ClientPlayPacket::QueryBlockNbt(_) | ClientPlayPacket::SetDifficulty(_) @@ -84,7 +88,6 @@ pub fn handle_packet( | ClientPlayPacket::SteerBoat(_) | ClientPlayPacket::PickItem(_) | ClientPlayPacket::CraftRecipeRequest(_) - | ClientPlayPacket::PlayerAbilities(_) | ClientPlayPacket::EntityAction(_) | ClientPlayPacket::SteerVehicle(_) | ClientPlayPacket::SetDisplayedRecipe(_) diff --git a/feather/server/src/packet_handlers/movement.rs b/feather/server/src/packet_handlers/movement.rs index 5d31ad8d2..ed812999e 100644 --- a/feather/server/src/packet_handlers/movement.rs +++ b/feather/server/src/packet_handlers/movement.rs @@ -1,9 +1,13 @@ use base::Position; -use ecs::{EntityRef, SysResult}; +use common::Game; +use ecs::{Entity, EntityRef, SysResult}; use protocol::packets::client::{ - PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation, + PlayerAbilities, PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation, +}; +use quill_common::{ + components::{CreativeFlying, OnGround}, + events::CreativeFlyingEvent, }; -use quill_common::components::OnGround; use crate::{ClientId, Server}; @@ -89,3 +93,46 @@ fn update_client_position(server: &Server, player: EntityRef, pos: Position) -> } Ok(()) } + +/// Handles the PlayerAbilities packet that signals, if the client wants to +/// start/stop flying (like in creative mode). +pub fn handle_player_abilities( + game: &mut Game, + player: Entity, + packet: PlayerAbilities, +) -> SysResult { + let flying = game.ecs.get_mut::(player)?.0; + + match packet.flags { + 0 => { + // Flying stopped + if flying { + // Then it used to fly, therefor we need to trigger a event + // The vanilla client is actually quite good at keeping track of sending + // this packet only when there is a change, so this if should basically + // always trigger. + game.ecs + .insert_entity_event(player, CreativeFlyingEvent::new(false))?; + + game.ecs.get_mut::(player)?.0 = false; + } + } + 2 => { + // Flying started + if !flying { + // Then it used to not fly, therefor we need to trigger a event. + // The vanilla client is actually quite good at keeping track of sending + // this packet only when there is a change, so this if should basically + // always trigger. + game.ecs + .insert_entity_event(player, CreativeFlyingEvent::new(true))?; + game.ecs.get_mut::(player)?.0 = true; + } + } + err => { + log::error!("Got a unexpected flag in the PlayerAbilities packet. The value was: {} and not 0 or 2.", err) + } + } + + Ok(()) +} diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index 12be60756..25342584d 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -183,7 +183,10 @@ host_component_enum! { Particle = 1005, InteractEntityEvent = 1006, BlockPlacementEvent = 1007, - BlockInteractEvent = 1008 + BlockInteractEvent = 1008, + CreativeFlying = 1009, + CreativeFlyingEvent = 1010, + } } @@ -303,6 +306,14 @@ macro_rules! pod_component_impl { pod_component_impl!(Position); +/** +If you are using this macro and you get the error: +``` + error[E0599]: no variant or associated item named `...` found for enum `HostComponent` in the current scope. +``` +Then you need to go to the top of the file were this macro is defined. There you find the HostCompoent enum, that +you need to add your component to. +*/ macro_rules! bincode_component_impl { ($type:ident) => { unsafe impl crate::Component for $type { @@ -336,3 +347,4 @@ bincode_component_impl!(Particle); bincode_component_impl!(InteractEntityEvent); bincode_component_impl!(BlockPlacementEvent); bincode_component_impl!(BlockInteractEvent); +bincode_component_impl!(CreativeFlyingEvent); diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 09c7f18af..b145d594f 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -98,3 +98,9 @@ impl Display for CustomName { self.0.fmt(f) } } + +/// Whether an entity is flying (like in creative mode) +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct CreativeFlying(pub bool); + +bincode_component_impl!(CreativeFlying); diff --git a/quill/common/src/events.rs b/quill/common/src/events.rs index 6edffe0f1..8802d7b13 100644 --- a/quill/common/src/events.rs +++ b/quill/common/src/events.rs @@ -1,5 +1,7 @@ mod block_interact; +mod change; mod interact_entity; pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; +pub use change::CreativeFlyingEvent; pub use interact_entity::InteractEntityEvent; diff --git a/quill/common/src/events/change.rs b/quill/common/src/events/change.rs new file mode 100644 index 000000000..a91597ec6 --- /dev/null +++ b/quill/common/src/events/change.rs @@ -0,0 +1,13 @@ +use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct CreativeFlyingEvent { + pub is_flying: bool, +} + +impl CreativeFlyingEvent { + pub fn new(changed_to: bool) -> Self { + Self { + is_flying: changed_to, + } + } +} diff --git a/quill/example-plugins/observe-creativemode-flight-event/Cargo.toml b/quill/example-plugins/observe-creativemode-flight-event/Cargo.toml new file mode 100644 index 000000000..77fdfd2eb --- /dev/null +++ b/quill/example-plugins/observe-creativemode-flight-event/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "observe-creativemode-flight-event" +version = "0.1.0" +authors = ["Miro Andrin "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +quill = { path = "../../api" } diff --git a/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs new file mode 100644 index 000000000..befc67ec4 --- /dev/null +++ b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs @@ -0,0 +1,29 @@ +/* +This plugin observers the CreativeFlightEvent printing a msg when someone starts +flying. +*/ + +use quill::{events::CreativeFlyingEvent, Game, Plugin, Setup}; + +quill::plugin!(FlightPlugin); + +struct FlightPlugin {} + +impl Plugin for FlightPlugin { + fn enable(_game: &mut Game, setup: &mut Setup) -> Self { + setup.add_system(flight_observer_system); + FlightPlugin {} + } + + fn disable(self, _game: &mut Game) {} +} + +fn flight_observer_system(_plugin: &mut FlightPlugin, game: &mut Game) { + for (entity, change) in game.query::<&CreativeFlyingEvent>() { + if change.is_flying { + entity.send_message("Enjoy your flight!"); + } else { + entity.send_message("Hope you enjoyed your flight."); + } + } +}