1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

use macro for generating match statement for packet handler functions

This commit is contained in:
mat 2025-02-16 19:27:13 +00:00
commit 2f2d9ca2ac
26 changed files with 522 additions and 404 deletions

7
Cargo.lock generated
View file

@ -356,6 +356,7 @@ dependencies = [
"derive_more",
"minecraft_folder_path",
"parking_lot",
"paste",
"regex",
"reqwest",
"simdnbt",
@ -2172,6 +2173,12 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pem-rfc7468"
version = "0.7.0"

View file

@ -32,7 +32,6 @@ repository = "https://github.com/azalea-rs/azalea"
aes = "0.8.4"
anyhow = "1.0.95"
async-recursion = "1.1.1"
async-trait = "0.1.86"
base64 = "0.22.1"
bevy_app = "0.15.1"
bevy_ecs = { version = "0.15.1", default-features = false }
@ -49,7 +48,6 @@ env_logger = "0.11.6"
flate2 = "1.0.35"
futures = "0.3.31"
futures-lite = "2.6.0"
log = "0.4.25"
md-5 = "0.10.6"
minecraft_folder_path = "0.1.2"
nohash-hasher = "0.2.0"
@ -80,6 +78,7 @@ hickory-resolver = { version = "0.24.2", default-features = false }
uuid = "1.12.1"
num-format = "0.4.4"
indexmap = "2.7.1"
paste = "1.0.15"
# --- Profile Settings ---

View file

@ -27,6 +27,7 @@ bevy_time.workspace = true
derive_more = { workspace = true, features = ["deref", "deref_mut"] }
minecraft_folder_path.workspace = true
parking_lot.workspace = true
paste.workspace = true
regex.workspace = true
reqwest.workspace = true
simdnbt.workspace = true

View file

@ -12,7 +12,7 @@ use derive_more::{Deref, DerefMut};
use crate::{
interact::SwingArmEvent, local_player::LocalGameMode, movement::MoveEventsSet,
packet_handling::game::SendPacketEvent, respawn::perform_respawn, Client,
packet::game::SendPacketEvent, respawn::perform_respawn, Client,
};
pub struct AttackPlugin;

View file

@ -27,7 +27,7 @@ use uuid::Uuid;
use crate::{
client::Client,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
packet::game::{handle_sendpacketevent, SendPacketEvent},
};
/// A chat packet, either a system message or a chat message.
@ -190,7 +190,7 @@ impl Plugin for ChatPlugin {
Update,
(
handle_send_chat_event,
handle_send_chat_kind_event.after(handle_send_packet_event),
handle_send_chat_kind_event.after(handle_sendpacketevent),
)
.chain(),
);

View file

@ -21,7 +21,7 @@ use tracing::{error, trace};
use crate::{
interact::handle_block_interact_event,
inventory::InventorySet,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
packet::game::{handle_sendpacketevent, SendPacketEvent},
respawn::perform_respawn,
InstanceHolder,
};
@ -37,7 +37,7 @@ impl Plugin for ChunkPlugin {
handle_chunk_batch_finished_event,
)
.chain()
.before(handle_send_packet_event)
.before(handle_sendpacketevent)
.before(InventorySet)
.before(handle_block_interact_event)
.before(perform_respawn),

View file

@ -75,7 +75,7 @@ use crate::{
},
mining::{self, MinePlugin},
movement::{LastSentLookDirection, PhysicsState, PlayerMovePlugin},
packet_handling::{
packet::{
login::{self, LoginSendPacketQueue},
PacketHandlerPlugin,
},

View file

@ -10,7 +10,7 @@ use azalea_protocol::{
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use crate::{client::InConfigState, packet_handling::configuration::SendConfigurationEvent};
use crate::{client::InConfigState, packet::configuration::SendConfigurationEvent};
pub struct ConfigurationPlugin;
impl Plugin for ConfigurationPlugin {
@ -18,7 +18,7 @@ impl Plugin for ConfigurationPlugin {
app.add_systems(
Update,
handle_in_configuration_state
.before(crate::packet_handling::configuration::handle_send_packet_event),
.before(crate::packet::configuration::handle_send_packet_event),
);
}
}

View file

@ -23,8 +23,8 @@ use tokio::sync::mpsc;
use crate::{
chat::{ChatPacket, ChatReceivedEvent},
disconnect::DisconnectEvent,
packet_handling::game::{
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketEvent, RemovePlayerEvent,
packet::game::{
AddPlayerEvent, DeathEvent, KeepAliveEvent, ReceivePacketEvent, RemovePlayerEvent,
UpdatePlayerEvent,
},
PlayerInfo,
@ -130,7 +130,7 @@ impl Plugin for EventPlugin {
)
.add_systems(
PreUpdate,
init_listener.before(crate::packet_handling::game::process_packet_events),
init_listener.before(crate::packet::game::process_packet_events),
)
.add_systems(GameTick, tick_listener);
}
@ -166,7 +166,10 @@ pub fn tick_listener(query: Query<&LocalPlayerEvents, With<InstanceName>>) {
}
}
pub fn packet_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<PacketEvent>) {
pub fn packet_listener(
query: Query<&LocalPlayerEvents>,
mut events: EventReader<ReceivePacketEvent>,
) {
for event in events.read() {
let local_player_events = query
.get(event.entity)

View file

@ -35,7 +35,7 @@ use crate::{
inventory::{Inventory, InventorySet},
local_player::{LocalGameMode, PermissionLevel, PlayerAbilities},
movement::MoveEventsSet,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
packet::game::{handle_sendpacketevent, SendPacketEvent},
respawn::perform_respawn,
Client,
};
@ -54,7 +54,7 @@ impl Plugin for InteractPlugin {
handle_block_interact_event,
handle_swing_arm_event,
)
.before(handle_send_packet_event)
.before(handle_sendpacketevent)
.after(InventorySet)
.after(perform_respawn)
.after(handle_attack_event)

View file

@ -27,7 +27,7 @@ use tracing::warn;
use crate::{
local_player::PlayerAbilities,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
packet::game::{handle_sendpacketevent, SendPacketEvent},
respawn::perform_respawn,
Client,
};
@ -48,7 +48,7 @@ impl Plugin for InventoryPlugin {
handle_menu_opened_event,
handle_set_container_content_event,
handle_container_click_event,
handle_container_close_event.before(handle_send_packet_event),
handle_container_close_event.before(handle_sendpacketevent),
handle_client_side_close_container_event,
)
.chain()

View file

@ -22,7 +22,7 @@ pub mod inventory;
mod local_player;
pub mod mining;
pub mod movement;
pub mod packet_handling;
pub mod packet;
pub mod ping;
mod player;
pub mod raw_connection;

View file

@ -17,7 +17,7 @@ use crate::{
inventory::{Inventory, InventorySet},
local_player::{LocalGameMode, PermissionLevel, PlayerAbilities},
movement::MoveEventsSet,
packet_handling::game::SendPacketEvent,
packet::game::SendPacketEvent,
Client,
};

View file

@ -27,7 +27,7 @@ use bevy_ecs::{
use thiserror::Error;
use crate::client::Client;
use crate::packet_handling::game::SendPacketEvent;
use crate::packet::game::SendPacketEvent;
#[derive(Error, Debug)]
pub enum MovePlayerError {

View file

@ -17,7 +17,7 @@ use tracing::{debug, error, warn};
use crate::client::InConfigState;
use crate::disconnect::DisconnectEvent;
use crate::local_player::Hunger;
use crate::packet_handling::game::KeepAliveEvent;
use crate::packet::game::KeepAliveEvent;
use crate::raw_connection::RawConnection;
use crate::InstanceHolder;

View file

@ -0,0 +1,122 @@
use std::sync::{Arc, Weak};
use azalea_chat::FormattedText;
use azalea_core::resource_location::ResourceLocation;
use azalea_protocol::packets::{
game::{ClientboundGamePacket, ClientboundPlayerCombatKill, ServerboundGamePacket},
Packet,
};
use azalea_world::Instance;
use bevy_ecs::{entity::Entity, event::Event};
use parking_lot::RwLock;
use uuid::Uuid;
use crate::PlayerInfo;
/// An event that's sent when we receive a packet.
/// ```
/// # use azalea_client::packet::game::ReceivePacketEvent;
/// # use azalea_protocol::packets::game::ClientboundGamePacket;
/// # use bevy_ecs::event::EventReader;
///
/// fn handle_packets(mut events: EventReader<ReceivePacketEvent>) {
/// for ReceivePacketEvent {
/// entity,
/// packet,
/// } in events.read() {
/// match packet.as_ref() {
/// ClientboundGamePacket::LevelParticles(p) => {
/// // ...
/// }
/// _ => {}
/// }
/// }
/// }
/// ```
#[derive(Event, Debug, Clone)]
pub struct ReceivePacketEvent {
/// The client entity that received the packet.
pub entity: Entity,
/// The packet that was actually received.
pub packet: Arc<ClientboundGamePacket>,
}
/// An event for sending a packet to the server while we're in the `game` state.
#[derive(Event)]
pub struct SendPacketEvent {
pub sent_by: Entity,
pub packet: ServerboundGamePacket,
}
impl SendPacketEvent {
pub fn new(sent_by: Entity, packet: impl Packet<ServerboundGamePacket>) -> Self {
let packet = packet.into_variant();
Self { sent_by, packet }
}
}
/// A player joined the game (or more specifically, was added to the tab
/// list of a local player).
#[derive(Event, Debug, Clone)]
pub struct AddPlayerEvent {
/// The local player entity that received this event.
pub entity: Entity,
pub info: PlayerInfo,
}
/// A player left the game (or maybe is still in the game and was just
/// removed from the tab list of a local player).
#[derive(Event, Debug, Clone)]
pub struct RemovePlayerEvent {
/// The local player entity that received this event.
pub entity: Entity,
pub info: PlayerInfo,
}
/// A player was updated in the tab list of a local player (gamemode, display
/// name, or latency changed).
#[derive(Event, Debug, Clone)]
pub struct UpdatePlayerEvent {
/// The local player entity that received this event.
pub entity: Entity,
pub info: PlayerInfo,
}
/// Event for when an entity dies. dies. If it's a local player and there's a
/// reason in the death screen, the [`ClientboundPlayerCombatKill`] will
/// be included.
#[derive(Event, Debug, Clone)]
pub struct DeathEvent {
pub entity: Entity,
pub packet: Option<ClientboundPlayerCombatKill>,
}
/// A KeepAlive packet is sent from the server to verify that the client is
/// still connected.
#[derive(Event, Debug, Clone)]
pub struct KeepAliveEvent {
pub entity: Entity,
/// The ID of the keepalive. This is an arbitrary number, but vanilla
/// servers use the time to generate this.
pub id: u64,
}
#[derive(Event, Debug, Clone)]
pub struct ResourcePackEvent {
pub entity: Entity,
/// The random ID for this request to download the resource pack. The packet
/// for replying to a resource pack push must contain the same ID.
pub id: Uuid,
pub url: String,
pub hash: String,
pub required: bool,
pub prompt: Option<FormattedText>,
}
/// An instance (aka world, dimension) was loaded by a client.
///
/// Since the instance is given to you as a weak reference, it won't be able to
/// be `upgrade`d if all local players leave it.
#[derive(Event, Debug, Clone)]
pub struct InstanceLoadedEvent {
pub entity: Entity,
pub name: ResourceLocation,
pub instance: Weak<RwLock<Instance>>,
}

View file

@ -35,7 +35,10 @@ impl Plugin for PacketHandlerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
First,
(game::send_packet_events, configuration::send_packet_events),
(
game::send_receivepacketevent,
configuration::send_packet_events,
),
)
.add_systems(
PreUpdate,
@ -53,14 +56,14 @@ impl Plugin for PacketHandlerPlugin {
(
(
configuration::handle_send_packet_event,
game::handle_send_packet_event,
game::handle_sendpacketevent,
)
.chain(),
death_event_on_0_health.before(death_listener),
),
)
// we do this instead of add_event so we can handle the events ourselves
.init_resource::<Events<game::PacketEvent>>()
.init_resource::<Events<game::ReceivePacketEvent>>()
.init_resource::<Events<configuration::ConfigurationEvent>>()
.add_event::<game::SendPacketEvent>()
.add_event::<configuration::SendConfigurationEvent>()

View file

@ -8,7 +8,7 @@ use bevy_ecs::{
};
use uuid::Uuid;
use crate::{packet_handling::game::AddPlayerEvent, GameProfileComponent};
use crate::{packet::game::AddPlayerEvent, GameProfileComponent};
/// A player in the tab list.
#[derive(Debug, Clone)]

View file

@ -2,7 +2,7 @@ use azalea_protocol::packets::game::s_client_command::{self, ServerboundClientCo
use bevy_app::{App, Plugin, Update};
use bevy_ecs::prelude::*;
use crate::packet_handling::game::{handle_send_packet_event, SendPacketEvent};
use crate::packet::game::{handle_sendpacketevent, SendPacketEvent};
/// Tell the server that we're respawning.
#[derive(Event, Debug, Clone)]
@ -15,7 +15,7 @@ pub struct RespawnPlugin;
impl Plugin for RespawnPlugin {
fn build(&self, app: &mut App) {
app.add_event::<PerformRespawnEvent>()
.add_systems(Update, perform_respawn.before(handle_send_packet_event));
.add_systems(Update, perform_respawn.before(handle_sendpacketevent));
}
}

View file

@ -8,7 +8,7 @@ use azalea_world::InstanceName;
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use crate::{mining::MiningSet, packet_handling::game::SendPacketEvent};
use crate::{mining::MiningSet, packet::game::SendPacketEvent};
/// A plugin that makes clients send a [`ServerboundClientTickEnd`] packet every
/// tick.

View file

@ -1,7 +1,7 @@
use azalea_client::chunks::handle_chunk_batch_finished_event;
use azalea_client::inventory::InventorySet;
use azalea_client::packet_handling::game::SendPacketEvent;
use azalea_client::packet_handling::{death_event_on_0_health, game::ResourcePackEvent};
use azalea_client::packet::game::SendPacketEvent;
use azalea_client::packet::{death_event_on_0_health, game::ResourcePackEvent};
use azalea_client::respawn::perform_respawn;
use azalea_protocol::packets::game::s_resource_pack::{self, ServerboundResourcePack};
use bevy_app::Update;

View file

@ -1,5 +1,5 @@
use azalea_client::{
packet_handling::{death_event_on_0_health, game::DeathEvent},
packet::{death_event_on_0_health, game::DeathEvent},
respawn::{perform_respawn, PerformRespawnEvent},
};
use bevy_app::Update;

View file

@ -3,7 +3,7 @@ use std::fmt::Formatter;
use azalea_client::{
inventory::{CloseContainerEvent, ContainerClickEvent, Inventory},
packet_handling::game::PacketEvent,
packet::game::ReceivePacketEvent,
Client,
};
use azalea_core::position::BlockPos;
@ -234,7 +234,7 @@ impl ContainerHandle {
#[derive(Component, Debug)]
pub struct WaitingForInventoryOpen;
fn handle_menu_opened_event(mut commands: Commands, mut events: EventReader<PacketEvent>) {
fn handle_menu_opened_event(mut commands: Commands, mut events: EventReader<ReceivePacketEvent>) {
for event in events.read() {
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet.as_ref() {
commands

View file

@ -2,7 +2,7 @@
use std::sync::Arc;
use azalea_client::{inventory::Inventory, packet_handling::game::SendPacketEvent, PhysicsState};
use azalea_client::{inventory::Inventory, packet::game::SendPacketEvent, PhysicsState};
use azalea_core::{position::Vec3, resource_location::ResourceLocation, tick::GameTick};
use azalea_entity::{
attributes::AttributeInstance, Attributes, EntityDimensions, LookDirection, Physics, Position,