Skip to content

Commit

Permalink
Parse Serverbound Packet IDs from JSON as well
Browse files Browse the repository at this point in the history
Before only Clientbound Packet IDs where parsed from JSON, Now Serverbound Packet IDs are parsed from JSON as well. This makes porting to new minecraft version much easier
  • Loading branch information
Snowiiii committed Nov 3, 2024
1 parent 8f90b29 commit 1842e8e
Show file tree
Hide file tree
Showing 45 changed files with 251 additions and 315 deletions.
2 changes: 1 addition & 1 deletion extractor/src/main/kotlin/de/snowii/extractor/Extractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Extractor : ModInitializer {
Particles(),
SyncedRegistries(),
Packet(),
Screen(),
Screens(),
Items(),
Blocks(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Recipes : Extractor.Extractor {
}
}
recipeJson.add("ingredients", ingredientArray)
// recipeJson.addProperty("placementSlots", gson.toJson(recipe.ingredientPlacement.placementSlots))

recipesJson.add(recipeJson)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import de.snowii.extractor.Extractor
import net.minecraft.registry.Registries
import net.minecraft.server.MinecraftServer

class Screen : Extractor.Extractor {
class Screens : Extractor.Extractor {
override fun fileName(): String {
return "screens.json"
}
Expand Down
23 changes: 22 additions & 1 deletion pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {

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

gen.into()
}

#[proc_macro_attribute]
pub fn server_packet(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();

let name = &ast.ident;

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

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

let gen = quote! {
#item
impl #impl_generics crate::bytebuf::packet_id::Packet for #name #ty_generics {
const PACKET_ID: i32 = #input as i32;
}
};
Expand Down
1 change: 0 additions & 1 deletion pumpkin-macros/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub(crate) fn packet_clientbound(item: TokenStream) -> proc_macro2::TokenStream
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('"');
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/bytebuf/packet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ impl<'de> Deserialize<'de> for VarLong {
}
}

pub trait ClientPacketID {
pub trait Packet {
const PACKET_ID: VarIntType;
}

impl<P> ClientPacket for P
where
P: ClientPacketID + Serialize,
P: Packet + Serialize,
{
fn write(&self, bytebuf: &mut ByteBuffer) {
let mut serializer = serializer::Serializer::new(ByteBuffer::empty());
Expand All @@ -151,7 +151,7 @@ where

impl<P> ServerPacket for P
where
P: DeserializeOwned,
P: Packet + DeserializeOwned,
{
fn read(bytebuf: &mut ByteBuffer) -> Result<P, DeserializerError> {
let deserializer = deserializer::Deserializer::new(bytebuf);
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bytebuf::{packet_id::ClientPacketID, ByteBuffer, DeserializerError};
use bytebuf::{packet_id::Packet, ByteBuffer, DeserializerError};
use pumpkin_core::text::{style::Style, TextComponent};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -72,11 +72,11 @@ pub struct RawPacket {
pub bytebuf: ByteBuffer,
}

pub trait ClientPacket: ClientPacketID {
pub trait ClientPacket: Packet {
fn write(&self, bytebuf: &mut ByteBuffer);
}

pub trait ServerPacket: Sized {
pub trait ServerPacket: Packet + Sized {
fn read(bytebuf: &mut ByteBuffer) -> Result<Self, DeserializerError>;
}

Expand Down
15 changes: 0 additions & 15 deletions pumpkin-protocol/src/server/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@ mod s_client_information;
mod s_known_packs;
mod s_plugin_message;

use num_derive::FromPrimitive;
pub use s_acknowledge_finish_config::*;
pub use s_client_information::*;
pub use s_known_packs::*;
pub use s_plugin_message::*;

/// 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.
#[derive(FromPrimitive)]
pub enum ServerboundConfigPackets {
ClientInformation,
CookieResponse,
PluginMessage,
AcknowledgedFinish,
KeepAlive,
Pong,
ResourcePackResponse,
KnownPacks,
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
use pumpkin_macros::server_packet;

#[derive(serde::Deserialize)]
#[server_packet("configuration:finish_configuration")]
pub struct SAcknowledgeFinishConfig {}
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/config/s_client_information.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pumpkin_macros::server_packet;

use crate::VarInt;

#[derive(serde::Deserialize)]
#[server_packet("configuration:client_information")]
pub struct SClientInformationConfig {
pub locale: String, // 16
pub view_distance: i8,
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/config/s_known_packs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pumpkin_macros::server_packet;

use crate::VarInt;

#[derive(serde::Deserialize)]
#[server_packet("configuration:select_known_packs")]
pub struct SKnownPacks {
pub known_pack_count: VarInt,
// known_packs: &'a [KnownPack]
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/config/s_plugin_message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use pumpkin_macros::server_packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
Identifier, ServerPacket,
};

#[server_packet("configuration:custom_payload")]
pub struct SPluginMessage {
pub channel: Identifier,
pub data: Vec<u8>,
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/handshake/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use pumpkin_macros::server_packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ConnectionState, ServerPacket, VarInt,
};

#[server_packet("handshake:intention")]
pub struct SHandShake {
pub protocol_version: VarInt,
pub server_address: String, // 255
Expand Down
12 changes: 0 additions & 12 deletions pumpkin-protocol/src/server/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@ mod s_login_response;
mod s_login_start;
mod s_plugin_response;

use num_derive::FromPrimitive;
pub use s_encryption_response::*;
pub use s_login_response::*;
pub use s_login_start::*;
pub use s_plugin_response::*;

/// 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.
#[derive(FromPrimitive)]
pub enum ServerboundLoginPackets {
LoginStart,
EncryptionResponse,
PluginResponse,
LoginAcknowledged,
CookieResponse,
}
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/login/s_encryption_response.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use pumpkin_macros::server_packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket, VarInt,
};

#[server_packet("login:key")]
pub struct SEncryptionResponse {
pub shared_secret_length: VarInt,
pub shared_secret: Vec<u8>,
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/login/s_login_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use pumpkin_macros::server_packet;

// Acknowledgement to the Login Success packet sent to the server.
#[derive(serde::Deserialize)]
#[server_packet("login:login_acknowledged")]
pub struct SLoginAcknowledged {}
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/login/s_login_start.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use pumpkin_macros::server_packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket,
};

#[server_packet("login:hello")]
pub struct SLoginStart {
pub name: String, // 16
pub uuid: uuid::Uuid,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/login/s_plugin_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{
ServerPacket, VarInt,
};
use bytes::BytesMut;
use pumpkin_macros::server_packet;

#[server_packet("login:custom_query_answer")]
pub struct SLoginPluginResponse {
pub message_id: VarInt,
pub data: Option<BytesMut>,
Expand Down
70 changes: 0 additions & 70 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mod s_swing_arm;
mod s_use_item;
mod s_use_item_on;

use num_derive::FromPrimitive;
pub use s_chat_command::*;
pub use s_chat_message::*;
pub use s_click_container::*;
Expand All @@ -42,72 +41,3 @@ pub use s_set_held_item::*;
pub use s_swing_arm::*;
pub use s_use_item::*;
pub use s_use_item_on::*;

/// 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.
#[derive(FromPrimitive)]
pub enum ServerboundPlayPackets {
TeleportConfirm,
QueryBlockNbt,
SelectBundleItem,
SetDifficulty,
ChatAck,
ChatCommandUnsigned,
ChatCommand,
ChatMessage,
ChatSessionUpdate,
ChunkBatchAck,
ClientStatus,
ClientTickEnd,
ClientSettings,
TabComplete,
ConfigurationAck,
ClickWindowButton,
ClickWindow,
CloseWindow,
SlotStateChange,
CookieResponse,
PluginMessage,
DebugSampleSubscription,
EditBook,
QueryEntityNbt,
InteractEntity,
GenerateStructure,
KeepAlive,
LockDifficulty,
PlayerPosition,
PlayerPositionAndRotation,
PlayerRotation,
PlayerFlying,

VehicleMove,
SteerBoat,
PickItem,
DebugPing,
CraftRecipeRequest,
PlayerAbilities,
PlayerDigging,
EntityAction,
PlayerInput,
Pong,
SetRecipeBookState,
SetDisplayedRecipe,
NameItem,
ResourcePackStatus,
AdvancementTab,
SelectTrade,
SetBeaconEffect,
HeldItemChange,

UpdateCommandBlock,
UpdateCommandBlockMinecart,
CreativeInventoryAction,
UpdateJigsawBlock,
UpdateStructureBlock,
UpdateSign,

Animation,
Spectate,
PlayerBlockPlacement,
UseItem,
}
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/play/s_chat_command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pumpkin_macros::server_packet;

#[derive(serde::Deserialize)]
#[server_packet("play:chat_command")]
pub struct SChatCommand {
pub command: String,
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/s_chat_message.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use bytes::Bytes;
use pumpkin_macros::server_packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
FixedBitSet, ServerPacket, VarInt,
};

// derive(Deserialize)]
#[server_packet("play:chat")]
pub struct SChatMessage {
pub message: String,
pub timestamp: i64,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/s_click_container.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::slot::Slot;
use crate::VarInt;
use pumpkin_macros::server_packet;
use serde::de::SeqAccess;
use serde::{de, Deserialize};

#[derive(Debug)]
#[server_packet("play:container_click")]
pub struct SClickContainer {
pub window_id: VarInt,
pub state_id: VarInt,
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/play/s_client_information.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pumpkin_macros::server_packet;

use crate::VarInt;

#[derive(serde::Deserialize)]
#[server_packet("play:client_information")]
pub struct SClientInformationPlay {
pub locale: String, // 16
pub view_distance: i8,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/s_close_container.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use pumpkin_macros::server_packet;
use serde::Deserialize;

use crate::VarInt;

#[derive(Deserialize)]
#[server_packet("play:container_close")]
pub struct SCloseContainer {
pub window_id: VarInt,
}
3 changes: 3 additions & 0 deletions pumpkin-protocol/src/server/play/s_confirm_teleport.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pumpkin_macros::server_packet;

use crate::VarInt;

#[derive(serde::Deserialize)]
#[server_packet("play:accept_teleportation")]
pub struct SConfirmTeleport {
pub teleport_id: VarInt,
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/s_interact.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use pumpkin_macros::server_packet;

use crate::{bytebuf::DeserializerError, ServerPacket, VarInt};

#[server_packet("play:interact")]
pub struct SInteract {
pub entity_id: VarInt,
pub typ: VarInt,
Expand Down
Loading

0 comments on commit 1842e8e

Please sign in to comment.