aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/packets/status
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-13 00:07:21 -0600
committermat <github@matdoes.dev>2021-12-13 00:07:21 -0600
commitc96ae8fce4e53ea9fad111ac7f479f2fa33ef859 (patch)
tree2d35061fa712464b225317f56a806d0dd269ca82 /minecraft-protocol/src/packets/status
parent2c3bf3b79e133acd01580144771a7cf238ecc4ee (diff)
downloadazalea-drasl-c96ae8fce4e53ea9fad111ac7f479f2fa33ef859.tar.xz
start implementing joining servers
Diffstat (limited to 'minecraft-protocol/src/packets/status')
-rw-r--r--minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs12
-rw-r--r--minecraft-protocol/src/packets/status/mod.rs78
-rw-r--r--minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs14
3 files changed, 94 insertions, 10 deletions
diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
index 99bef586..30b1403c 100644
--- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
+++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs
@@ -9,9 +9,11 @@ use crate::{
packets::{Packet, PacketTrait},
};
+use super::StatusPacket;
+
#[derive(Clone, Debug, Deserialize)]
pub struct Version {
- pub name: String,
+ pub name: Component,
pub protocol: u32,
}
@@ -31,14 +33,16 @@ pub struct Players {
// the entire packet is just json, which is why it has deserialize
#[derive(Clone, Debug, Deserialize)]
pub struct ClientboundStatusResponsePacket {
- pub version: Version,
pub description: Component,
+ pub favicon: Option<String>,
+ pub players: Players,
+ pub version: Version,
}
#[async_trait]
impl PacketTrait for ClientboundStatusResponsePacket {
- fn get(self) -> Packet {
- Packet::ClientboundStatusResponsePacket(self)
+ fn get(self) -> StatusPacket {
+ StatusPacket::ClientboundStatusResponsePacket(self)
}
fn write(&self, _buf: &mut Vec<u8>) {}
diff --git a/minecraft-protocol/src/packets/status/mod.rs b/minecraft-protocol/src/packets/status/mod.rs
index efc03631..7e327ca7 100644
--- a/minecraft-protocol/src/packets/status/mod.rs
+++ b/minecraft-protocol/src/packets/status/mod.rs
@@ -1,2 +1,80 @@
pub mod clientbound_status_response_packet;
pub mod serverbound_status_request_packet;
+
+use async_trait::async_trait;
+use tokio::io::BufReader;
+
+use crate::connect::PacketFlow;
+
+use super::{ConnectionProtocol, PacketTrait, ProtocolPacket};
+
+#[derive(Clone, Debug)]
+pub enum StatusPacket {
+ ServerboundStatusRequestPacket(
+ serverbound_status_request_packet::ServerboundStatusRequestPacket,
+ ),
+ ClientboundStatusResponsePacket(
+ clientbound_status_response_packet::ClientboundStatusResponsePacket,
+ ),
+}
+
+// #[async_trait]
+// impl ProtocolPacket for StatusPacket {
+impl StatusPacket {
+ fn get_inner(self) -> impl PacketTrait {
+ match self {
+ StatusPacket::ServerboundStatusRequestPacket(packet) => packet,
+ StatusPacket::ClientboundStatusResponsePacket(packet) => packet,
+ }
+ }
+ // fn get_inner(&self) -> StatusPacket {
+ // match self {
+ // StatusPacket::ServerboundStatusRequestPacket(packet) => packet,
+ // StatusPacket::ClientboundStatusResponsePacket(packet) => packet,
+ // }
+ // }
+
+ fn id(&self) -> u32 {
+ match self {
+ StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00,
+ StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00,
+ }
+ }
+
+ fn write(&self, buf: &mut Vec<u8>) {
+ match self {
+ StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf),
+ StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf),
+ }
+ }
+
+ /// Read a packet by its id, ConnectionProtocol, and flow
+ async fn read<
+ T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send,
+ P: ProtocolPacket,
+ >(
+ id: u32,
+ flow: &PacketFlow,
+ buf: &mut BufReader<T>,
+ ) -> Result<P, String>
+ where
+ Self: Sized,
+ {
+ match flow {
+ PacketFlow::ServerToClient => match id {
+ 0x00 => Ok(
+ clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf)
+ .await?,
+ ),
+ _ => Err(format!("Unknown ServerToClient status packet id: {}", id)),
+ },
+ PacketFlow::ClientToServer => match id {
+ 0x00 => Ok(
+ serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf)
+ .await?,
+ ),
+ _ => Err(format!("Unknown ClientToServer status packet id: {}", id)),
+ },
+ }
+ }
+}
diff --git a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs
index 56799677..066131cc 100644
--- a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs
+++ b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs
@@ -2,19 +2,21 @@ use async_trait::async_trait;
use std::hash::Hash;
use tokio::io::BufReader;
-use crate::{
- packets::{Packet, PacketTrait},
-};
+use crate::packets::{Packet, PacketTrait, ProtocolPacket};
+
+use super::StatusPacket;
#[derive(Hash, Clone, Debug)]
pub struct ServerboundStatusRequestPacket {}
#[async_trait]
impl PacketTrait for ServerboundStatusRequestPacket {
- fn get(self) -> Packet {
- Packet::ServerboundStatusRequestPacket(self)
+ fn get(self) -> StatusPacket {
+ StatusPacket::ServerboundStatusRequestPacket(self)
+ }
+ fn write(&self, _buf: &mut Vec<u8>) {
+ panic!("ServerboundStatusRequestPacket::write not implemented")
}
- fn write(&self, _buf: &mut Vec<u8>) {}
async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
_buf: &mut BufReader<T>,