Skip to content

Commit

Permalink
Clientbound Packets: use ids from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Oct 27, 2024
1 parent 5507b20 commit a6f3b3f
Show file tree
Hide file tree
Showing 81 changed files with 125 additions and 396 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ and customizable experience. It prioritizes performance and player enjoyment whi
- [x] Chunk Loading
- [x] Chunk Generation
- [x] Scoreboard
- [ ] World Borders
- [x] World Borders
- [ ] World Saving
- Player
- [x] Player Skins
Expand Down
2 changes: 1 addition & 1 deletion extractor/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ maven_group=de.snowii
archives_base_name=extractor
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.106.1+1.21.3
fabric_version=0.107.0+1.21.3
6 changes: 4 additions & 2 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use proc_macro::TokenStream;
use quote::quote;

extern crate proc_macro;

mod packet;
#[proc_macro_attribute]
pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();
Expand All @@ -10,13 +12,13 @@ pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {

let (impl_generics, ty_generics, _) = ast.generics.split_for_impl();

let input: proc_macro2::TokenStream = input.into();
let input: proc_macro2::TokenStream = packet::packet_clientbound(input);
let item: proc_macro2::TokenStream = item.into();

let gen = quote! {
#item
impl #impl_generics crate::bytebuf::packet_id::ClientPacketID for #name #ty_generics {
const PACKET_ID: i32 = #input;
const PACKET_ID: i32 = #input as i32;
}
};

Expand Down
42 changes: 42 additions & 0 deletions pumpkin-macros/src/packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{collections::HashMap, sync::LazyLock};

use proc_macro::TokenStream;
use quote::quote;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Packet {
name: String,
phase: String,
side: String,
id: u16,
}

static PACKETS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Packet>>(include_str!("../../assets/packets.json"))
.expect("Could not parse packets.json registry.")
.into_iter()
.map(|val| (format!("{}:{}:{}", val.side, val.phase, val.name), val.id))
.collect()
});

pub(crate) fn packet_clientbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string();
let packet_name = input_string.trim_matches('"');

let id = PACKETS
.get(&format!("clientbound:{}", packet_name))
.expect("Invalid Packet");
quote! { #id }
}

#[expect(dead_code)]
pub(crate) fn packet_serverbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string();
let packet_name = input_string.trim_matches('"');

let id = PACKETS
.get(&format!("serverbound:{}", packet_name))
.expect("Invalid Packet");
quote! { #id }
}
4 changes: 2 additions & 2 deletions pumpkin-macros/src/particle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use serde::Deserialize;
#[derive(Deserialize)]
pub struct Particle {
name: String,
id: u32,
id: u16,
}

static SOUNDS: LazyLock<HashMap<String, u32>> = LazyLock::new(|| {
static SOUNDS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Particle>>(include_str!("../../assets/particles.json"))
.expect("Could not parse particles.json registry.")
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-macros/src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use serde::Deserialize;
#[derive(Deserialize)]
pub struct Sound {
name: String,
id: u32,
id: u16,
}

static SOUNDS: LazyLock<HashMap<String, u32>> = LazyLock::new(|| {
static SOUNDS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Sound>>(include_str!("../../assets/sounds.json"))
.expect("Could not parse sounds.json registry.")
.into_iter()
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_add_resource_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use pumpkin_core::text::TextComponent;
use pumpkin_macros::client_packet;
use serde::Serialize;

use super::ClientboundConfigPackets;

#[derive(Serialize)]
#[client_packet(ClientboundConfigPackets::AddResourcePack as i32)]
#[client_packet("configuration:resource_pack_push")]
pub struct CConfigAddResourcePack<'a> {
uuid: uuid::Uuid,
url: &'a str,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_config_disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use pumpkin_macros::client_packet;

use super::ClientboundConfigPackets;

#[derive(serde::Serialize)]
#[client_packet(ClientboundConfigPackets::Disconnect as i32)]
#[client_packet("configuration:disconnect")]
pub struct CConfigDisconnect<'a> {
reason: &'a str,
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_cookie_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use pumpkin_macros::client_packet;

use crate::Identifier;

use super::ClientboundConfigPackets;

#[derive(serde::Serialize)]
#[client_packet(ClientboundConfigPackets::CookieRequest as i32)]
#[client_packet("configuration:cookie_request")]
pub struct CCookieRequest {
key: Identifier,
}
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_finish_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use pumpkin_macros::client_packet;

use super::ClientboundConfigPackets;

#[derive(serde::Serialize)]
#[client_packet(ClientboundConfigPackets::Finish as i32)]
#[client_packet("configuration:finish_configuration")]
pub struct CFinishConfig {}

impl Default for CFinishConfig {
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_known_packs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use pumpkin_macros::client_packet;

use crate::{bytebuf::ByteBuffer, ClientPacket, KnownPack};

use super::ClientboundConfigPackets;

#[client_packet(ClientboundConfigPackets::KnownPacks as i32)]
#[client_packet("configuration:select_known_packs")]
pub struct CKnownPacks<'a> {
known_packs: &'a [KnownPack<'a>],
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_plugin_message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use pumpkin_macros::client_packet;
use serde::Serialize;

use super::ClientboundConfigPackets;

#[derive(Serialize)]
#[client_packet(ClientboundConfigPackets::PluginMessage as i32)]
#[client_packet("configuration:custom_payload")]
pub struct CPluginMessage<'a> {
channel: &'a str,
data: &'a [u8],
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/config/c_registry_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use pumpkin_macros::client_packet;

use crate::{bytebuf::ByteBuffer, ClientPacket};

use super::ClientboundConfigPackets;

#[client_packet(ClientboundConfigPackets::RegistryData as i32)]
#[client_packet("configuration:registry_data")]
pub struct CRegistryData<'a> {
registry_id: &'a str,
entries: &'a [RegistryEntry<'a>],
Expand Down
23 changes: 0 additions & 23 deletions pumpkin-protocol/src/client/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,3 @@ pub use c_finish_config::*;
pub use c_known_packs::*;
pub use c_plugin_message::*;
pub use c_registry_data::*;

/// DO NOT CHANGE ORDER
/// This Enum has the exact order like vanilla, Vanilla parses their Packet IDs from the enum order. Its also way easier to port.
#[repr(i32)]
pub enum ClientboundConfigPackets {
CookieRequest,
PluginMessage,
Disconnect,
Finish,
KeepAlive,
Ping,
ResetChat,
RegistryData,
RemoveResourcePack,
AddResourcePack,
StoreCookie,
Transfer,
FeatureFlags,
UpdateTags,
KnownPacks,
CustomReportDetails,
ServerLinks,
}
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/login/c_encryption_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundLoginPackets;

#[derive(Serialize)]
#[client_packet(ClientboundLoginPackets::EncryptionRequest as i32)]
#[client_packet("login:hello")]
pub struct CEncryptionRequest<'a> {
server_id: &'a str, // 20
public_key_length: VarInt,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/login/c_login_disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use pumpkin_macros::client_packet;
use serde::Serialize;

use super::ClientboundLoginPackets;

#[derive(Serialize)]
#[client_packet(ClientboundLoginPackets::Disconnect as i32)]
#[client_packet("login:login_disconnect")]
pub struct CLoginDisconnect<'a> {
json_reason: &'a str,
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/login/c_login_success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use pumpkin_macros::client_packet;

use crate::{bytebuf::ByteBuffer, ClientPacket, Property};

use super::ClientboundLoginPackets;

#[client_packet(ClientboundLoginPackets::LoginSuccess as i32)]
#[client_packet("login:login_finished")]
pub struct CLoginSuccess<'a> {
pub uuid: &'a uuid::Uuid,
pub username: &'a str, // 16
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/login/c_plugin_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundLoginPackets;

#[derive(Serialize)]
#[client_packet(ClientboundLoginPackets::LoginPluginRequest as i32)]
#[client_packet("login:custom_query")]
pub struct CLoginPluginRequest<'a> {
message_id: VarInt,
channel: &'a str,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/login/c_set_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundLoginPackets;

#[derive(Serialize)]
#[client_packet(ClientboundLoginPackets::SetCompression as i32)]
#[client_packet("login:login_compression")]
pub struct CSetCompression {
threshold: VarInt,
}
Expand Down
12 changes: 0 additions & 12 deletions pumpkin-protocol/src/client/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,3 @@ pub use c_login_disconnect::*;
pub use c_login_success::*;
pub use c_plugin_request::*;
pub use c_set_compression::*;

/// DO NOT CHANGE ORDER
/// This Enum has the exact order like vanilla, Vanilla parses their Packet IDs from the enum order. Its also way easier to port.
#[repr(i32)]
pub enum ClientboundLoginPackets {
Disconnect,
EncryptionRequest,
LoginSuccess,
SetCompression,
LoginPluginRequest,
CookieRequest,
}
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_acknowledge_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::AcknowledgeBlockChanges as i32)]
#[client_packet("play:block_changed_ack")]
pub struct CAcknowledgeBlockChange {
sequence_id: VarInt,
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use pumpkin_core::text::TextComponent;
use pumpkin_macros::client_packet;
use serde::Serialize;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::ActionBar as i32)]
#[client_packet("play:set_action_bar_text")]
pub struct CActionBar<'a> {
action_bar: TextComponent<'a>,
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_block_destroy_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::BlockBreakAnimation as i32)]
#[client_packet("play:block_destruction")]
pub struct CSetBlockDestroyStage {
entity_id: VarInt,
location: WorldPosition,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_block_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::BlockChange as i32)]
#[client_packet("play:block_update")]
pub struct CBlockUpdate<'a> {
location: &'a WorldPosition,
block_id: VarInt,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_center_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use pumpkin_macros::client_packet;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(serde::Serialize)]
#[client_packet(ClientboundPlayPackets::UpdateViewPosition as i32)]
#[client_packet("play:set_chunk_cache_center")]
pub struct CCenterChunk {
pub chunk_x: VarInt,
pub chunk_z: VarInt,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_change_difficulty.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use pumpkin_macros::client_packet;
use serde::Serialize;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::ServerDifficulty as i32)]
#[client_packet("play:change_difficulty")]
pub struct CChangeDifficulty {
difficulty: u8,
locked: bool,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_chunk_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use itertools::Itertools;
use pumpkin_macros::client_packet;
use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS};

use super::ClientboundPlayPackets;

#[client_packet(ClientboundPlayPackets::ChunkData as i32)]
#[client_packet("play:level_chunk_with_light")]
pub struct CChunkData<'a>(pub &'a ChunkData);

impl<'a> ClientPacket for CChunkData<'a> {
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_close_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::CloseWindow as i32)]
#[client_packet("play:container_close")]
pub struct CCloseContainer {
window_id: VarInt,
}
Expand Down
4 changes: 1 addition & 3 deletions pumpkin-protocol/src/client/play/c_disguised_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use serde::Serialize;

use crate::VarInt;

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::DisguisedChat as i32)]
#[client_packet("play:disguised_chat")]
pub struct CDisguisedChatMessage<'a> {
message: TextComponent<'a>,
chat_type: VarInt,
Expand Down
Loading

0 comments on commit a6f3b3f

Please sign in to comment.