Skip to content

Commit

Permalink
reorganise all packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Bafbi committed Aug 1, 2023
1 parent 0428c3e commit e37dcba
Show file tree
Hide file tree
Showing 185 changed files with 3,910 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/valence_packet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition.workspace = true
[dependencies]
valence_core.workspace = true
valence_nbt.workspace = true
valence_registry.workspace = true
anyhow.workspace = true
bitfield-struct.workspace = true
byteorder.workspace = true
Expand Down
8 changes: 7 additions & 1 deletion crates/valence_packet/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# TODO
# valence_packet

This contain all (some) the packets structures of the minecraft protocol #763.

## Naming

Tha naming of the struct are based on the yarn mapping name and the packet side, for that reason it can be a bit different from the [wiki.vg](https://wiki.vg) name.
15 changes: 15 additions & 0 deletions crates/valence_packet/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,24 @@ pub struct PlayerSpawnPositionS2c {
pub angle: f32,
}

// https://wiki.vg/Protocol#Spawn_Player
/// 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)]
#[packet(id = packet_id::PLAYER_SPAWN_S2C)]
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,
Expand Down
9 changes: 9 additions & 0 deletions crates/valence_packet/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ pub struct EntitySetHeadYawS2c {
pub head_yaw: ByteAngle,
}

// https://wiki.vg/Protocol#Spawn_Experience_Orb
/// Sent by the server when a vehicle or other non-living entity is created.
///
/// wiki : [Spawn Entity](https://wiki.vg/Protocol#Spawn_Experience_Orb)
#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ENTITY_SPAWN_S2C)]
pub struct EntitySpawnS2c {
Expand Down Expand Up @@ -227,11 +231,16 @@ pub struct EntityVelocityUpdateS2c {
pub velocity: [i16; 3],
}

// https://wiki.vg/Protocol#Spawn_Experience_Orb
/// Spawns one or more experience orbs.
///
/// wiki : [Spawn Experience Orb](https://wiki.vg/Protocol#Spawn_Experience_Orb)
#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::EXPERIENCE_ORB_SPAWN_S2C)]
pub struct ExperienceOrbSpawnS2c {
pub entity_id: VarInt,
pub position: DVec3,
/// The amount of experience this orb will reward once collected.
pub count: i16,
}

Expand Down
1 change: 1 addition & 0 deletions crates/valence_packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod instance;
pub mod inventory;
pub mod map;
pub mod network;
pub mod packets;
pub mod player_list;
pub mod scoreboard;
pub mod sound;
Expand Down
16 changes: 16 additions & 0 deletions crates/valence_packet/src/packets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::borrow::Cow;
use std::io::Write;

use anyhow::bail;
use uuid::Uuid;
use valence_core::ident::Ident;
use valence_core::property::Property;
use valence_core::protocol::raw::RawBytes;
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{packet_id, Decode, Encode, Packet};
use valence_core::text::Text;

pub mod handshaking;
pub mod login;
pub mod play;
pub mod status;
18 changes: 18 additions & 0 deletions crates/valence_packet/src/packets/handshaking/handshake_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::HANDSHAKE_C2S, state = PacketState::Handshaking)]
pub struct HandshakeC2s<'a> {
pub protocol_version: VarInt,
pub server_address: &'a str,
pub server_port: u16,
pub next_state: HandshakeNextState,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Encode, Decode)]
pub enum HandshakeNextState {
#[packet(tag = 1)]
Status,
#[packet(tag = 2)]
Login,
}
5 changes: 5 additions & 0 deletions crates/valence_packet/src/packets/handshaking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use valence_core::protocol::PacketState;

use super::*;

pub mod handshake_c2s;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use valence_core::protocol::PacketState;

use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_COMPRESSION_S2C, state = PacketState::Login)]
pub struct LoginCompressionS2c {
pub threshold: VarInt,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_DISCONNECT_S2C, state = PacketState::Login)]
pub struct LoginDisconnectS2c<'a> {
pub reason: Cow<'a, Text>,
}
8 changes: 8 additions & 0 deletions crates/valence_packet/src/packets/login/login_hello_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_HELLO_C2S, state = PacketState::Login)]
pub struct LoginHelloC2s<'a> {
pub username: &'a str, // TODO: bound this
pub profile_id: Option<Uuid>,
}
9 changes: 9 additions & 0 deletions crates/valence_packet/src/packets/login/login_hello_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_HELLO_S2C, state = PacketState::Login)]
pub struct LoginHelloS2c<'a> {
pub server_id: &'a str,
pub public_key: &'a [u8],
pub verify_token: &'a [u8],
}
8 changes: 8 additions & 0 deletions crates/valence_packet/src/packets/login/login_key_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_KEY_C2S, state = PacketState::Login)]
pub struct LoginKeyC2s<'a> {
pub shared_secret: &'a [u8],
pub verify_token: &'a [u8],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_QUERY_REQUEST_S2C, state = PacketState::Login)]
pub struct LoginQueryRequestS2c<'a> {
pub message_id: VarInt,
pub channel: Ident<Cow<'a, str>>,
pub data: RawBytes<'a>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_QUERY_RESPONSE_C2S, state = PacketState::Login)]
pub struct LoginQueryResponseC2s<'a> {
pub message_id: VarInt,
pub data: Option<RawBytes<'a>>,
}
9 changes: 9 additions & 0 deletions crates/valence_packet/src/packets/login/login_success_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::LOGIN_SUCCESS_S2C, state = PacketState::Login)]
pub struct LoginSuccessS2c<'a> {
pub uuid: Uuid,
pub username: &'a str, // TODO: bound this.
pub properties: Cow<'a, [Property]>,
}
12 changes: 12 additions & 0 deletions crates/valence_packet/src/packets/login/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use valence_core::protocol::PacketState;

use super::*;

pub mod login_compression_s2c;
pub mod login_disconnect_s2c;
pub mod login_hello_c2s;
pub mod login_hello_s2c;
pub mod login_key_c2s;
pub mod login_query_request_s2c;
pub mod login_query_response_c2s;
pub mod login_success_s2c;
8 changes: 8 additions & 0 deletions crates/valence_packet/src/packets/play/advancement_tab_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ADVANCEMENT_TAB_C2S)]
pub enum AdvancementTabC2s<'a> {
OpenedTab { tab_id: Ident<Cow<'a, str>> },
ClosedScreen,
}
80 changes: 80 additions & 0 deletions crates/valence_packet/src/packets/play/advancement_update_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::ADVANCEMENT_UPDATE_S2C)]
pub struct GenericAdvancementUpdateS2c<'a, AM: 'a> {
pub reset: bool,
pub advancement_mapping: Vec<AM>,
pub identifiers: Vec<Ident<Cow<'a, str>>>,
pub progress_mapping: Vec<(Ident<Cow<'a, str>>, Vec<AdvancementCriteria<'a>>)>,
}

#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub struct AdvancementCriteria<'a> {
pub criterion_identifier: Ident<Cow<'a, str>>,
/// If present, the criteria has been achieved at the
/// time wrapped; time represented as millis since epoch
pub criterion_progress: Option<i64>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct AdvancementDisplay<'a, I> {
pub title: Cow<'a, Text>,
pub description: Cow<'a, Text>,
pub icon: I,
pub frame_type: VarInt,
pub flags: i32,
pub background_texture: Option<Ident<Cow<'a, str>>>,
pub x_coord: f32,
pub y_coord: f32,
}

impl<I: Encode> Encode for AdvancementDisplay<'_, I> {
fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
self.title.encode(&mut w)?;
self.description.encode(&mut w)?;
self.icon.encode(&mut w)?;
self.frame_type.encode(&mut w)?;
self.flags.encode(&mut w)?;

match self.background_texture.as_ref() {
None => {}
Some(texture) => texture.encode(&mut w)?,
}

self.x_coord.encode(&mut w)?;
self.y_coord.encode(&mut w)?;

Ok(())
}
}

impl<'a, I: Decode<'a>> Decode<'a> for AdvancementDisplay<'a, I> {
fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
let title = <Cow<'a, Text>>::decode(r)?;
let description = <Cow<'a, Text>>::decode(r)?;
let icon = I::decode(r)?;
let frame_type = VarInt::decode(r)?;
let flags = i32::decode(r)?;

let background_texture = if flags & 1 == 1 {
Some(Ident::decode(r)?)
} else {
None
};

let x_coord = f32::decode(r)?;
let y_coord = f32::decode(r)?;

Ok(Self {
title,
description,
icon,
frame_type,
flags,
background_texture,
x_coord,
y_coord,
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BLOCK_BREAKING_PROGRESS_S2C)]
pub struct BlockBreakingProgressS2c {
pub entity_id: VarInt,
pub position: BlockPos,
pub destroy_stage: u8,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BLOCK_ENTITY_UPDATE_S2C)]
pub struct BlockEntityUpdateS2c<'a> {
pub position: BlockPos,
pub kind: VarInt,
pub data: Cow<'a, Compound>,
}
10 changes: 10 additions & 0 deletions crates/valence_packet/src/packets/play/block_event_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BLOCK_EVENT_S2C)]
pub struct BlockEventS2c {
pub position: BlockPos,
pub action_id: u8,
pub action_parameter: u8,
pub block_type: VarInt,
}
8 changes: 8 additions & 0 deletions crates/valence_packet/src/packets/play/block_update_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BLOCK_UPDATE_S2C)]
pub struct BlockUpdateS2c {
pub position: BlockPos,
pub block_id: VarInt,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BOAT_PADDLE_STATE_C2S)]
pub struct BoatPaddleStateC2s {
pub left_paddle_turning: bool,
pub right_paddle_turning: bool,
}
9 changes: 9 additions & 0 deletions crates/valence_packet/src/packets/play/book_update_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BOOK_UPDATE_C2S)]
pub struct BookUpdateC2s<'a> {
pub slot: VarInt,
pub entries: Vec<&'a str>,
pub title: Option<&'a str>,
}
26 changes: 26 additions & 0 deletions crates/valence_packet/src/packets/play/boss_bar_s2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use valence_core::boss_bar::{BossBarColor, BossBarDivision, BossBarFlags};

use super::*;

#[derive(Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BOSS_BAR_S2C)]
pub struct BossBarS2c<'a> {
pub id: Uuid,
pub action: BossBarAction<'a>,
}

#[derive(Clone, PartialEq, Debug, Encode, Decode)]
pub enum BossBarAction<'a> {
Add {
title: Cow<'a, Text>,
health: f32,
color: BossBarColor,
division: BossBarDivision,
flags: BossBarFlags,
},
Remove,
UpdateHealth(f32),
UpdateTitle(Cow<'a, Text>),
UpdateStyle(BossBarColor, BossBarDivision),
UpdateFlags(BossBarFlags),
}
5 changes: 5 additions & 0 deletions crates/valence_packet/src/packets/play/bundle_splitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BUNDLE_SPLITTER)]
pub struct BundleSplitterS2c;
8 changes: 8 additions & 0 deletions crates/valence_packet/src/packets/play/button_click_c2s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::BUTTON_CLICK_C2S)]
pub struct ButtonClickC2s {
pub window_id: i8,
pub button_id: i8,
}
Loading

0 comments on commit e37dcba

Please sign in to comment.