From b30f995a8b352ba37e4e74fd99b45042bf7f121c Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 15:10:41 +0200 Subject: [PATCH 1/6] Added CreativeFlyingComponent. --- quill/common/src/component.rs | 12 +++++++++++- quill/common/src/components.rs | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index 12be60756..e16a697ae 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -183,7 +183,9 @@ host_component_enum! { Particle = 1005, InteractEntityEvent = 1006, BlockPlacementEvent = 1007, - BlockInteractEvent = 1008 + BlockInteractEvent = 1008, + CreativeFlying = 1009, + } } @@ -303,6 +305,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 { diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index 09c7f18af..be135b664 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); \ No newline at end of file From 88c21f5bcd2541bbfc810334c64264e197b75b16 Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 16:28:25 +0200 Subject: [PATCH 2/6] basic packet handeling --- feather/server/src/packet_handlers.rs | 5 +- .../server/src/packet_handlers/movement.rs | 58 +++++++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/feather/server/src/packet_handlers.rs b/feather/server/src/packet_handlers.rs index 5b9288681..aba6045fe 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(server, player, 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..8fac41812 100644 --- a/feather/server/src/packet_handlers/movement.rs +++ b/feather/server/src/packet_handlers/movement.rs @@ -1,9 +1,7 @@ use base::Position; use ecs::{EntityRef, SysResult}; -use protocol::packets::client::{ - PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation, -}; -use quill_common::components::OnGround; +use protocol::packets::{client::{PlayerAbilities, PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation}}; +use quill_common::components::{CreativeFlying, OnGround}; use crate::{ClientId, Server}; @@ -89,3 +87,55 @@ 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( + server: &Server, + player: EntityRef, + packet: PlayerAbilities, +) -> SysResult { + + let mut flying = player.get_mut::()?; + + match packet.flags { + 0 => { + // Flying stopped + + if flying.0 { + // 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. + + // @TODO + } + + flying.0 = false; + }, + 2 => { + // Flying started + + if ! flying.0 { + // 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. + + + // @TODO + } + + flying.0 = false; + }, + err => { + // Unexpected flat value + log::error!("Got a unexpected flag in the PlayerAbilities packet. The value was: {} and not 0 or 2.", err) + } + } + + Ok(()) + + + +} \ No newline at end of file From ce148f8935cd7fe1e52e538d7b4fd3db6f2af265 Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 17:01:51 +0200 Subject: [PATCH 3/6] Added CreativeFlying component to all players. --- feather/common/src/entities/player.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index 6e8d5c878..a360fcd6d 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -1,12 +1,15 @@ 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 #[derive(Debug, Clone, Copy, Eq, PartialEq)] From f4b648672a6f17ce1c6bc5bb1dd3ce63b72bbe84 Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 20:14:40 +0200 Subject: [PATCH 4/6] Added change event to CreativeFlying --- feather/server/src/packet_handlers.rs | 2 +- .../server/src/packet_handlers/movement.rs | 57 +++++++++---------- quill/common/src/component.rs | 8 ++- quill/common/src/events.rs | 2 + quill/common/src/events/change.rs | 13 +++++ 5 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 quill/common/src/events/change.rs diff --git a/feather/server/src/packet_handlers.rs b/feather/server/src/packet_handlers.rs index aba6045fe..008219249 100644 --- a/feather/server/src/packet_handlers.rs +++ b/feather/server/src/packet_handlers.rs @@ -67,7 +67,7 @@ pub fn handle_packet( ClientPlayPacket::ClientSettings(packet) => handle_client_settings(server, player, packet), ClientPlayPacket::PlayerAbilities(packet) => { - movement::handle_player_abilities(server, player, packet) + movement::handle_player_abilities(game, player_id,packet) } ClientPlayPacket::TeleportConfirm(_) diff --git a/feather/server/src/packet_handlers/movement.rs b/feather/server/src/packet_handlers/movement.rs index 8fac41812..ed812999e 100644 --- a/feather/server/src/packet_handlers/movement.rs +++ b/feather/server/src/packet_handlers/movement.rs @@ -1,7 +1,13 @@ use base::Position; -use ecs::{EntityRef, SysResult}; -use protocol::packets::{client::{PlayerAbilities, PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation}}; -use quill_common::components::{CreativeFlying, OnGround}; +use common::Game; +use ecs::{Entity, EntityRef, SysResult}; +use protocol::packets::client::{ + PlayerAbilities, PlayerMovement, PlayerPosition, PlayerPositionAndRotation, PlayerRotation, +}; +use quill_common::{ + components::{CreativeFlying, OnGround}, + events::CreativeFlyingEvent, +}; use crate::{ClientId, Server}; @@ -88,54 +94,45 @@ 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, if the client wants to /// start/stop flying (like in creative mode). pub fn handle_player_abilities( - server: &Server, - player: EntityRef, + game: &mut Game, + player: Entity, packet: PlayerAbilities, ) -> SysResult { - - let mut flying = player.get_mut::()?; + let flying = game.ecs.get_mut::(player)?.0; match packet.flags { 0 => { // Flying stopped - - if flying.0 { + 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. - - // @TODO - } + // always trigger. + game.ecs + .insert_entity_event(player, CreativeFlyingEvent::new(false))?; - flying.0 = false; - }, + game.ecs.get_mut::(player)?.0 = false; + } + } 2 => { // Flying started - - if ! flying.0 { + 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. - - - // @TODO + // always trigger. + game.ecs + .insert_entity_event(player, CreativeFlyingEvent::new(true))?; + game.ecs.get_mut::(player)?.0 = true; } - - flying.0 = false; - }, + } err => { - // Unexpected flat value log::error!("Got a unexpected flag in the PlayerAbilities packet. The value was: {} and not 0 or 2.", err) } } Ok(()) - - - -} \ No newline at end of file +} diff --git a/quill/common/src/component.rs b/quill/common/src/component.rs index e16a697ae..25342584d 100644 --- a/quill/common/src/component.rs +++ b/quill/common/src/component.rs @@ -185,6 +185,7 @@ host_component_enum! { BlockPlacementEvent = 1007, BlockInteractEvent = 1008, CreativeFlying = 1009, + CreativeFlyingEvent = 1010, } } @@ -308,10 +309,10 @@ 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. + 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. +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) => { @@ -346,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/events.rs b/quill/common/src/events.rs index 6edffe0f1..c5f5896ef 100644 --- a/quill/common/src/events.rs +++ b/quill/common/src/events.rs @@ -1,5 +1,7 @@ mod block_interact; mod interact_entity; +mod change; pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; pub use interact_entity::InteractEntityEvent; +pub use change::CreativeFlyingEvent; diff --git a/quill/common/src/events/change.rs b/quill/common/src/events/change.rs new file mode 100644 index 000000000..1ec9d60fc --- /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 + } + } +} \ No newline at end of file From 1d81c5776b2b9bf34273690f48e8b8cb2c0a9d6f Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 20:15:22 +0200 Subject: [PATCH 5/6] Added example plugin --- Cargo.toml | 1 + .../Cargo.toml | 11 +++++++ .../src/lib.rs | 32 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 quill/example-plugins/observe-creativemode-flight-event/Cargo.toml create mode 100644 quill/example-plugins/observe-creativemode-flight-event/src/lib.rs 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/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..39c78015a --- /dev/null +++ b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs @@ -0,0 +1,32 @@ +/* +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."); + } + } +} From c2869c1a4ac2b0592180b594da757dd2f2dbd061 Mon Sep 17 00:00:00 2001 From: Miro Andrin Date: Tue, 11 May 2021 20:41:41 +0200 Subject: [PATCH 6/6] Fogot to run cargo fmt --- feather/common/src/entities/player.rs | 2 +- feather/server/src/packet_handlers.rs | 2 +- quill/common/src/components.rs | 4 ++-- quill/common/src/events.rs | 4 ++-- quill/common/src/events/change.rs | 4 ++-- .../observe-creativemode-flight-event/src/lib.rs | 5 +---- 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/feather/common/src/entities/player.rs b/feather/common/src/entities/player.rs index a360fcd6d..49ef576c8 100644 --- a/feather/common/src/entities/player.rs +++ b/feather/common/src/entities/player.rs @@ -9,7 +9,7 @@ pub fn build_default(builder: &mut EntityBuilder) { .add(Player) .add(CreativeFlying(false)) .add(EntityKind::Player); -} +} /// The hotbar slot a player's cursor is currently on #[derive(Debug, Clone, Copy, Eq, PartialEq)] diff --git a/feather/server/src/packet_handlers.rs b/feather/server/src/packet_handlers.rs index 008219249..39cfd184d 100644 --- a/feather/server/src/packet_handlers.rs +++ b/feather/server/src/packet_handlers.rs @@ -67,7 +67,7 @@ pub fn handle_packet( ClientPlayPacket::ClientSettings(packet) => handle_client_settings(server, player, packet), ClientPlayPacket::PlayerAbilities(packet) => { - movement::handle_player_abilities(game, player_id,packet) + movement::handle_player_abilities(game, player_id, packet) } ClientPlayPacket::TeleportConfirm(_) diff --git a/quill/common/src/components.rs b/quill/common/src/components.rs index be135b664..b145d594f 100644 --- a/quill/common/src/components.rs +++ b/quill/common/src/components.rs @@ -99,8 +99,8 @@ impl Display for CustomName { } } -/// Whether an entity is flying (like in creative mode) +/// 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); \ No newline at end of file +bincode_component_impl!(CreativeFlying); diff --git a/quill/common/src/events.rs b/quill/common/src/events.rs index c5f5896ef..8802d7b13 100644 --- a/quill/common/src/events.rs +++ b/quill/common/src/events.rs @@ -1,7 +1,7 @@ mod block_interact; -mod interact_entity; mod change; +mod interact_entity; pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; -pub use interact_entity::InteractEntityEvent; 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 index 1ec9d60fc..a91597ec6 100644 --- a/quill/common/src/events/change.rs +++ b/quill/common/src/events/change.rs @@ -7,7 +7,7 @@ pub struct CreativeFlyingEvent { impl CreativeFlyingEvent { pub fn new(changed_to: bool) -> Self { Self { - is_flying: changed_to + is_flying: changed_to, } } -} \ No newline at end of file +} 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 39c78015a..befc67ec4 100644 --- a/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs +++ b/quill/example-plugins/observe-creativemode-flight-event/src/lib.rs @@ -3,10 +3,7 @@ This plugin observers the CreativeFlightEvent printing a msg when someone starts flying. */ -use quill::{ - events::CreativeFlyingEvent, - Game,Plugin,Setup, -}; +use quill::{events::CreativeFlyingEvent, Game, Plugin, Setup}; quill::plugin!(FlightPlugin);