Skip to content

Commit

Permalink
feat: entity utils
Browse files Browse the repository at this point in the history
  • Loading branch information
radstevee committed Dec 30, 2024
1 parent 0575ff3 commit 30664f6
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
"src/lib/utils/logging",
"src/lib/utils/profiling",
"src/lib/world",
"src/lib/entity_utils",
]

#================== Lints ==================#
Expand Down Expand Up @@ -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"] }
Expand Down
19 changes: 0 additions & 19 deletions src/lib/commands/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -52,18 +47,4 @@ impl CommandContext {
todo!();
}
}

pub async fn reply(&self, text: TextComponent) -> NetResult<()> {
let mut stream_writer = self
.state
.universe
.get_mut::<StreamWriter>(self.connection_id)?;

stream_writer
.send_packet(
&SystemMessagePacket::new(text, false),
&NetEncodeOpts::WithLength,
)
.await
}
}
1 change: 1 addition & 0 deletions src/lib/default_commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
23 changes: 12 additions & 11 deletions src/lib/default_commands/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -24,15 +25,15 @@ async fn echo(ctx: Arc<CommandContext>) -> CommandResult {
.get::<PlayerIdentity>(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(())
}
27 changes: 17 additions & 10 deletions src/lib/default_commands/src/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandContext>) -> 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(())
}

Expand All @@ -33,14 +38,16 @@ async fn abc(ctx: Arc<CommandContext>) -> CommandResult {
let word = ctx.arg::<String>("word");
let number = ctx.arg::<i32>("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(())
}
11 changes: 11 additions & 0 deletions src/lib/entity_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions src/lib/entity_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod send_message;
36 changes: 36 additions & 0 deletions src/lib/entity_utils/src/send_message.rs
Original file line number Diff line number Diff line change
@@ -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::<StreamWriter>(*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::<StreamWriter>(*self)?;
writer
.send_packet(
&SystemMessagePacket::new(message, true),
&NetEncodeOpts::WithLength,
)
.await
}
}

0 comments on commit 30664f6

Please sign in to comment.