diff --git a/Cargo.toml b/Cargo.toml index cfb3dbcb..d909232a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ members = [ "src/lib/utils/logging", "src/lib/utils/profiling", "src/lib/world", + "src/lib/entity_utils", ] #================== Lints ==================# @@ -106,7 +107,7 @@ ferrumc-storage = { path = "src/lib/storage" } ferrumc-text = { path = "src/lib/text" } ferrumc-utils = { path = "src/lib/utils" } ferrumc-world = { path = "src/lib/world" } - +ferrumc-entity-utils = { path = "src/lib/entity_utils" } # Asynchronous tokio = { version = "1.40.0", features = ["full"] } diff --git a/src/lib/commands/src/ctx.rs b/src/lib/commands/src/ctx.rs index 4bb7ceea..fa71a3db 100644 --- a/src/lib/commands/src/ctx.rs +++ b/src/lib/commands/src/ctx.rs @@ -3,12 +3,7 @@ use std::{ sync::{Arc, Mutex}, }; -use ferrumc_net::{ - connection::StreamWriter, packets::outgoing::system_message::SystemMessagePacket, NetResult, -}; -use ferrumc_net_codec::encode::NetEncodeOpts; use ferrumc_state::GlobalState; -use ferrumc_text::TextComponent; use crate::{input::CommandInput, Command}; @@ -52,18 +47,4 @@ impl CommandContext { todo!(); } } - - pub async fn reply(&self, text: TextComponent) -> NetResult<()> { - let mut stream_writer = self - .state - .universe - .get_mut::(self.connection_id)?; - - stream_writer - .send_packet( - &SystemMessagePacket::new(text, false), - &NetEncodeOpts::WithLength, - ) - .await - } } diff --git a/src/lib/default_commands/Cargo.toml b/src/lib/default_commands/Cargo.toml index a637bac9..c1349694 100644 --- a/src/lib/default_commands/Cargo.toml +++ b/src/lib/default_commands/Cargo.toml @@ -9,5 +9,6 @@ ferrumc-ecs = { workspace = true } ferrumc-macros = { workspace = true } ferrumc-text = { workspace = true } ferrumc-core = { workspace = true } +ferrumc-entity-utils = { workspace = true } ctor = { workspace = true } tracing = { workspace = true } diff --git a/src/lib/default_commands/src/echo.rs b/src/lib/default_commands/src/echo.rs index 1f94ec58..5bfd761f 100644 --- a/src/lib/default_commands/src/echo.rs +++ b/src/lib/default_commands/src/echo.rs @@ -11,6 +11,7 @@ use ferrumc_commands::{ Command, CommandResult, }; use ferrumc_core::identity::player_identity::PlayerIdentity; +use ferrumc_entity_utils::send_message::SendMessageExt; use ferrumc_macros::{arg, command}; use ferrumc_text::{NamedColor, TextComponentBuilder}; @@ -24,15 +25,15 @@ async fn echo(ctx: Arc) -> CommandResult { .get::(ctx.connection_id) .expect("failed to get identity"); - ctx.reply( - TextComponentBuilder::new(format!("{} said: {message}", identity.username)) - .color(NamedColor::Green) - .build(), - ) - .await - .map_err(|err| { - TextComponentBuilder::new(err.to_string()) - .color(NamedColor::Red) - .build() - }) + ctx.connection_id + .send_message( + TextComponentBuilder::new(format!("{} said: {message}", identity.username)) + .color(NamedColor::Green) + .build(), + &ctx.state.universe, + ) + .await + .expect("failed sending message"); + + Ok(()) } diff --git a/src/lib/default_commands/src/nested.rs b/src/lib/default_commands/src/nested.rs index 74487df4..44dbbc65 100644 --- a/src/lib/default_commands/src/nested.rs +++ b/src/lib/default_commands/src/nested.rs @@ -13,14 +13,19 @@ use ferrumc_commands::{ infrastructure::register_command, Command, CommandResult, }; +use ferrumc_entity_utils::send_message::SendMessageExt; use ferrumc_macros::{arg, command}; use ferrumc_text::TextComponentBuilder; #[command("nested")] async fn root(ctx: Arc) -> CommandResult { - ctx.reply(TextComponentBuilder::new("Executed /nested").build()) + ctx.connection_id + .send_message( + TextComponentBuilder::new("Executed /nested").build(), + &ctx.state.universe, + ) .await - .unwrap(); + .expect("failed sending message"); Ok(()) } @@ -33,14 +38,16 @@ async fn abc(ctx: Arc) -> CommandResult { let word = ctx.arg::("word"); let number = ctx.arg::("number"); - ctx.reply( - TextComponentBuilder::new(format!( - "Message: {message:?}, Word: {word:?}, Number: {number}" - )) - .build(), - ) - .await - .unwrap(); + ctx.connection_id + .send_message( + TextComponentBuilder::new(format!( + "Message: {message:?}, Word: {word:?}, Number: {number}" + )) + .build(), + &ctx.state.universe, + ) + .await + .expect("failed sending message"); Ok(()) } diff --git a/src/lib/entity_utils/Cargo.toml b/src/lib/entity_utils/Cargo.toml new file mode 100644 index 00000000..5cb59d75 --- /dev/null +++ b/src/lib/entity_utils/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ferrumc-entity-utils" +version = "0.1.0" +edition = "2024" + +[dependencies] +ferrumc-net = { workspace = true } +ferrumc-net-codec = { workspace = true } +ferrumc-ecs = { workspace = true } +ferrumc-text = { workspace = true } +async-trait = { workspace = true } diff --git a/src/lib/entity_utils/src/lib.rs b/src/lib/entity_utils/src/lib.rs new file mode 100644 index 00000000..0ef7ce44 --- /dev/null +++ b/src/lib/entity_utils/src/lib.rs @@ -0,0 +1 @@ +pub mod send_message; diff --git a/src/lib/entity_utils/src/send_message.rs b/src/lib/entity_utils/src/send_message.rs new file mode 100644 index 00000000..16f1f487 --- /dev/null +++ b/src/lib/entity_utils/src/send_message.rs @@ -0,0 +1,36 @@ +use async_trait::async_trait; +use ferrumc_ecs::Universe; +use ferrumc_net::{ + NetResult, connection::StreamWriter, packets::outgoing::system_message::SystemMessagePacket, +}; +use ferrumc_net_codec::encode::NetEncodeOpts; +use ferrumc_text::TextComponent; + +#[async_trait] +pub trait SendMessageExt { + async fn send_message(&self, message: TextComponent, universe: &Universe) -> NetResult<()>; + async fn send_actionbar(&self, message: TextComponent, universe: &Universe) -> NetResult<()>; +} + +#[async_trait] +impl SendMessageExt for usize { + async fn send_message(&self, message: TextComponent, universe: &Universe) -> NetResult<()> { + let mut writer = universe.get_mut::(*self)?; + writer + .send_packet( + &SystemMessagePacket::new(message, false), + &NetEncodeOpts::WithLength, + ) + .await + } + + async fn send_actionbar(&self, message: TextComponent, universe: &Universe) -> NetResult<()> { + let mut writer = universe.get_mut::(*self)?; + writer + .send_packet( + &SystemMessagePacket::new(message, true), + &NetEncodeOpts::WithLength, + ) + .await + } +}