Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server #92

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ bedrockrs_shared = { path = "crates/shared" }

bedrockrs_proto = { path = "crates/proto", optional = true }
bedrockrs_proto_core = { path = "crates/proto_core", optional = true }

bedrockrs_server = { path = "crates/server", optional = true }

bedrockrs_macros = { path = "crates/macros", optional = true }

bedrockrs_addon = { path = "crates/addon", optional = true }
Expand All @@ -27,10 +30,11 @@ tokio = { version = "1.40", features = ["full"] }
nbtx = { git = "https://github.com/bedrock-crustaceans/nbtx" }

[features]
full = ["addon", "proto", "world"]
full = ["addons", "world", "proto", "server"]

addon = ["dep:bedrockrs_addon"]
addons = ["dep:bedrockrs_addon"]
proto = ["dep:bedrockrs_proto", "dep:bedrockrs_proto_core", "dep:bedrockrs_macros"]
server = ["dep:bedrockrs_server"]
world = ["dep:bedrockrs_world", "dep:bedrockrs_paletted_storage"]

[[example]]
Expand Down
23 changes: 23 additions & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "bedrockrs_server"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "2.0"

bedrockrs_core = { path = "../core" }
bedrockrs_shared = { path = "../shared" }
bedrockrs_proto = { path = "../proto" }

tokio = { version = "1.40", features = ["full", "tracing"] }
shipyard = { version = "0.7", features = ["parallel", "proc", "rayon", "tracing"]}
rayon = "1.10"
vek = "0.17"

xuid = "1.0"

[features]
scoreboard = []
forms = []
visibility = []
8 changes: 8 additions & 0 deletions crates/server/src/ecs/components/connected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use shipyard::Component;
use bedrockrs_proto::connection::shard::arc::ConnectionShared;
use bedrockrs_proto::helper::ProtoHelper;

Check failure on line 3 in crates/server/src/ecs/components/connected.rs

View workflow job for this annotation

GitHub Actions / Check

module `helper` is private

Check failure on line 3 in crates/server/src/ecs/components/connected.rs

View workflow job for this annotation

GitHub Actions / Test Suite

module `helper` is private

#[derive(Component)]
pub struct Connected<T: ProtoHelper + 'static> where <T as ProtoHelper>::GamePacketType: Sync {
pub connection: ConnectionShared<T>,
}
6 changes: 6 additions & 0 deletions crates/server/src/ecs/components/damage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use shipyard::Component;

#[derive(Component)]
pub struct Damage {
pub damage: f32,
}
8 changes: 8 additions & 0 deletions crates/server/src/ecs/components/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use shipyard::Component;

#[derive(Component)]
pub struct Health {
pub current: f32,
pub min: f32,
pub max: f32,
}
5 changes: 5 additions & 0 deletions crates/server/src/ecs/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod connected;
pub mod position;
pub mod velocity;
mod health;
mod damage;
5 changes: 5 additions & 0 deletions crates/server/src/ecs/components/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use shipyard::Component;
use vek::Vec3;

#[derive(Component)]
pub struct Pos(pub Vec3<f32>);
5 changes: 5 additions & 0 deletions crates/server/src/ecs/components/velocity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use shipyard::Component;
use vek::Vec3;

#[derive(Component)]
pub struct Vel(pub Vec3<f32>);
2 changes: 2 additions & 0 deletions crates/server/src/ecs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod components;
pub mod systems;
5 changes: 5 additions & 0 deletions crates/server/src/ecs/systems/death.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use shipyard::World;

pub fn death() {

}
1 change: 1 addition & 0 deletions crates/server/src/ecs/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod death;
20 changes: 20 additions & 0 deletions crates/server/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::error::Error;
use thiserror::Error;
use bedrockrs_proto::error::ConnectionError;

pub enum StartError {

}

#[derive(Error, Debug)]
pub enum LoginError {
#[error("Connection Error: {0}")]
ConnectionError(#[from] ConnectionError),
#[error("Login aborted, reason: {reason}")]
Abort { reason: String },
#[error("Wrong protocol version (client: {client}, server: {server:?})")]
WrongProtocolVersion { client: i32, server: Vec<i32> },
#[error("Format Error: {0}")]
FormatError(String),
}

4 changes: 4 additions & 0 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod server;
pub mod login;
pub mod error;
pub mod ecs;
3 changes: 3 additions & 0 deletions crates/server/src/login/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait LoginHandler {

}
14 changes: 14 additions & 0 deletions crates/server/src/login/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod handler;

use bedrockrs_proto::connection::Connection;
use shipyard::World;
use bedrockrs_proto::connection::shard::arc::{ConnectionShared, shard};
use bedrockrs_proto::version::v729::helper::ProtoHelperV729;

Check failure on line 6 in crates/server/src/login/mod.rs

View workflow job for this annotation

GitHub Actions / Check

module `version` is private

Check failure on line 6 in crates/server/src/login/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

module `version` is private
use crate::error::LoginError;
use crate::login::handler::LoginHandler;

pub async fn login(connection: Connection, world: &mut World, login_handler: impl LoginHandler) -> Result<(), LoginError> {
let mut shard = shard::<ProtoHelperV729>(connection);

todo!()
}
69 changes: 69 additions & 0 deletions crates/server/src/server/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::server::Server;
use bedrockrs_proto::listener::Listener;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

pub struct ServerBuilder {
name: String,
sub_name: String,
listeners_info: Vec<SocketAddr>,
max_player: u32,
}

impl ServerBuilder {
pub fn new() -> ServerBuilder {
Self::default()
}

pub fn name(mut self, name: &str) -> ServerBuilder {
self.name = name.to_owned();
self
}

pub fn sub_name(mut self, sub_name: &str) -> ServerBuilder {
self.sub_name = sub_name.to_owned();
self
}

pub fn listener(mut self, addr: SocketAddr) -> ServerBuilder {
self.listeners_info.push(addr);
self
}

pub async fn build(mut self) -> Server {
if self.listeners_info.is_empty() {
self.listeners_info.push(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 0)), 19132));
}

let mut listeners = Vec::with_capacity(self.listeners_info.len());

for addr in self.listeners_info {
listeners.push(Listener::new_raknet(
self.name.clone(),
self.sub_name.clone(),
String::from("1.21.0"),
self.max_player,
0,
addr,
false,
).await.unwrap())
};

Server {
listeners,
name: self.name,
sub_name: self.sub_name,
world: Default::default(),
}
}
}

impl Default for ServerBuilder {
fn default() -> Self {
Self {
name: "bedrock-server".to_string(),
sub_name: "bedrock-rs".to_string(),
listeners_info: vec![],
max_player: 100,
}
}
}
34 changes: 34 additions & 0 deletions crates/server/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::error::Error;
use bedrockrs_proto::listener::Listener;
use shipyard::World;
use crate::error::LoginError;
use crate::login::login;

pub mod builder;

pub struct Server {
listeners: Vec<Listener>,
name: String,
sub_name: String,
pub world: World,
}

impl Server {
pub async fn start(&mut self) {
for listener in &mut self.listeners {
listener.start().await.unwrap();
}

self.world.run_default_workload().unwrap()
}

pub async fn stop(&mut self) {
for listener in &mut self.listeners {
listener.stop().await.expect("TODO: panic message");
}
}

pub async fn accept(&mut self) -> Result<(), LoginError> {
todo!()
}
}
14 changes: 7 additions & 7 deletions examples/proto/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
};
use bedrockrs_proto::version::v729::packets::resource_packs_info::ResourcePacksInfoPacket;
use bedrockrs_proto::version::v729::packets::resource_packs_stack::ResourcePacksStackPacket;
use bedrockrs_proto::version::v729::packets::start_game::StartGamePacket;

Check failure on line 14 in examples/proto/server.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

expected identifier, found `;`
use bedrockrs_proto::version::v729::types::base_game_version::BaseGameVersion;
use bedrockrs_proto::version::v729::types::block_pos::BlockPos;
use bedrockrs_proto::version::v729::types::chat_restriction_level::ChatRestrictionLevel;
Expand Down Expand Up @@ -46,7 +46,7 @@
"1.0".to_string(),
100,
10,
SocketAddr::V4(SocketAddrV4::from_str("127.0.0.1:19132").unwrap()),
"127.0.0.1:19132".parse().unwrap(),
false,
)
.await
Expand Down Expand Up @@ -124,12 +124,12 @@
println!("{:#?}", conn.recv::<ProtoHelperV729>().await.unwrap());
println!("ResourcePackClientResponse");

conn.send::<ProtoHelperV729>(&[GamePackets::DisconnectPlayer(DisconnectPlayerPacket {
reason: DisconnectReason::Unknown,
message: Some(String::from("IDK")),
})])
.await
.unwrap();
// conn.send::<ProtoHelperV729>(&[GamePackets::DisconnectPlayer(DisconnectPlayerPacket {
// reason: DisconnectReason::Unknown,
// message: Some(String::from("IDK")),
// })])
// .await
// .unwrap();

// let packet1 = StartGamePacket {
// target_actor_id: ActorUniqueID(609),
Expand Down
Loading