diff --git a/Cargo.toml b/Cargo.toml index 72ff26e7..e130eb0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,15 +12,23 @@ resolver = "2" #================= Members =================# members = [ "src/bin", + "src/lib/adapters/anvil", + "src/lib/adapters/mca", + "src/lib/adapters/nbt", "src/lib/core", + "src/lib/core/state", + "src/lib/derive_macros", "src/lib/ecs", "src/lib/events", "src/lib/net", - "src/lib/net/crates/encryption", "src/lib/net/crates/codec", + "src/lib/net/crates/encryption", "src/lib/plugins", "src/lib/storage", - "src/lib/utils", "src/lib/utils/logging", "src/lib/utils/profiling", "src/lib/utils/general_purpose", + "src/lib/utils", + "src/lib/utils/general_purpose", + "src/lib/utils/logging", + "src/lib/utils/profiling", "src/lib/world", "src/lib/derive_macros", "src/lib/adapters/nbt", "src/lib/adapters/mca", @@ -85,6 +93,7 @@ ferrumc-world = { path = "src/lib/world" } ferrumc-nbt = { path = "src/lib/adapters/nbt" } ferrumc-anvil = { path = "src/lib/adapters/anvil" } ferrumc-tests = { path = "src/tests" } +ferrumc-state = { path = "src/lib/core/state"} # Asynchronous diff --git a/src/bin/Cargo.toml b/src/bin/Cargo.toml index e53332f8..ef9f0c03 100644 --- a/src/bin/Cargo.toml +++ b/src/bin/Cargo.toml @@ -32,6 +32,7 @@ ferrumc-world = { workspace = true } ferrumc-macros = { workspace = true } ferrumc-nbt = { workspace = true } ferrumc-general-purpose = { workspace = true } +ferrumc-state = { workspace = true } ctor = { workspace = true } parking_lot = { workspace = true } diff --git a/src/bin/src/main.rs b/src/bin/src/main.rs index 26f8c42d..5f083103 100644 --- a/src/bin/src/main.rs +++ b/src/bin/src/main.rs @@ -8,7 +8,7 @@ use ferrumc_config::statics::get_global_config; use ferrumc_ecs::Universe; use ferrumc_general_purpose::paths::get_root_path; use ferrumc_net::server::create_server_listener; -use ferrumc_net::ServerState; +use ferrumc_state::ServerState; use ferrumc_world::World; use std::sync::Arc; use systems::definition; diff --git a/src/bin/src/packet_handlers/handshake.rs b/src/bin/src/packet_handlers/handshake.rs index a65d4b47..a58d6af6 100644 --- a/src/bin/src/packet_handlers/handshake.rs +++ b/src/bin/src/packet_handlers/handshake.rs @@ -3,7 +3,7 @@ use ferrumc_net::connection::ConnectionState; use ferrumc_net::errors::NetError::{Packet}; use ferrumc_net::errors::{NetError, PacketError}; use ferrumc_net::packets::incoming::handshake::HandshakeEvent; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use tracing::{error, trace}; use ferrumc_ecs::errors::ECSError; use ferrumc_net::utils::ecs_helpers::EntityExt; diff --git a/src/bin/src/packet_handlers/login_process.rs b/src/bin/src/packet_handlers/login_process.rs index 9e63c695..d96235b2 100644 --- a/src/bin/src/packet_handlers/login_process.rs +++ b/src/bin/src/packet_handlers/login_process.rs @@ -19,7 +19,7 @@ use ferrumc_net::packets::outgoing::set_render_distance::SetRenderDistance; use ferrumc_net::packets::outgoing::registry_data::get_registry_packets; use ferrumc_net::packets::outgoing::set_default_spawn_position::SetDefaultSpawnPositionPacket; use ferrumc_net::packets::outgoing::synchronize_player_position::SynchronizePlayerPositionPacket; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use ferrumc_net_codec::encode::NetEncodeOpts; use tracing::{debug, trace}; use ferrumc_core::transform::grounded::OnGround; diff --git a/src/bin/src/packet_handlers/tick_handler.rs b/src/bin/src/packet_handlers/tick_handler.rs index a53ee58d..117200cd 100644 --- a/src/bin/src/packet_handlers/tick_handler.rs +++ b/src/bin/src/packet_handlers/tick_handler.rs @@ -5,14 +5,11 @@ use ferrumc_net::errors::NetError; use ferrumc_net::packets::outgoing::update_time::TickEvent; use ferrumc_net::packets::outgoing::update_time::UpdateTimePacket; use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll}; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use tracing::warn; #[event_handler] -async fn handle_tick( - event: TickEvent, - state: GlobalState, -) -> Result { +async fn handle_tick(event: TickEvent, state: GlobalState) -> Result { // info!("Tick {} ", event.tick); // TODO: Handle tick in terms of game logic here // this should call a function in world which handles the world state and calls the appropriate events which send their respective packets @@ -36,9 +33,11 @@ async fn handle_tick( }) .collect::>(); - tokio::spawn(async move { - if let Err(e) = state.broadcast(&packet, BroadcastOptions::default().only(entities)).await { + if let Err(e) = state + .broadcast(&packet, BroadcastOptions::default().only(entities)) + .await + { warn!("Failed to broadcast tick packet: {:?}", e); } }); diff --git a/src/bin/src/packet_handlers/transform/update_player_position.rs b/src/bin/src/packet_handlers/transform/update_player_position.rs index 187fe7ba..e5e05cb8 100644 --- a/src/bin/src/packet_handlers/transform/update_player_position.rs +++ b/src/bin/src/packet_handlers/transform/update_player_position.rs @@ -3,9 +3,9 @@ use ferrumc_core::transform::position::Position; use ferrumc_core::transform::rotation::Rotation; use ferrumc_macros::event_handler; use ferrumc_net::errors::NetError; -use ferrumc_net::utils::ecs_helpers::EntityExt; -use ferrumc_net::GlobalState; use ferrumc_net::packets::packet_events::TransformEvent; +use ferrumc_net::utils::ecs_helpers::EntityExt; +use ferrumc_state::GlobalState; #[event_handler] async fn handle_player_move( @@ -16,21 +16,13 @@ async fn handle_player_move( if let Some(ref new_position) = event.position { let mut position = conn_id.get_mut::(&state)?; - *position = Position::new( - new_position.x, - new_position.y, - new_position.z, - ); + *position = Position::new(new_position.x, new_position.y, new_position.z); } if let Some(ref new_rotation) = event.rotation { let mut rotation = conn_id.get_mut::(&state)?; - *rotation = Rotation::new( - new_rotation.yaw, - new_rotation.pitch, - ); - + *rotation = Rotation::new(new_rotation.yaw, new_rotation.pitch); } if let Some(new_grounded) = event.on_ground { @@ -40,4 +32,4 @@ async fn handle_player_move( } Ok(event) -} \ No newline at end of file +} diff --git a/src/bin/src/systems/chunk_sender.rs b/src/bin/src/systems/chunk_sender.rs index f1b0b5a6..8d6869e4 100644 --- a/src/bin/src/systems/chunk_sender.rs +++ b/src/bin/src/systems/chunk_sender.rs @@ -5,8 +5,8 @@ use ferrumc_core::transform::position::Position; use ferrumc_net::connection::StreamWriter; use ferrumc_net::packets::outgoing::chunk_and_light_data::ChunkAndLightData; use ferrumc_net::packets::outgoing::set_center_chunk::SetCenterChunk; -use ferrumc_net::GlobalState; use ferrumc_net_codec::encode::NetEncodeOpts; +use ferrumc_state::GlobalState; use std::ops::Div; use std::simd::num::SimdFloat; use std::simd::{f64x2, StdFloat}; diff --git a/src/bin/src/systems/definition.rs b/src/bin/src/systems/definition.rs index 42b107cb..e54fe459 100644 --- a/src/bin/src/systems/definition.rs +++ b/src/bin/src/systems/definition.rs @@ -2,7 +2,8 @@ use crate::systems::keep_alive_system::KeepAliveSystem; use crate::systems::tcp_listener_system::TcpListenerSystem; use crate::systems::ticking_system::TickingSystem; use async_trait::async_trait; -use ferrumc_net::{GlobalState, NetResult}; +use ferrumc_net::{NetResult}; +use ferrumc_state::GlobalState; use futures::stream::FuturesUnordered; use std::sync::{Arc, LazyLock}; use tracing::{debug, debug_span, info, Instrument}; diff --git a/src/bin/src/systems/keep_alive_system.rs b/src/bin/src/systems/keep_alive_system.rs index fc57ad5b..7fbb6df7 100644 --- a/src/bin/src/systems/keep_alive_system.rs +++ b/src/bin/src/systems/keep_alive_system.rs @@ -6,7 +6,7 @@ use ferrumc_net::packets::incoming::keep_alive::IncomingKeepAlivePacket; use ferrumc_net::packets::outgoing::keep_alive::OutgoingKeepAlivePacket; use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll}; use ferrumc_net::utils::state::terminate_connection; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use tracing::{error, info, trace, warn}; @@ -49,7 +49,10 @@ impl System for KeepAliveSystem { .into_iter() .filter_map(|entity| { let conn_state = state.universe.get::(entity).ok()?; - let keep_alive = state.universe.get_mut::(entity).ok()?; + let keep_alive = state + .universe + .get_mut::(entity) + .ok()?; if matches!(*conn_state, ConnectionState::Play) && (current_time - keep_alive.timestamp) >= 15000 @@ -87,7 +90,9 @@ impl System for KeepAliveSystem { } } } - let packet = OutgoingKeepAlivePacket { timestamp: current_time }; + let packet = OutgoingKeepAlivePacket { + timestamp: current_time, + }; let broadcast_opts = BroadcastOptions::default() .only(entities) @@ -106,7 +111,12 @@ impl System for KeepAliveSystem { }); if let Err(e) = state - .broadcast(&OutgoingKeepAlivePacket { timestamp: current_time }, broadcast_opts) + .broadcast( + &OutgoingKeepAlivePacket { + timestamp: current_time, + }, + broadcast_opts, + ) .await { error!("Error sending keep alive packet: {}", e); diff --git a/src/bin/src/systems/tcp_listener_system.rs b/src/bin/src/systems/tcp_listener_system.rs index 605ad169..8898f7a8 100644 --- a/src/bin/src/systems/tcp_listener_system.rs +++ b/src/bin/src/systems/tcp_listener_system.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use async_trait::async_trait; use tracing::{debug, error, info, info_span, Instrument}; use ferrumc_net::connection::handle_connection; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use crate::systems::definition::System; use crate::Result; diff --git a/src/bin/src/systems/ticking_system.rs b/src/bin/src/systems/ticking_system.rs index e4a2a511..ffe6f21f 100644 --- a/src/bin/src/systems/ticking_system.rs +++ b/src/bin/src/systems/ticking_system.rs @@ -2,7 +2,7 @@ use crate::systems::definition::System; use async_trait::async_trait; use ferrumc_events::infrastructure::Event; use ferrumc_net::packets::outgoing::update_time::TickEvent; -use ferrumc_net::GlobalState; +use ferrumc_state::GlobalState; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Duration; diff --git a/src/lib/core/Cargo.toml b/src/lib/core/Cargo.toml index c0a9f01e..79e2e1c3 100644 --- a/src/lib/core/Cargo.toml +++ b/src/lib/core/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] thiserror = { workspace = true } +tokio = { workspace = true} +ferrumc-ecs = {workspace = true} diff --git a/src/lib/core/src/lib.rs b/src/lib/core/src/lib.rs index f048b6fa..09a67c3b 100644 --- a/src/lib/core/src/lib.rs +++ b/src/lib/core/src/lib.rs @@ -3,4 +3,5 @@ pub mod errors; // Core structs/types. Usually used in ECS Components. pub mod transform; -pub mod identity; \ No newline at end of file +pub mod identity; +pub mod state; \ No newline at end of file diff --git a/src/lib/core/src/state.rs b/src/lib/core/src/state.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/core/state/Cargo.toml b/src/lib/core/state/Cargo.toml new file mode 100644 index 00000000..c6ef8db4 --- /dev/null +++ b/src/lib/core/state/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "ferrumc-state" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { workspace = true } +ferrumc-ecs = { workspace = true } +ferrumc-world = { workspace = true } diff --git a/src/lib/core/state/src/lib.rs b/src/lib/core/state/src/lib.rs new file mode 100644 index 00000000..3df5d4b4 --- /dev/null +++ b/src/lib/core/state/src/lib.rs @@ -0,0 +1,11 @@ +use ferrumc_ecs::Universe; +use ferrumc_world::World; +use std::sync::Arc; +use tokio::net::TcpListener; +pub struct ServerState { + pub universe: Universe, + pub tcp_listener: TcpListener, + pub world: World, +} + +pub type GlobalState = Arc; diff --git a/src/lib/derive_macros/src/events/mod.rs b/src/lib/derive_macros/src/events/mod.rs index fffa9cd8..d46d9caf 100644 --- a/src/lib/derive_macros/src/events/mod.rs +++ b/src/lib/derive_macros/src/events/mod.rs @@ -130,7 +130,7 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream { let output = quote! { impl ::ferrumc_events::infrastructure::Event for #name { type Data = Self; - type State = #net_crate::GlobalState; + type State = ferrumc_state::GlobalState; type Error = #net_crate::errors::NetError; fn name() -> &'static str { diff --git a/src/lib/derive_macros/src/net/packets/mod.rs b/src/lib/derive_macros/src/net/packets/mod.rs index 548ed5f1..f171511d 100644 --- a/src/lib/derive_macros/src/net/packets/mod.rs +++ b/src/lib/derive_macros/src/net/packets/mod.rs @@ -144,7 +144,7 @@ pub fn bake_registry(input: TokenStream) -> TokenStream { let match_arms = match_arms.into_iter(); let output = quote! { - pub async fn handle_packet(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc) -> crate::NetResult<()> { + pub async fn handle_packet(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc) -> crate::NetResult<()> { match (packet_id, conn_state.as_str()) { #(#match_arms)* _ => tracing::debug!("No packet found for ID: 0x{:02X} in state: {}", packet_id, conn_state.as_str()), diff --git a/src/lib/net/Cargo.toml b/src/lib/net/Cargo.toml index 39e08eba..dd1ad654 100644 --- a/src/lib/net/Cargo.toml +++ b/src/lib/net/Cargo.toml @@ -30,3 +30,4 @@ rand = { workspace = true } uuid = { workspace = true, features = ["v4"] } async-trait = { workspace = true } byteorder = { workspace = true } +ferrumc-state = { workspace = true } diff --git a/src/lib/net/src/connection.rs b/src/lib/net/src/connection.rs index 5f0bc6fb..63f190b4 100644 --- a/src/lib/net/src/connection.rs +++ b/src/lib/net/src/connection.rs @@ -1,8 +1,9 @@ use crate::packets::incoming::packet_skeleton::PacketSkeleton; use crate::utils::state::terminate_connection; -use crate::{handle_packet, NetResult, ServerState}; +use crate::{handle_packet, NetResult}; use ferrumc_net_codec::encode::NetEncode; use ferrumc_net_codec::encode::NetEncodeOpts; +use ferrumc_state::ServerState; use std::sync::Arc; use std::time::Duration; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; diff --git a/src/lib/net/src/lib.rs b/src/lib/net/src/lib.rs index f6808a34..8187ddcb 100644 --- a/src/lib/net/src/lib.rs +++ b/src/lib/net/src/lib.rs @@ -1,11 +1,7 @@ #![feature(portable_simd)] extern crate core; -use tokio::net::TcpListener; -use ferrumc_ecs::Universe; use ferrumc_macros::bake_packet_registry; -use std::sync::{Arc}; -use ferrumc_world::World; pub mod connection; pub mod errors; @@ -14,13 +10,7 @@ pub mod server; pub mod utils; pub type NetResult = Result; -pub struct ServerState { - pub universe: Universe, - pub tcp_listener: TcpListener, - pub world: World -} -pub type GlobalState = Arc; bake_packet_registry!("\\src\\packets\\incoming"); diff --git a/src/lib/net/src/packets/incoming/ack_finish_configuration.rs b/src/lib/net/src/packets/incoming/ack_finish_configuration.rs index 27b070f9..4ea1168f 100644 --- a/src/lib/net/src/packets/incoming/ack_finish_configuration.rs +++ b/src/lib/net/src/packets/incoming/ack_finish_configuration.rs @@ -1,8 +1,9 @@ use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; +use ferrumc_state::ServerState; +use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, Event, NetDecode}; use std::sync::Arc; -use ferrumc_events::infrastructure::Event; #[derive(NetDecode)] #[packet(packet_id = 0x03, state = "configuration")] @@ -11,9 +12,9 @@ pub struct AckFinishConfigurationPacket {} impl IncomingPacket for AckFinishConfigurationPacket { async fn handle(self, conn_id: usize, state: Arc) -> NetResult<()> { let event = AckFinishConfigurationEvent::new(self, conn_id); - + tokio::spawn(AckFinishConfigurationEvent::trigger(event, state)); - + Ok(()) } } diff --git a/src/lib/net/src/packets/incoming/client_information.rs b/src/lib/net/src/packets/incoming/client_information.rs index 053a04a8..c4654ecc 100644 --- a/src/lib/net/src/packets/incoming/client_information.rs +++ b/src/lib/net/src/packets/incoming/client_information.rs @@ -1,9 +1,10 @@ use std::sync::Arc; use tracing::debug; +use ferrumc_state::ServerState; use ferrumc_macros::{packet, NetDecode}; use ferrumc_net_codec::net_types::var_int::VarInt; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; #[derive(Debug, NetDecode)] #[packet(packet_id = 0x00, state = "configuration")] diff --git a/src/lib/net/src/packets/incoming/handshake.rs b/src/lib/net/src/packets/incoming/handshake.rs index 7dffb511..39d0569f 100644 --- a/src/lib/net/src/packets/incoming/handshake.rs +++ b/src/lib/net/src/packets/incoming/handshake.rs @@ -1,5 +1,5 @@ use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, Event, NetDecode}; use ferrumc_net_codec::net_types::var_int::VarInt; diff --git a/src/lib/net/src/packets/incoming/keep_alive.rs b/src/lib/net/src/packets/incoming/keep_alive.rs index 2aa6049a..a71239f0 100644 --- a/src/lib/net/src/packets/incoming/keep_alive.rs +++ b/src/lib/net/src/packets/incoming/keep_alive.rs @@ -1,8 +1,9 @@ use crate::packets::outgoing::keep_alive::OutgoingKeepAlivePacket; use crate::packets::IncomingPacket; use crate::utils::state::terminate_connection; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_macros::{packet, NetDecode}; +use ferrumc_state::ServerState; use std::sync::Arc; use tracing::debug; @@ -26,7 +27,8 @@ impl IncomingPacket for IncomingKeepAlivePacket { debug!("Error terminating connection: {:?}", e); } } else { - let mut last_rec_keep_alive = state.universe.get_mut::(conn_id)?; + let mut last_rec_keep_alive = + state.universe.get_mut::(conn_id)?; *last_rec_keep_alive = self; } diff --git a/src/lib/net/src/packets/incoming/login_acknowledged.rs b/src/lib/net/src/packets/incoming/login_acknowledged.rs index b1cfd044..ca0e16f3 100644 --- a/src/lib/net/src/packets/incoming/login_acknowledged.rs +++ b/src/lib/net/src/packets/incoming/login_acknowledged.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use ferrumc_macros::{Event, NetDecode, packet}; use ferrumc_events::infrastructure::Event; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; #[derive(Debug, NetDecode)] #[packet(packet_id = 0x03, state = "login")] diff --git a/src/lib/net/src/packets/incoming/login_start.rs b/src/lib/net/src/packets/incoming/login_start.rs index 46926442..9b189b52 100644 --- a/src/lib/net/src/packets/incoming/login_start.rs +++ b/src/lib/net/src/packets/incoming/login_start.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, Event, NetDecode}; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; #[derive(Debug, NetDecode)] #[packet(packet_id = 0x00, state = "login")] diff --git a/src/lib/net/src/packets/incoming/ping.rs b/src/lib/net/src/packets/incoming/ping.rs index f7c51d99..5f575041 100644 --- a/src/lib/net/src/packets/incoming/ping.rs +++ b/src/lib/net/src/packets/incoming/ping.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use ferrumc_macros::{packet, NetDecode}; use ferrumc_net_codec::encode::NetEncodeOpts; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; use crate::connection::StreamWriter; use crate::packets::outgoing::ping_response::PongPacket; diff --git a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs index 8551474f..ff48f711 100644 --- a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs +++ b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs @@ -4,7 +4,7 @@ use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, Event, NetDecode}; use ferrumc_net_codec::net_types::length_prefixed_vec::LengthPrefixedVec; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; #[derive(Debug, NetDecode)] #[packet(packet_id = 0x07, state = "configuration")] diff --git a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs index fc1dd4f2..e3c393aa 100644 --- a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs +++ b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs @@ -4,7 +4,7 @@ use tracing::debug; use ferrumc_macros::{packet}; use ferrumc_net_codec::decode::{NetDecode, NetDecodeOpts, NetDecodeResult}; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; #[derive(Debug)] #[packet(packet_id = 0x02, state = "configuration")] diff --git a/src/lib/net/src/packets/incoming/set_player_position.rs b/src/lib/net/src/packets/incoming/set_player_position.rs index 5656b9a2..f02b6949 100644 --- a/src/lib/net/src/packets/incoming/set_player_position.rs +++ b/src/lib/net/src/packets/incoming/set_player_position.rs @@ -1,9 +1,10 @@ -use std::sync::Arc; +use crate::packets::packet_events::TransformEvent; +use crate::packets::IncomingPacket; +use crate::NetResult; use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, NetDecode}; -use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; -use crate::packets::packet_events::TransformEvent; +use ferrumc_state::ServerState; +use std::sync::Arc; #[derive(NetDecode)] #[packet(packet_id = 0x1A, state = "play")] @@ -11,7 +12,7 @@ pub struct SetPlayerPositionPacket { pub x: f64, pub feet_y: f64, pub z: f64, - pub on_ground: bool + pub on_ground: bool, } impl IncomingPacket for SetPlayerPositionPacket { @@ -24,4 +25,4 @@ impl IncomingPacket for SetPlayerPositionPacket { Ok(()) } -} \ No newline at end of file +} diff --git a/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs b/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs index 415b742e..afae64d9 100644 --- a/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs +++ b/src/lib/net/src/packets/incoming/set_player_position_and_rotation.rs @@ -1,9 +1,10 @@ -use std::sync::Arc; +use crate::packets::packet_events::TransformEvent; +use crate::packets::IncomingPacket; +use crate::NetResult; use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, NetDecode}; -use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; -use crate::packets::packet_events::TransformEvent; +use ferrumc_state::ServerState; +use std::sync::Arc; #[derive(NetDecode)] #[packet(packet_id = 0x1B, state = "play")] @@ -13,7 +14,7 @@ pub struct SetPlayerPositionAndRotationPacket { pub z: f64, pub yaw: f32, pub pitch: f32, - pub on_ground: bool + pub on_ground: bool, } impl IncomingPacket for SetPlayerPositionAndRotationPacket { @@ -24,7 +25,7 @@ impl IncomingPacket for SetPlayerPositionAndRotationPacket { .on_ground(self.on_ground); TransformEvent::trigger(event, state).await?; - + Ok(()) } -} \ No newline at end of file +} diff --git a/src/lib/net/src/packets/incoming/set_player_rotation.rs b/src/lib/net/src/packets/incoming/set_player_rotation.rs index 72fb39ee..99413cc9 100644 --- a/src/lib/net/src/packets/incoming/set_player_rotation.rs +++ b/src/lib/net/src/packets/incoming/set_player_rotation.rs @@ -1,19 +1,20 @@ -use std::sync::Arc; +use crate::packets::packet_events::TransformEvent; +use crate::packets::IncomingPacket; +use crate::NetResult; use ferrumc_events::infrastructure::Event; use ferrumc_macros::{packet, NetDecode}; -use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; -use crate::packets::packet_events::TransformEvent; +use ferrumc_state::ServerState; +use std::sync::Arc; #[derive(NetDecode)] #[packet(packet_id = 0x1C, state = "play")] pub struct SetPlayerRotationPacket { pub yaw: f32, pub pitch: f32, - pub on_ground: bool + pub on_ground: bool, } -impl IncomingPacket for SetPlayerRotationPacket { +impl IncomingPacket for SetPlayerRotationPacket { async fn handle(self, conn_id: usize, state: Arc) -> NetResult<()> { let event = TransformEvent::new(conn_id) .rotation((self.yaw, self.pitch).into()) @@ -23,4 +24,4 @@ impl IncomingPacket for SetPlayerRotationPacket { Ok(()) } -} \ No newline at end of file +} diff --git a/src/lib/net/src/packets/incoming/status_request.rs b/src/lib/net/src/packets/incoming/status_request.rs index c23e0e98..bf7983ef 100644 --- a/src/lib/net/src/packets/incoming/status_request.rs +++ b/src/lib/net/src/packets/incoming/status_request.rs @@ -1,7 +1,7 @@ use crate::connection::StreamWriter; use crate::packets::outgoing::status_response::StatusResponse; use crate::packets::IncomingPacket; -use crate::{NetResult, ServerState}; +use crate::NetResult; use ferrumc_state::ServerState; use ferrumc_config::favicon::get_favicon_base64; use ferrumc_config::statics::get_global_config; use ferrumc_core::identity::player_identity::PlayerIdentity; diff --git a/src/lib/net/src/packets/mod.rs b/src/lib/net/src/packets/mod.rs index 40d8501c..f698e879 100644 --- a/src/lib/net/src/packets/mod.rs +++ b/src/lib/net/src/packets/mod.rs @@ -6,5 +6,5 @@ pub mod packet_events; #[allow(async_fn_in_trait)] pub trait IncomingPacket { - async fn handle(self, conn_id: usize, state: std::sync::Arc) -> NetResult<()>; + async fn handle(self, conn_id: usize, state: std::sync::Arc) -> NetResult<()>; } \ No newline at end of file diff --git a/src/lib/net/src/utils/broadcast.rs b/src/lib/net/src/utils/broadcast.rs index 8eb6b148..6cb14cdd 100644 --- a/src/lib/net/src/utils/broadcast.rs +++ b/src/lib/net/src/utils/broadcast.rs @@ -5,7 +5,8 @@ use futures::StreamExt; use tracing::debug; use ferrumc_ecs::entities::Entity; use ferrumc_net_codec::encode::{NetEncode, NetEncodeOpts}; -use crate::{GlobalState, NetResult}; +use crate::NetResult; +use ferrumc_state::GlobalState; use crate::connection::StreamWriter; type AsyncCallbackFn = Box Pin + Send + '_>> + Send + Sync>; diff --git a/src/lib/net/src/utils/ecs_helpers.rs b/src/lib/net/src/utils/ecs_helpers.rs index 821b8567..c8dd4c20 100644 --- a/src/lib/net/src/utils/ecs_helpers.rs +++ b/src/lib/net/src/utils/ecs_helpers.rs @@ -1,6 +1,6 @@ use ferrumc_ecs::components::storage::{Component, ComponentRef, ComponentRefMut}; use ferrumc_ecs::ECSResult; -use crate::GlobalState; +use ferrumc_state::GlobalState; pub trait EntityExt { fn get(&self, state: &GlobalState) -> ECSResult>; diff --git a/src/lib/net/src/utils/state.rs b/src/lib/net/src/utils/state.rs index 3a114927..9431c361 100644 --- a/src/lib/net/src/utils/state.rs +++ b/src/lib/net/src/utils/state.rs @@ -2,9 +2,10 @@ use crate::{ connection::{ConnectionControl, StreamWriter}, errors::NetError, packets::outgoing::disconnect::DisconnectPacket, - GlobalState, NetResult, + NetResult, }; use ferrumc_net_codec::encode::NetEncodeOpts; +use ferrumc_state::GlobalState; use tracing::{trace, warn}; use super::ecs_helpers::EntityExt; @@ -35,7 +36,10 @@ pub async fn terminate_connection( }; if let Err(e) = writer - .send_packet(&DisconnectPacket::from_string(reason), &NetEncodeOpts::WithLength) + .send_packet( + &DisconnectPacket::from_string(reason), + &NetEncodeOpts::WithLength, + ) .await { warn!(