Skip to content

Commit

Permalink
Repare packets
Browse files Browse the repository at this point in the history
With now the state added the the const name of the packet some of them needed to be repare by adding the name and/or the side
  • Loading branch information
Bafbi committed Jan 28, 2024
1 parent 1e6f30b commit ba1d364
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 67 deletions.
4 changes: 2 additions & 2 deletions crates/valence_generated/build/packet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub fn build() -> anyhow::Result<TokenStream> {

for packet in packets {
let stripped_name = packet.name.strip_suffix("Packet").unwrap_or(&packet.name);

let name_ident = ident(stripped_name.to_shouty_snake_case());
let stated_name = format!("{}{}", packet.state, stripped_name);
let name_ident = ident(stated_name.to_shouty_snake_case());
let id = packet.id;

let doc = format!("Side: {}\n\nState: {}", packet.side, packet.state);
Expand Down
1 change: 1 addition & 0 deletions crates/valence_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ pub enum PacketState {
Status,
Login,
Play,
Configuration,
}

#[allow(dead_code)]
Expand Down
15 changes: 9 additions & 6 deletions crates/valence_protocol/src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ pub mod login {
pub use login_success_s2c::LoginSuccessS2c;
}

pub mod configuration {
pub mod features_s2c;
pub use features_s2c::FeaturesS2c;
}

pub mod play {
pub mod advancement_tab_c2s;
pub use advancement_tab_c2s::AdvancementTabC2s;
Expand Down Expand Up @@ -144,8 +149,6 @@ pub mod play {
pub use experience_orb_spawn_s2c::ExperienceOrbSpawnS2c;
pub mod explosion_s2c;
pub use explosion_s2c::ExplosionS2c;
pub mod features_s2c;
pub use features_s2c::FeaturesS2c;
pub mod full_c2s;
pub use full_c2s::FullC2s;
pub mod game_join_s2c;
Expand Down Expand Up @@ -232,8 +235,8 @@ pub mod play {
pub use player_session_c2s::PlayerSessionC2s;
pub mod player_spawn_position_s2c;
pub use player_spawn_position_s2c::PlayerSpawnPositionS2c;
pub mod player_spawn_s2c;
pub use player_spawn_s2c::PlayerSpawnS2c;
// pub mod player_spawn_s2c;
// pub use player_spawn_s2c::PlayerSpawnS2c;
pub mod position_and_on_ground_c2s;
pub use position_and_on_ground_c2s::PositionAndOnGroundC2s;
pub mod profileless_chat_message_s2c;
Expand Down Expand Up @@ -357,8 +360,8 @@ pub mod play {
pub mod status {
pub mod query_ping_c2s;
pub use query_ping_c2s::QueryPingC2s;
pub mod query_pong_s2c;
pub use query_pong_s2c::QueryPongS2c;
// pub mod query_pong_s2c;
// pub use query_pong_s2c::QueryPongS2c;
pub mod query_request_c2s;
pub use query_request_c2s::QueryRequestC2s;
pub mod query_response_s2c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::collections::BTreeSet;

use valence_ident::Ident;

use crate::{Decode, Encode, Packet};
use crate::{Decode, Encode, Packet, PacketState};

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(state= PacketState::Configuration)]
pub struct FeaturesS2c<'a> {
pub features: Cow<'a, BTreeSet<Ident<String>>>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::io::Write;
use valence_ident::Ident;
use valence_text::Text;

use crate::{packet_id, Decode, Encode, ItemStack, Packet, VarInt};
use crate::{Decode, Encode, ItemStack, Packet, VarInt};

pub type AdvancementUpdateS2c<'a> =
GenericAdvancementUpdateS2c<'a, (Ident<Cow<'a, str>>, Advancement<'a, ItemStack>)>;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ADVANCEMENT_UPDATE_S2C)]
#[packet(name = "ADVANCEMENT_UPDATE_S2C")]
pub struct GenericAdvancementUpdateS2c<'a, AM: 'a> {
pub reset: bool,
pub advancement_mapping: Vec<AM>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{packet_id, Decode, Encode, Packet};
use crate::{PacketSide, Decode, Encode, Packet};

Check warning on line 1 in crates/valence_protocol/src/packets/play/bundle_splitter_s2c.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/bundle_splitter_s2c.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BUNDLE_SPLITTER)]
#[packet(name = "BUNDLE_SPLITTER", side = PacketSide::Clientbound)]
pub struct BundleSplitterS2c;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bitfield_struct::bitfield;
use crate::{Decode, Encode, Packet};

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(name = "CLIENT_OPTIONS_C2S")]
pub struct ClientSettingsC2s<'a> {
pub locale: &'a str,
pub view_distance: u8,
Expand Down
3 changes: 1 addition & 2 deletions crates/valence_protocol/src/packets/play/disconnect_s2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::borrow::Cow;

use valence_text::Text;

use crate::{packet_id, Decode, Encode, Packet};
use crate::{Decode, Encode, Packet};

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::DISCONNECT_S2C)]
pub struct DisconnectS2c<'a> {
pub reason: Cow<'a, Text>,
}
4 changes: 2 additions & 2 deletions crates/valence_protocol/src/packets/play/full_c2s.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use valence_math::DVec3;

Check warning on line 1 in crates/valence_protocol/src/packets/play/full_c2s.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/full_c2s.rs

use crate::{packet_id, Decode, Encode, Packet};
use crate::{PacketSide, Decode, Encode, Packet};

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::FULL)]
#[packet(name = "FULL", side = PacketSide::Serverbound)]
pub struct FullC2s {
pub position: DVec3,
pub yaw: f32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{packet_id, Decode, Encode, Packet};
use crate::{PacketSide, Decode, Encode, Packet};

Check warning on line 1 in crates/valence_protocol/src/packets/play/look_and_on_ground_c2s.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/look_and_on_ground_c2s.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOOK_AND_ON_GROUND)]
#[packet(name = "LOOK_AND_ON_GROUND", side = PacketSide::Serverbound)]
pub struct LookAndOnGroundC2s {
pub yaw: f32,
pub pitch: f32,
Expand Down
4 changes: 2 additions & 2 deletions crates/valence_protocol/src/packets/play/move_relative_s2c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{packet_id, Decode, Encode, Packet, VarInt};
use crate::{PacketSide, Decode, Encode, Packet, VarInt};

Check warning on line 1 in crates/valence_protocol/src/packets/play/move_relative_s2c.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/move_relative_s2c.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::MOVE_RELATIVE)]
#[packet(name = "MOVE_RELATIVE", side = PacketSide::Clientbound)]
pub struct MoveRelativeS2c {
pub entity_id: VarInt,
pub delta: [i16; 3],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{packet_id, Decode, Encode, Packet};
use crate::{PacketSide, Decode, Encode, Packet};

Check warning on line 1 in crates/valence_protocol/src/packets/play/on_ground_only_c2s.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/on_ground_only_c2s.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ON_GROUND_ONLY)]
#[packet(name = "ON_GROUND_ONLY", side = PacketSide::Serverbound)]
pub struct OnGroundOnlyC2s {
pub on_ground: bool,
}
1 change: 1 addition & 0 deletions crates/valence_protocol/src/packets/play/play_ping_s2c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Decode, Encode, Packet};

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(name = "COMMON_PING_S2C")]
pub struct PlayPingS2c {
pub id: i32,
}
1 change: 1 addition & 0 deletions crates/valence_protocol/src/packets/play/play_pong_c2s.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Decode, Encode, Packet};

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(name = "COMMON_PONG_C2S")]
pub struct PlayPongC2s {
pub id: i32,
}
52 changes: 27 additions & 25 deletions crates/valence_protocol/src/packets/play/player_spawn_s2c.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
use uuid::Uuid;
use valence_math::DVec3;
// use uuid::Uuid;
// use valence_math::DVec3;

use crate::{ByteAngle, Decode, Encode, Packet, VarInt};
// use crate::{ByteAngle, Decode, Encode, Packet, VarInt};

/// This packet is sent by the server when a player comes into visible range,
/// not when a player joins.
///
/// This packet must be sent after the Player Info Update packet that adds the
/// player data for the client to use when spawning a player. If the Player Info
/// for the player spawned by this packet is not present when this packet
/// arrives, Notchian clients will not spawn the player entity. The Player Info
/// packet includes skin/cape data.
///
/// Servers can, however, safely spawn player entities for players not in
/// visible range. The client appears to handle it correctly.
///
/// wiki : [Spawn Player](https://wiki.vg/Protocol#Spawn_Player)
#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
pub struct PlayerSpawnS2c {
/// A unique integer ID mostly used in the protocol to identify the player.
pub entity_id: VarInt,
pub player_uuid: Uuid,
pub position: DVec3,
pub yaw: ByteAngle,
pub pitch: ByteAngle,
}
// /// This packet is sent by the server when a player comes into visible range,
// /// not when a player joins.
// ///
// /// This packet must be sent after the Player Info Update packet that adds the
// /// player data for the client to use when spawning a player. If the Player Info
// /// for the player spawned by this packet is not present when this packet
// /// arrives, Notchian clients will not spawn the player entity. The Player Info
// /// packet includes skin/cape data.
// ///
// /// Servers can, however, safely spawn player entities for players not in
// /// visible range. The client appears to handle it correctly.
// ///
// /// wiki : [Spawn Player](https://wiki.vg/Protocol#Spawn_Player)
// #[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
// pub struct PlayerSpawnS2c {
// /// A unique integer ID mostly used in the protocol to identify the player.
// pub entity_id: VarInt,
// pub player_uuid: Uuid,
// pub position: DVec3,
// pub yaw: ByteAngle,
// pub pitch: ByteAngle,
// }
//
/// Seems to be deprecated, need to be verify and remove if true
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use valence_math::DVec3;

Check warning on line 1 in crates/valence_protocol/src/packets/play/position_and_on_ground_c2s.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/position_and_on_ground_c2s.rs

use crate::{packet_id, Decode, Encode, Packet};
use crate::{PacketSide, Decode, Encode, Packet};

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::POSITION_AND_ON_GROUND)]
#[packet(name = "POSITION_AND_ON_GROUND", side = PacketSide::Serverbound)]
pub struct PositionAndOnGroundC2s {
pub position: DVec3,
pub on_ground: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{packet_id, ByteAngle, Decode, Encode, Packet, VarInt};
use crate::{PacketSide, ByteAngle, Decode, Encode, Packet, VarInt};

Check warning on line 1 in crates/valence_protocol/src/packets/play/rotate_and_move_relative_s2c.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/rotate_and_move_relative_s2c.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ROTATE_AND_MOVE_RELATIVE)]
#[packet(name = "ROTATE_AND_MOVE_RELATIVE", side = PacketSide::Clientbound)]
pub struct RotateAndMoveRelativeS2c {
pub entity_id: VarInt,
pub delta: [i16; 3],
Expand Down
4 changes: 2 additions & 2 deletions crates/valence_protocol/src/packets/play/rotate_s2c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{packet_id, ByteAngle, Decode, Encode, Packet, VarInt};
use crate::{PacketSide, ByteAngle, Decode, Encode, Packet, VarInt};

Check warning on line 1 in crates/valence_protocol/src/packets/play/rotate_s2c.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol/src/packets/play/rotate_s2c.rs

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ROTATE)]
#[packet(name = "ROTATE", side=PacketSide::Clientbound)]
pub struct RotateS2c {
pub entity_id: VarInt,
pub yaw: ByteAngle,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Decode, Encode, Packet, VarInt};

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(name = "SCOREBOARD_SCORE_UPDATE_S2C")]
pub struct ScoreboardPlayerUpdateS2c<'a> {
pub entity_name: &'a str,
pub action: ScoreboardPlayerUpdateAction<'a>,
Expand Down
14 changes: 8 additions & 6 deletions crates/valence_protocol/src/packets/status/query_pong_s2c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{Decode, Encode, Packet, PacketState};
// use crate::{Decode, Encode, Packet, PacketState};

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(state = PacketState::Status)]
pub struct QueryPongS2c {
pub payload: u64,
}
// #[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
// #[packet(state = PacketState::Status)]
// pub struct QueryPongS2c {
// pub payload: u64,
// }
//
/// Deprecated I would guess
24 changes: 17 additions & 7 deletions crates/valence_protocol_macros/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ pub(super) fn derive_packet(item: TokenStream) -> Result<TokenStream> {

let name = input.ident.clone();

Check warning on line 14 in crates/valence_protocol_macros/src/packet.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol_macros/src/packet.rs

let state = packet_attr
.state
.unwrap_or_else(|| parse_quote!(::valence_protocol::PacketState::Play));

let state_str = match state {
Expr::Path(ref path) => path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(Error::new(state.span(), "invalid state")),
};

Check warning on line 24 in crates/valence_protocol_macros/src/packet.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/valence/valence/crates/valence_protocol_macros/src/packet.rs
let name_str = if let Some(attr_name) = packet_attr.name {
attr_name.value()
} else {
name.to_string()
} else {
name.to_string()
};

let name_state_str = format!("{}{}", state_str ,name_str);

// debug name_str
println!("name_str: {}", name_state_str);

let packet_id: Expr = match packet_attr.id {
Some(expr) => expr,
None => match syn::parse_str::<Ident>(&name_str.to_shouty_snake_case()) {
None => match syn::parse_str::<Ident>(&name_state_str.to_shouty_snake_case()) {
Ok(ident) => parse_quote!(::valence_protocol::packet_id::#ident),
Err(_) => {
return Err(Error::new(
Expand Down Expand Up @@ -49,10 +63,6 @@ pub(super) fn derive_packet(item: TokenStream) -> Result<TokenStream> {
));
};

let state = packet_attr
.state
.unwrap_or_else(|| parse_quote!(::valence_protocol::PacketState::Play));

Ok(quote! {
impl #impl_generics ::valence_protocol::__private::Packet for #name #ty_generics
#where_clause
Expand Down

0 comments on commit ba1d364

Please sign in to comment.