Skip to content

Commit e21e1b9

Browse files
authored
Refactor azalea-client (#205)
* 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
1 parent f8130c3 commit e21e1b9

39 files changed

+2342
-2087
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ repository = "https://github.com/azalea-rs/azalea"
3232
aes = "0.8.4"
3333
anyhow = "1.0.95"
3434
async-recursion = "1.1.1"
35-
async-trait = "0.1.86"
3635
base64 = "0.22.1"
3736
bevy_app = "0.15.2"
3837
bevy_ecs = { version = "0.15.2", default-features = false }
@@ -49,7 +48,6 @@ env_logger = "0.11.6"
4948
flate2 = "1.0.35"
5049
futures = "0.3.31"
5150
futures-lite = "2.6.0"
52-
log = "0.4.25"
5351
md-5 = "0.10.6"
5452
minecraft_folder_path = "0.1.2"
5553
nohash-hasher = "0.2.0"
@@ -80,6 +78,7 @@ hickory-resolver = { version = "0.24.3", default-features = false }
8078
uuid = "1.12.1"
8179
num-format = "0.4.4"
8280
indexmap = "2.7.1"
81+
paste = "1.0.15"
8382
compact_str = "0.8.1"
8483

8584
# --- Profile Settings ---

azalea-brigadier/src/command_dispatcher.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,16 @@ impl<S> CommandDispatcher<S> {
288288
next.push(child.copy_for(context.source.clone()));
289289
}
290290
}
291-
} else {
292-
match &context.command {
293-
Some(context_command) => {
294-
found_command = true;
291+
} else if let Some(context_command) = &context.command {
292+
found_command = true;
295293

296-
let value = context_command(context);
297-
result += value;
298-
// consumer.on_command_complete(context, true, value);
299-
successful_forks += 1;
294+
let value = context_command(context);
295+
result += value;
296+
// consumer.on_command_complete(context, true, value);
297+
successful_forks += 1;
300298

301-
// TODO: allow context_command to error and handle
302-
// those errors
303-
}
304-
_ => {}
305-
}
299+
// TODO: allow context_command to error and handle
300+
// those errors
306301
}
307302
}
308303

azalea-client/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bevy_time.workspace = true
2727
derive_more = { workspace = true, features = ["deref", "deref_mut"] }
2828
minecraft_folder_path.workspace = true
2929
parking_lot.workspace = true
30+
paste.workspace = true
3031
regex.workspace = true
3132
reqwest.workspace = true
3233
simdnbt.workspace = true

azalea-client/src/client.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,27 @@ use uuid::Uuid;
6363
use crate::{
6464
Account, PlayerInfo,
6565
attack::{self, AttackPlugin},
66+
brand::BrandPlugin,
6667
chat::ChatPlugin,
67-
chunks::{ChunkBatchInfo, ChunkPlugin},
68-
configuration::ConfigurationPlugin,
68+
chunks::{ChunkBatchInfo, ChunksPlugin},
6969
disconnect::{DisconnectEvent, DisconnectPlugin},
70-
events::{Event, EventPlugin, LocalPlayerEvents},
70+
events::{Event, EventsPlugin, LocalPlayerEvents},
7171
interact::{CurrentSequenceNumber, InteractPlugin},
7272
inventory::{Inventory, InventoryPlugin},
7373
local_player::{
7474
GameProfileComponent, Hunger, InstanceHolder, PermissionLevel, PlayerAbilities, TabList,
75-
death_event,
7675
},
77-
mining::{self, MinePlugin},
78-
movement::{LastSentLookDirection, PhysicsState, PlayerMovePlugin},
79-
packet_handling::{
80-
PacketHandlerPlugin,
76+
mining::{self, MiningPlugin},
77+
movement::{LastSentLookDirection, MovementPlugin, PhysicsState},
78+
packet::{
79+
PacketPlugin,
8180
login::{self, InLoginState, LoginSendPacketQueue},
8281
},
8382
player::retroactively_add_game_profile_component,
8483
raw_connection::RawConnection,
8584
respawn::RespawnPlugin,
86-
send_client_end::TickEndPlugin,
8785
task_pool::TaskPoolPlugin,
86+
tick_end::TickEndPlugin,
8887
};
8988

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

@@ -468,7 +467,7 @@ impl Client {
468467
ClientboundLoginPacket::CustomQuery(p) => {
469468
debug!("Got custom query {:?}", p);
470469
// replying to custom query is done in
471-
// packet_handling::login::process_packet_events
470+
// packet::login::process_packet_events
472471
}
473472
ClientboundLoginPacket::CookieRequest(p) => {
474473
debug!("Got cookie request {:?}", p);
@@ -794,7 +793,7 @@ pub struct LocalPlayerBundle {
794793
/// A bundle for the components that are present on a local player that is
795794
/// currently in the `game` protocol state. If you want to filter for this, just
796795
/// use [`LocalEntity`].
797-
#[derive(Bundle)]
796+
#[derive(Bundle, Default)]
798797
pub struct JoinedClientBundle {
799798
// note that InstanceHolder isn't here because it's set slightly before we fully join the world
800799
pub physics_state: PhysicsState,
@@ -826,8 +825,6 @@ impl Plugin for AzaleaPlugin {
826825
app.add_systems(
827826
Update,
828827
(
829-
// fire the Death event when the player dies.
830-
death_event,
831828
// add GameProfileComponent when we get an AddPlayerEvent
832829
retroactively_add_game_profile_component.after(EntityUpdateSet::Index),
833830
),
@@ -972,23 +969,23 @@ impl PluginGroup for DefaultPlugins {
972969
let mut group = PluginGroupBuilder::start::<Self>()
973970
.add(AmbiguityLoggerPlugin)
974971
.add(TimePlugin)
975-
.add(PacketHandlerPlugin)
972+
.add(PacketPlugin)
976973
.add(AzaleaPlugin)
977974
.add(EntityPlugin)
978975
.add(PhysicsPlugin)
979-
.add(EventPlugin)
976+
.add(EventsPlugin)
980977
.add(TaskPoolPlugin::default())
981978
.add(InventoryPlugin)
982979
.add(ChatPlugin)
983980
.add(DisconnectPlugin)
984-
.add(PlayerMovePlugin)
981+
.add(MovementPlugin)
985982
.add(InteractPlugin)
986983
.add(RespawnPlugin)
987-
.add(MinePlugin)
984+
.add(MiningPlugin)
988985
.add(AttackPlugin)
989-
.add(ChunkPlugin)
986+
.add(ChunksPlugin)
990987
.add(TickEndPlugin)
991-
.add(ConfigurationPlugin)
988+
.add(BrandPlugin)
992989
.add(TickBroadcastPlugin);
993990
#[cfg(feature = "log")]
994991
{

azalea-client/src/lib.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,13 @@
88
#![feature(error_generic_member_access)]
99

1010
mod account;
11-
pub mod attack;
12-
pub mod chat;
13-
pub mod chunks;
1411
mod client;
15-
pub mod configuration;
16-
pub mod disconnect;
1712
mod entity_query;
18-
pub mod events;
19-
pub mod interact;
20-
pub mod inventory;
2113
mod local_player;
22-
pub mod mining;
23-
pub mod movement;
24-
pub mod packet_handling;
2514
pub mod ping;
2615
mod player;
16+
mod plugins;
2717
pub mod raw_connection;
28-
pub mod respawn;
29-
pub mod send_client_end;
30-
pub mod task_pool;
3118

3219
#[doc(hidden)]
3320
pub mod test_simulation;
@@ -44,3 +31,4 @@ pub use movement::{
4431
PhysicsState, SprintDirection, StartSprintEvent, StartWalkEvent, WalkDirection,
4532
};
4633
pub use player::PlayerInfo;
34+
pub use plugins::*;

azalea-client/src/local_player.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{collections::HashMap, io, sync::Arc};
22

33
use azalea_auth::game_profile::GameProfile;
44
use azalea_core::game_type::GameMode;
5-
use azalea_entity::Dead;
65
use azalea_protocol::packets::game::c_player_abilities::ClientboundPlayerAbilities;
76
use azalea_world::{Instance, PartialInstance};
87
use bevy_ecs::{component::Component, prelude::*};
@@ -13,10 +12,7 @@ use tokio::sync::mpsc;
1312
use tracing::error;
1413
use uuid::Uuid;
1514

16-
use crate::{
17-
ClientInformation, PlayerInfo,
18-
events::{Event as AzaleaEvent, LocalPlayerEvents},
19-
};
15+
use crate::{ClientInformation, PlayerInfo, events::Event as AzaleaEvent};
2016

2117
/// A component that keeps strong references to our [`PartialInstance`] and
2218
/// [`Instance`] for local players.
@@ -150,13 +146,6 @@ impl InstanceHolder {
150146
}
151147
}
152148

153-
/// Send the "Death" event for [`LocalEntity`]s that died with no reason.
154-
pub fn death_event(query: Query<&LocalPlayerEvents, Added<Dead>>) {
155-
for local_player_events in &query {
156-
local_player_events.send(AzaleaEvent::Death(None)).unwrap();
157-
}
158-
}
159-
160149
#[derive(Error, Debug)]
161150
pub enum HandlePacketError {
162151
#[error("{0}")]

0 commit comments

Comments
 (0)