aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-24 17:37:57 -0500
committermat <github@matdoes.dev>2022-04-24 17:37:57 -0500
commit3e507f0db4020eaf406ba69aae3d4dc1301d29ac (patch)
treeca6c127c9db6dfd14511e98944fc031fe5f1e43a /azalea-protocol/src/packets/game
parent9f576c5600ba9a244bc0d433bb7de174284066a2 (diff)
parentb7641ff308aab7840d2a2253ae50f8ee496b2a97 (diff)
downloadazalea-drasl-3e507f0db4020eaf406ba69aae3d4dc1301d29ac.tar.xz
Merge branch 'main' into auth
Diffstat (limited to 'azalea-protocol/src/packets/game')
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs8
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs8
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs96
-rw-r--r--azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs9
-rwxr-xr-x[-rw-r--r--]azalea-protocol/src/packets/game/clientbound_login_packet.rs107
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs57
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs7
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_update_tags_packet.rs105
-rwxr-xr-xazalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs9
-rwxr-xr-x[-rw-r--r--]azalea-protocol/src/packets/game/mod.rs65
10 files changed, 327 insertions, 144 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs
new file mode 100755
index 00000000..e12cfff3
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs
@@ -0,0 +1,8 @@
+use azalea_core::difficulty::Difficulty;
+use packet_macros::GamePacket;
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundChangeDifficultyPacket {
+ pub difficulty: Difficulty,
+ pub locked: bool,
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs
new file mode 100755
index 00000000..134a3109
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs
@@ -0,0 +1,8 @@
+use azalea_core::resource_location::ResourceLocation;
+use packet_macros::GamePacket;
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundCustomPayloadPacket {
+ pub identifier: ResourceLocation,
+ pub data: Vec<u8>,
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs
new file mode 100755
index 00000000..1403630d
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs
@@ -0,0 +1,96 @@
+use std::hash::Hash;
+
+use async_trait::async_trait;
+use tokio::io::AsyncRead;
+
+use crate::mc_buf::{McBufReadable, Readable};
+
+use super::GamePacket;
+
+#[derive(Hash, Clone, Debug)]
+pub struct ClientboundDeclareCommandsPacket {
+ pub entries: Vec<BrigadierNodeStub>,
+ pub root_index: i32,
+}
+
+impl ClientboundDeclareCommandsPacket {
+ pub fn get(self) -> GamePacket {
+ GamePacket::ClientboundDeclareCommandsPacket(self)
+ }
+
+ pub fn write(&self, _buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ panic!("ClientboundDeclareCommandsPacket::write not implemented")
+ }
+
+ pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
+ buf: &mut T,
+ ) -> Result<GamePacket, String> {
+ let node_count = buf.read_varint().await?;
+ println!("node_count: {}", node_count);
+ let mut nodes = Vec::with_capacity(node_count as usize);
+ for _ in 0..node_count {
+ let node = BrigadierNodeStub::read_into(buf).await?;
+ nodes.push(node);
+ }
+ let root_index = buf.read_varint().await?;
+ Ok(GamePacket::ClientboundDeclareCommandsPacket(
+ ClientboundDeclareCommandsPacket {
+ entries: nodes,
+ root_index,
+ },
+ ))
+ }
+}
+
+#[derive(Hash, Debug, Clone)]
+pub struct BrigadierNodeStub {}
+
+// azalea_brigadier::tree::CommandNode
+#[async_trait]
+impl McBufReadable for BrigadierNodeStub {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ let flags = u8::read_into(buf).await?;
+
+ let node_type = flags & 0x03;
+ let is_executable = flags & 0x04 != 0;
+ let has_redirect = flags & 0x08 != 0;
+ let has_suggestions_type = flags & 0x10 != 0;
+ println!("flags: {}, node_type: {}, is_executable: {}, has_redirect: {}, has_suggestions_type: {}", flags, node_type, is_executable, has_redirect, has_suggestions_type);
+
+ let children = buf.read_int_id_list().await?;
+ println!("children: {:?}", children);
+ let redirect_node = if has_redirect {
+ buf.read_varint().await?
+ } else {
+ 0
+ };
+ println!("redirect_node: {}", redirect_node);
+
+ if node_type == 2 {
+ let name = buf.read_utf().await?;
+ println!("name: {}", name);
+
+ let resource_location = if has_suggestions_type {
+ Some(buf.read_resource_location().await?)
+ } else {
+ None
+ };
+ println!(
+ "node_type=2, flags={}, name={}, resource_location={:?}",
+ flags, name, resource_location
+ );
+ return Ok(BrigadierNodeStub {});
+ }
+ if node_type == 1 {
+ let name = buf.read_utf().await?;
+ println!("node_type=1, flags={}, name={}", flags, name);
+ return Ok(BrigadierNodeStub {});
+ }
+ println!("node_type={}, flags={}", node_type, flags);
+ Ok(BrigadierNodeStub {})
+ // return Err("Unknown node type".to_string());
+ }
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs
new file mode 100644
index 00000000..74f5f72e
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs
@@ -0,0 +1,9 @@
+use azalea_chat::component::Component;
+use azalea_core::resource_location::ResourceLocation;
+use packet_macros::GamePacket;
+use serde::Deserialize;
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundDisconnectPacket {
+ pub reason: Component,
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs
index 9043fa1a..57869202 100644..100755
--- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs
+++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs
@@ -1,26 +1,8 @@
-use super::GamePacket;
-use crate::mc_buf::{Readable, Writable};
use azalea_core::{game_type::GameType, resource_location::ResourceLocation};
+use packet_macros::GamePacket;
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, GamePacket)]
pub struct ClientboundLoginPacket {
- // private final int playerId;
- // private final boolean hardcore;
- // private final GameType gameType;
- // @Nullable
- // private final GameType previousGameType;
- // private final Set<ResourceKey<Level>> levels;
- // private final RegistryAccess.RegistryHolder registryHolder;
- // private final DimensionType dimensionType;
- // private final ResourceKey<Level> dimension;
- // private final long seed;
- // private final int maxPlayers;
- // private final int chunkRadius;
- // private final int simulationDistance;
- // private final boolean reducedDebugInfo;
- // private final boolean showDeathScreen;
- // private final boolean isDebug;
- // private final boolean isFlat;
pub player_id: i32,
pub hardcore: bool,
pub game_type: GameType,
@@ -30,93 +12,14 @@ pub struct ClientboundLoginPacket {
pub dimension_type: azalea_nbt::Tag,
pub dimension: ResourceLocation,
pub seed: i64,
+ #[varint]
pub max_players: i32,
+ #[varint]
pub chunk_radius: i32,
+ #[varint]
pub simulation_distance: i32,
pub reduced_debug_info: bool,
pub show_death_screen: bool,
pub is_debug: bool,
pub is_flat: bool,
}
-
-impl ClientboundLoginPacket {
- pub fn get(self) -> GamePacket {
- GamePacket::ClientboundLoginPacket(self)
- }
-
- pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
- buf.write_int(self.player_id)?;
- buf.write_boolean(self.hardcore)?;
- buf.write_byte(self.game_type.to_id())?;
- buf.write_byte(GameType::to_optional_id(&self.previous_game_type) as u8)?;
- buf.write_list(&self.levels, |buf, resource_location| {
- buf.write_resource_location(resource_location)
- })?;
- buf.write_nbt(&self.registry_holder)?;
- buf.write_nbt(&self.dimension_type)?;
- buf.write_resource_location(&self.dimension)?;
- buf.write_long(self.seed)?;
- buf.write_varint(self.max_players)?;
- buf.write_varint(self.chunk_radius)?;
- buf.write_varint(self.simulation_distance)?;
- buf.write_boolean(self.reduced_debug_info)?;
- buf.write_boolean(self.show_death_screen)?;
- buf.write_boolean(self.is_debug)?;
- buf.write_boolean(self.is_flat)?;
- Ok(())
- }
-
- pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
- buf: &mut T,
- ) -> Result<GamePacket, String> {
- let player_id = buf.read_int().await?;
- let hardcore = buf.read_boolean().await?;
- let game_type = GameType::from_id(buf.read_byte().await?)?;
- let previous_game_type = GameType::from_optional_id(buf.read_byte().await? as i8)?;
-
- let mut levels = Vec::new();
- let length = buf.read_varint().await?;
- for _ in 0..length {
- levels.push(buf.read_resource_location().await?);
- }
-
- // println!("about to read nbt");
- // // read all the bytes into a buffer, print it, and panic
- // let mut registry_holder_buf = Vec::new();
- // buf.read_to_end(&mut registry_holder_buf).await.unwrap();
- // println!("{:?}", String::from_utf8_lossy(&registry_holder_buf));
- // panic!("");
-
- let registry_holder = buf.read_nbt().await?;
- let dimension_type = buf.read_nbt().await?;
- let dimension = buf.read_resource_location().await?;
- let seed = buf.read_long().await?;
- let max_players = buf.read_varint().await?;
- let chunk_radius = buf.read_varint().await?;
- let simulation_distance = buf.read_varint().await?;
- let reduced_debug_info = buf.read_boolean().await?;
- let show_death_screen = buf.read_boolean().await?;
- let is_debug = buf.read_boolean().await?;
- let is_flat = buf.read_boolean().await?;
-
- Ok(ClientboundLoginPacket {
- player_id,
- hardcore,
- game_type,
- previous_game_type,
- levels,
- registry_holder,
- dimension_type,
- dimension,
- seed,
- max_players,
- chunk_radius,
- simulation_distance,
- reduced_debug_info,
- show_death_screen,
- is_debug,
- is_flat,
- }
- .get())
- }
-}
diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs
new file mode 100755
index 00000000..f4f528cf
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs
@@ -0,0 +1,57 @@
+// i don't know the actual name of this packet, i couldn't find it in the source code
+
+use crate::mc_buf::{McBufReadable, McBufWritable, Readable};
+use async_trait::async_trait;
+use packet_macros::GamePacket;
+use tokio::io::AsyncRead;
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundPlayerAbilitiesPacket {
+ pub flags: PlayerAbilitiesFlags,
+ pub flying_speed: f32,
+ /// Used for the fov
+ pub walking_speed: f32,
+}
+
+#[derive(Clone, Debug)]
+pub struct PlayerAbilitiesFlags {
+ pub invulnerable: bool,
+ pub flying: bool,
+ pub can_fly: bool,
+ pub instant_break: bool,
+}
+
+#[async_trait]
+impl McBufReadable for PlayerAbilitiesFlags {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ let byte = buf.read_byte().await?;
+ Ok(PlayerAbilitiesFlags {
+ invulnerable: byte & 1 != 0,
+ flying: byte & 2 != 0,
+ can_fly: byte & 4 != 0,
+ instant_break: byte & 8 != 0,
+ })
+ }
+}
+
+impl McBufWritable for PlayerAbilitiesFlags {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ let mut byte = 0;
+ if self.invulnerable {
+ byte = byte | 1;
+ }
+ if self.flying {
+ byte = byte | 2;
+ }
+ if self.can_fly {
+ byte = byte | 4;
+ }
+ if self.instant_break {
+ byte = byte | 8;
+ }
+ u8::write_into(&byte, buf)
+ }
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs
new file mode 100755
index 00000000..4f0bf575
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs
@@ -0,0 +1,7 @@
+use packet_macros::GamePacket;
+
+/// Sent to change the player's slot selection.
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundSetCarriedItemPacket {
+ pub slot: u8,
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs
new file mode 100755
index 00000000..a2f17370
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs
@@ -0,0 +1,105 @@
+use std::collections::HashMap;
+
+use async_trait::async_trait;
+use azalea_core::resource_location::ResourceLocation;
+use packet_macros::GamePacket;
+use tokio::io::AsyncRead;
+
+use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable};
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundUpdateTagsPacket {
+ pub tags: HashMap<ResourceLocation, Vec<Tags>>,
+}
+
+#[derive(Clone, Debug)]
+pub struct Tags {
+ pub name: ResourceLocation,
+ pub elements: Vec<i32>,
+}
+
+#[async_trait]
+impl McBufReadable for HashMap<ResourceLocation, Vec<Tags>> {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ println!("reading tags!");
+ let length = buf.read_varint().await? as usize;
+ println!("length: {}", length);
+ let mut data = HashMap::with_capacity(length);
+ for _ in 0..length {
+ let tag_type = buf.read_resource_location().await?;
+ println!("read tag type {}", tag_type);
+ let tags_count = buf.read_varint().await? as usize;
+ let mut tags_vec = Vec::with_capacity(tags_count);
+ for _ in 0..tags_count {
+ let tags = Tags::read_into(buf).await?;
+ tags_vec.push(tags);
+ }
+ println!("tags: {} {:?}", tag_type, tags_vec);
+ data.insert(tag_type, tags_vec);
+ }
+ Ok(data)
+ }
+}
+
+impl McBufWritable for HashMap<ResourceLocation, Vec<Tags>> {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_varint(self.len() as i32)?;
+ for (k, v) in self {
+ k.write_into(buf)?;
+ v.write_into(buf)?;
+ }
+ Ok(())
+ }
+}
+
+#[async_trait]
+impl McBufReadable for Vec<Tags> {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ let tags_count = buf.read_varint().await? as usize;
+ let mut tags_vec = Vec::with_capacity(tags_count);
+ for _ in 0..tags_count {
+ let tags = Tags::read_into(buf).await?;
+ tags_vec.push(tags);
+ }
+ Ok(tags_vec)
+ }
+}
+
+impl McBufWritable for Vec<Tags> {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_varint(self.len() as i32)?;
+ for tag in self {
+ tag.write_into(buf)?;
+ }
+ Ok(())
+ }
+}
+
+#[async_trait]
+impl McBufReadable for Tags {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ println!("reading tags.");
+ let name = buf.read_resource_location().await?;
+ println!("tags name: {}", name);
+ let elements = buf.read_int_id_list().await?;
+ println!("elements: {:?}", elements);
+ Ok(Tags { name, elements })
+ }
+}
+
+impl McBufWritable for Tags {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ self.name.write_into(buf)?;
+ buf.write_int_id_list(&self.elements)?;
+ Ok(())
+ }
+}
diff --git a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs
new file mode 100755
index 00000000..8301c089
--- /dev/null
+++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs
@@ -0,0 +1,9 @@
+// i don't know the actual name of this packet, i couldn't find it in the source code
+
+use packet_macros::GamePacket;
+
+#[derive(Clone, Debug, GamePacket)]
+pub struct ClientboundUpdateViewDistancePacket {
+ #[varint]
+ pub view_distance: i32,
+}
diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs
index 00fa1d75..dde3f753 100644..100755
--- a/azalea-protocol/src/packets/game/mod.rs
+++ b/azalea-protocol/src/packets/game/mod.rs
@@ -1,46 +1,27 @@
+pub mod clientbound_change_difficulty_packet;
+pub mod clientbound_custom_payload_packet;
+pub mod clientbound_declare_commands_packet;
+pub mod clientbound_disconnect_packet;
pub mod clientbound_login_packet;
+pub mod clientbound_player_abilities_packet;
+pub mod clientbound_set_carried_item_packet;
+pub mod clientbound_update_tags_packet;
+pub mod clientbound_update_view_distance_packet;
-use super::ProtocolPacket;
-use crate::connect::PacketFlow;
-use async_trait::async_trait;
+use packet_macros::declare_state_packets;
-#[derive(Clone, Debug)]
-pub enum GamePacket
-where
- Self: Sized,
-{
- ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket),
-}
-
-#[async_trait]
-impl ProtocolPacket for GamePacket {
- fn id(&self) -> u32 {
- match self {
- GamePacket::ClientboundLoginPacket(_packet) => 0x00,
- }
- }
-
- fn write(&self, _buf: &mut Vec<u8>) {}
-
- /// Read a packet by its id, ConnectionProtocol, and flow
- async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
- id: u32,
- flow: &PacketFlow,
- buf: &mut T,
- ) -> Result<GamePacket, String>
- where
- Self: Sized,
- {
- Ok(match flow {
- PacketFlow::ServerToClient => match id {
- 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?,
-
- _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)),
- },
- PacketFlow::ClientToServer => match id {
- // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?,
- _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)),
- },
- })
+declare_state_packets!(
+ GamePacket,
+ Serverbound => {},
+ Clientbound => {
+ 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket,
+ 0x12: clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket,
+ 0x1a: clientbound_disconnect_packet::ClientboundDisconnectPacket,
+ 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket,
+ 0x26: clientbound_login_packet::ClientboundLoginPacket,
+ 0x32: clientbound_player_abilities_packet::ClientboundPlayerAbilitiesPacket,
+ 0x48: clientbound_set_carried_item_packet::ClientboundSetCarriedItemPacket,
+ 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket,
+ 0x67: clientbound_update_tags_packet::ClientboundUpdateTagsPacket
}
-}
+);