diff options
author | nea <nea@nea.moe> | 2023-03-27 20:06:14 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-03-27 20:06:14 +0200 |
commit | b1c4131231227a7780e1c1940b399983cdd7f552 (patch) | |
tree | 6f1b4d3365baa008bb08ef76bf8f9cf95d18f63f /src | |
parent | 7392a99613f599148a4311fd3683133b18056570 (diff) | |
download | mgasi-b1c4131231227a7780e1c1940b399983cdd7f552.tar.gz mgasi-b1c4131231227a7780e1c1940b399983cdd7f552.tar.bz2 mgasi-b1c4131231227a7780e1c1940b399983cdd7f552.zip |
Split
Diffstat (limited to 'src')
-rw-r--r-- | src/connect.rs | 78 | ||||
-rw-r--r-- | src/main.rs | 149 | ||||
-rw-r--r-- | src/nbtdsl.rs | 82 |
3 files changed, 163 insertions, 146 deletions
diff --git a/src/connect.rs b/src/connect.rs new file mode 100644 index 0000000..82590fb --- /dev/null +++ b/src/connect.rs @@ -0,0 +1,78 @@ +use std::net::TcpStream; + +use anyhow::Result; +use craftio_rs::{CraftSyncReader, CraftSyncWriter, CraftTcpConnection}; +use mcproto_rs::protocol::HasPacketKind; +use mcproto_rs::v1_19_3::{Packet761, Packet761Kind, RawPacket761}; + +#[macro_export] +macro_rules! await_packet { + ($packet_type:ident, $conn:expr) => { + assert_packet!( + $packet_type, + $conn.wait_for_packet(Packet761Kind::$packet_type).await? + ) + }; +} + +#[macro_export] +macro_rules! assert_packet { + ($packet_type:ident, $obj:expr) => { + if let Packet761::$packet_type(packet_data) = $obj { + packet_data + } else { + panic!("Expected packet of type {}", stringify!($packet_type)) + } + }; + ($packet_type:ident, $conn:expr, $errmgs:literal) => { + assert_packet!( + $packet_type, + $conn + .read_next_packet() + .await? + .ok_or(anyhow::anyhow!("Missing packet"))? + ) + }; +} + +pub struct MinecraftClient { + pub connection: CraftTcpConnection, +} + +impl MinecraftClient { + pub fn new(connection: CraftTcpConnection) -> Self { + Self { connection } + } + + pub fn from_stream(stream: TcpStream) -> Result<Self> { + Ok(Self { + connection: CraftTcpConnection::from_std( + stream, + mcproto_rs::protocol::PacketDirection::ServerBound, + )?, + }) + } + pub async fn read_next_packet(&mut self) -> Result<Option<Packet761>> { + if let Some(raw) = self.connection.read_packet::<RawPacket761>()? { + println!("Client -> Server: {:?}", raw); + Ok(Some(raw)) + } else { + Ok(None) + } + } + pub async fn send_packet(&mut self, packet: Packet761) -> Result<()> { + println!("Server -> Client: {:?}", packet); + self.connection.write_packet(packet)?; + Ok(()) + } + pub async fn wait_for_packet(&mut self, packet_kind: Packet761Kind) -> Result<Packet761> { + loop { + if let Some(packet) = self.read_next_packet().await? { + if packet.kind() == packet_kind { + return Ok(packet); + } + println!("Skipping packet {:?}", packet); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index d81b0a6..088f27f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,155 +12,12 @@ use mcproto_rs::uuid::UUID4; use mcproto_rs::v1_19_3::{BitSet, BlobArray, BookSettings, ChunkDataAndUpdateLightSpec, ChunkDataSpec, ChunkSection, CommandNode, CommandNodeSpec, CommandsSpec, EntityEventSpec, InitializeWorldBorderSpec, KeepAliveClientBoundSpec, LightDataSpec, PalettedContainer, PluginMessageSpec, RecipeBookAction, RecipeBookInitSpec, SetCenterChunkSpec, SetContainerContentSpec, SetDefaultSpawnPositionSpec, SynchronizePlayerPositionSpec, TagType, TypedTagList, UpdateRecipeBookSpec}; use mcproto_rs::v1_19_3::{GameMode, HandshakeNextState, LoginPlaySpec, LoginSuccessSpec, Packet761, Packet761Kind, PingRequestSpec, PingResponseSpec, PreviousGameMode, RawPacket761, SetHeldItemClientSpec, StatusResponseSpec, UpdateRecipesSpec, UpdateTagsSpec}; use tokio; +use crate::connect::MinecraftClient; -pub struct MinecraftClient { - connection: CraftTcpConnection, -} - -impl MinecraftClient { - pub fn new(connection: CraftTcpConnection) -> Self { - Self { connection } - } - - pub fn from_stream(stream: TcpStream) -> Result<Self> { - Ok(Self { - connection: CraftTcpConnection::from_std( - stream, - mcproto_rs::protocol::PacketDirection::ServerBound, - )?, - }) - } - pub async fn read_next_packet(&mut self) -> Result<Option<Packet761>> { - if let Some(raw) = self.connection.read_packet::<RawPacket761>()? { - println!("Client -> Server: {:?}", raw); - Ok(Some(raw)) - } else { - Ok(None) - } - } - pub async fn send_packet(&mut self, packet: Packet761) -> Result<()> { - println!("Server -> Client: {:?}", packet); - self.connection.write_packet(packet)?; - Ok(()) - } - pub async fn wait_for_packet(&mut self, packet_kind: Packet761Kind) -> Result<Packet761> { - loop { - if let Some(packet) = self.read_next_packet().await? { - if packet.kind() == packet_kind { - return Ok(packet); - } - println!("Skipping packet {:?}", packet); - } - } - } -} - -macro_rules! await_packet { - ($packet_type:ident, $conn:expr) => { - assert_packet!( - $packet_type, - $conn.wait_for_packet(Packet761Kind::$packet_type).await? - ) - }; -} - -macro_rules! assert_packet { - ($packet_type:ident, $obj:expr) => { - if let Packet761::$packet_type(packet_data) = $obj { - packet_data - } else { - panic!("Expected packet of type {}", stringify!($packet_type)) - } - }; - ($packet_type:ident, $conn:expr, $errmgs:literal) => { - assert_packet!( - $packet_type, - $conn - .read_next_packet() - .await? - .ok_or(anyhow::anyhow!("Missing packet"))? - ) - }; -} - -trait ShittyToNbt { - fn to_nbt(self) -> Tag; -} -impl ShittyToNbt for String { - fn to_nbt(self) -> Tag { - Tag::String(self) - } -} - -impl ShittyToNbt for &str { - fn to_nbt(self) -> Tag { - Tag::String(self.into()) - } -} - -impl ShittyToNbt for i32 { - fn to_nbt(self) -> Tag { - Tag::Int(self) - } -} - -impl ShittyToNbt for i64 { - fn to_nbt(self) -> Tag { - Tag::Long(self) - } -} - -impl ShittyToNbt for bool { - fn to_nbt(self) -> Tag { - Tag::Byte(if self { 1 } else { 0 }) - } -} - -impl ShittyToNbt for Tag { - fn to_nbt(self) -> Tag { - self - } -} - -impl ShittyToNbt for f32 { - fn to_nbt(self) -> Tag { - Tag::Float(self) - } -} - -impl ShittyToNbt for f64 { - fn to_nbt(self) -> Tag { - Tag::Double(self) - } -} +pub mod connect; +pub mod nbtdsl; -macro_rules! nbt { - ( {$($name:literal :: $value:tt),* $(,)? } ) => { - Tag::Compound(vec![ - $( - nbt!($value).with_name($name) - ),* - ]) - }; - { $($name:literal :: $value:tt),* $(,)? } => { - Tag::Compound(vec![ - $( - nbt!($value).with_name($name) - ),* - ]) - }; - ([ $($value:tt),* $(,)? ]) => { - Tag::List(vec![ - $( - nbt!($value) - ),* - ]) - }; - (($e:expr)) => { - ShittyToNbt::to_nbt($e) - }; -} #[tokio::main] async fn main() -> Result<()> { println!("Starting server"); diff --git a/src/nbtdsl.rs b/src/nbtdsl.rs new file mode 100644 index 0000000..bfa9090 --- /dev/null +++ b/src/nbtdsl.rs @@ -0,0 +1,82 @@ +use mcproto_rs::nbt::Tag; + + +pub trait ShittyToNbt { + fn to_nbt(self) -> Tag; +} + +impl ShittyToNbt for String { + fn to_nbt(self) -> Tag { + Tag::String(self) + } +} + +impl ShittyToNbt for &str { + fn to_nbt(self) -> Tag { + Tag::String(self.into()) + } +} + +impl ShittyToNbt for i32 { + fn to_nbt(self) -> Tag { + Tag::Int(self) + } +} + +impl ShittyToNbt for i64 { + fn to_nbt(self) -> Tag { + Tag::Long(self) + } +} + +impl ShittyToNbt for bool { + fn to_nbt(self) -> Tag { + Tag::Byte(if self { 1 } else { 0 }) + } +} + +impl ShittyToNbt for Tag { + fn to_nbt(self) -> Tag { + self + } +} + +impl ShittyToNbt for f32 { + fn to_nbt(self) -> Tag { + Tag::Float(self) + } +} + +impl ShittyToNbt for f64 { + fn to_nbt(self) -> Tag { + Tag::Double(self) + } +} + +#[macro_export] +macro_rules! nbt { + ( {$($name:literal :: $value:tt),* $(,)? } ) => { + Tag::Compound(vec![ + $( + nbt!($value).with_name($name) + ),* + ]) + }; + { $($name:literal :: $value:tt),* $(,)? } => { + Tag::Compound(vec![ + $( + nbt!($value).with_name($name) + ),* + ]) + }; + ([ $($value:tt),* $(,)? ]) => { + Tag::List(vec![ + $( + nbt!($value) + ),* + ]) + }; + (($e:expr)) => { + $crate::nbtdsl::ShittyToNbt::to_nbt($e) + }; +}
\ No newline at end of file |