Skip to content

Commit

Permalink
Refactor azalea-client (#205)
Browse files Browse the repository at this point in the history
* start organizing packet_handling more by moving packet handlers into their own functions

* finish writing all the handler functions for packets

* use macro for generating match statement for packet handler functions

* fix set_entity_data

* update config state to also use handler functions

* organize az-client file structure by moving things into plugins directory

* fix merge issues
  • Loading branch information
mat-1 authored Feb 23, 2025
1 parent f8130c3 commit e21e1b9
Show file tree
Hide file tree
Showing 39 changed files with 2,342 additions and 2,087 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.2"
bevy_ecs = { version = "0.15.2", default-features = false }
Expand All @@ -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"
Expand Down Expand Up @@ -80,6 +78,7 @@ hickory-resolver = { version = "0.24.3", default-features = false }
uuid = "1.12.1"
num-format = "0.4.4"
indexmap = "2.7.1"
paste = "1.0.15"
compact_str = "0.8.1"

# --- Profile Settings ---
Expand Down
21 changes: 8 additions & 13 deletions azalea-brigadier/src/command_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,16 @@ impl<S> CommandDispatcher<S> {
next.push(child.copy_for(context.source.clone()));
}
}
} else {
match &context.command {
Some(context_command) => {
found_command = true;
} else if let Some(context_command) = &context.command {
found_command = true;

let value = context_command(context);
result += value;
// consumer.on_command_complete(context, true, value);
successful_forks += 1;
let value = context_command(context);
result += value;
// consumer.on_command_complete(context, true, value);
successful_forks += 1;

// TODO: allow context_command to error and handle
// those errors
}
_ => {}
}
// TODO: allow context_command to error and handle
// those errors
}
}

Expand Down
1 change: 1 addition & 0 deletions azalea-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 17 additions & 20 deletions azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,27 @@ use uuid::Uuid;
use crate::{
Account, PlayerInfo,
attack::{self, AttackPlugin},
brand::BrandPlugin,
chat::ChatPlugin,
chunks::{ChunkBatchInfo, ChunkPlugin},
configuration::ConfigurationPlugin,
chunks::{ChunkBatchInfo, ChunksPlugin},
disconnect::{DisconnectEvent, DisconnectPlugin},
events::{Event, EventPlugin, LocalPlayerEvents},
events::{Event, EventsPlugin, LocalPlayerEvents},
interact::{CurrentSequenceNumber, InteractPlugin},
inventory::{Inventory, InventoryPlugin},
local_player::{
GameProfileComponent, Hunger, InstanceHolder, PermissionLevel, PlayerAbilities, TabList,
death_event,
},
mining::{self, MinePlugin},
movement::{LastSentLookDirection, PhysicsState, PlayerMovePlugin},
packet_handling::{
PacketHandlerPlugin,
mining::{self, MiningPlugin},
movement::{LastSentLookDirection, MovementPlugin, PhysicsState},
packet::{
PacketPlugin,
login::{self, InLoginState, LoginSendPacketQueue},
},
player::retroactively_add_game_profile_component,
raw_connection::RawConnection,
respawn::RespawnPlugin,
send_client_end::TickEndPlugin,
task_pool::TaskPoolPlugin,
tick_end::TickEndPlugin,
};

/// `Client` has the things that a user interacting with the library will want.
Expand Down Expand Up @@ -370,7 +369,7 @@ impl Client {
let (ecs_packets_tx, mut ecs_packets_rx) = mpsc::unbounded_channel();
ecs_lock.lock().entity_mut(entity).insert((
LoginSendPacketQueue { tx: ecs_packets_tx },
login::IgnoreQueryIds::default(),
crate::packet::login::IgnoreQueryIds::default(),
InLoginState,
));

Expand Down Expand Up @@ -468,7 +467,7 @@ impl Client {
ClientboundLoginPacket::CustomQuery(p) => {
debug!("Got custom query {:?}", p);
// replying to custom query is done in
// packet_handling::login::process_packet_events
// packet::login::process_packet_events
}
ClientboundLoginPacket::CookieRequest(p) => {
debug!("Got cookie request {:?}", p);
Expand Down Expand Up @@ -794,7 +793,7 @@ pub struct LocalPlayerBundle {
/// A bundle for the components that are present on a local player that is
/// currently in the `game` protocol state. If you want to filter for this, just
/// use [`LocalEntity`].
#[derive(Bundle)]
#[derive(Bundle, Default)]
pub struct JoinedClientBundle {
// note that InstanceHolder isn't here because it's set slightly before we fully join the world
pub physics_state: PhysicsState,
Expand Down Expand Up @@ -826,8 +825,6 @@ impl Plugin for AzaleaPlugin {
app.add_systems(
Update,
(
// fire the Death event when the player dies.
death_event,
// add GameProfileComponent when we get an AddPlayerEvent
retroactively_add_game_profile_component.after(EntityUpdateSet::Index),
),
Expand Down Expand Up @@ -972,23 +969,23 @@ impl PluginGroup for DefaultPlugins {
let mut group = PluginGroupBuilder::start::<Self>()
.add(AmbiguityLoggerPlugin)
.add(TimePlugin)
.add(PacketHandlerPlugin)
.add(PacketPlugin)
.add(AzaleaPlugin)
.add(EntityPlugin)
.add(PhysicsPlugin)
.add(EventPlugin)
.add(EventsPlugin)
.add(TaskPoolPlugin::default())
.add(InventoryPlugin)
.add(ChatPlugin)
.add(DisconnectPlugin)
.add(PlayerMovePlugin)
.add(MovementPlugin)
.add(InteractPlugin)
.add(RespawnPlugin)
.add(MinePlugin)
.add(MiningPlugin)
.add(AttackPlugin)
.add(ChunkPlugin)
.add(ChunksPlugin)
.add(TickEndPlugin)
.add(ConfigurationPlugin)
.add(BrandPlugin)
.add(TickBroadcastPlugin);
#[cfg(feature = "log")]
{
Expand Down
16 changes: 2 additions & 14 deletions azalea-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@
#![feature(error_generic_member_access)]

mod account;
pub mod attack;
pub mod chat;
pub mod chunks;
mod client;
pub mod configuration;
pub mod disconnect;
mod entity_query;
pub mod events;
pub mod interact;
pub mod inventory;
mod local_player;
pub mod mining;
pub mod movement;
pub mod packet_handling;
pub mod ping;
mod player;
mod plugins;
pub mod raw_connection;
pub mod respawn;
pub mod send_client_end;
pub mod task_pool;

#[doc(hidden)]
pub mod test_simulation;
Expand All @@ -44,3 +31,4 @@ pub use movement::{
PhysicsState, SprintDirection, StartSprintEvent, StartWalkEvent, WalkDirection,
};
pub use player::PlayerInfo;
pub use plugins::*;
13 changes: 1 addition & 12 deletions azalea-client/src/local_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{collections::HashMap, io, sync::Arc};

use azalea_auth::game_profile::GameProfile;
use azalea_core::game_type::GameMode;
use azalea_entity::Dead;
use azalea_protocol::packets::game::c_player_abilities::ClientboundPlayerAbilities;
use azalea_world::{Instance, PartialInstance};
use bevy_ecs::{component::Component, prelude::*};
Expand All @@ -13,10 +12,7 @@ use tokio::sync::mpsc;
use tracing::error;
use uuid::Uuid;

use crate::{
ClientInformation, PlayerInfo,
events::{Event as AzaleaEvent, LocalPlayerEvents},
};
use crate::{ClientInformation, PlayerInfo, events::Event as AzaleaEvent};

/// A component that keeps strong references to our [`PartialInstance`] and
/// [`Instance`] for local players.
Expand Down Expand Up @@ -150,13 +146,6 @@ impl InstanceHolder {
}
}

/// Send the "Death" event for [`LocalEntity`]s that died with no reason.
pub fn death_event(query: Query<&LocalPlayerEvents, Added<Dead>>) {
for local_player_events in &query {
local_player_events.send(AzaleaEvent::Death(None)).unwrap();
}
}

#[derive(Error, Debug)]
pub enum HandlePacketError {
#[error("{0}")]
Expand Down
Loading

0 comments on commit e21e1b9

Please sign in to comment.