diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 149 |
1 files changed, 3 insertions, 146 deletions
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"); |