mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
Add PacketEvent (#75)
* add PacketEvent * docs and fixes * Event::Packet works
This commit is contained in:
parent
c1588ef66e
commit
cbc6af81fb
7 changed files with 909 additions and 826 deletions
|
@ -7,7 +7,7 @@ use azalea_ecs::{
|
||||||
app::{App, Plugin},
|
app::{App, Plugin},
|
||||||
component::Component,
|
component::Component,
|
||||||
event::EventReader,
|
event::EventReader,
|
||||||
query::{Added, Changed},
|
query::Added,
|
||||||
system::Query,
|
system::Query,
|
||||||
AppTickExt,
|
AppTickExt,
|
||||||
};
|
};
|
||||||
|
@ -21,7 +21,7 @@ use tokio::sync::mpsc;
|
||||||
use crate::{
|
use crate::{
|
||||||
chat::{ChatPacket, ChatReceivedEvent},
|
chat::{ChatPacket, ChatReceivedEvent},
|
||||||
packet_handling::{
|
packet_handling::{
|
||||||
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketReceiver, RemovePlayerEvent,
|
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketEvent, RemovePlayerEvent,
|
||||||
UpdatePlayerEvent,
|
UpdatePlayerEvent,
|
||||||
},
|
},
|
||||||
PlayerInfo,
|
PlayerInfo,
|
||||||
|
@ -62,6 +62,23 @@ pub enum Event {
|
||||||
Chat(ChatPacket),
|
Chat(ChatPacket),
|
||||||
/// Happens 20 times per second, but only when the world is loaded.
|
/// Happens 20 times per second, but only when the world is loaded.
|
||||||
Tick,
|
Tick,
|
||||||
|
/// We received a packet from the server.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use azalea_client::Event;
|
||||||
|
/// # use azalea_protocol::packets::game::ClientboundGamePacket;
|
||||||
|
/// # async fn example(event: Event) {
|
||||||
|
/// # match event {
|
||||||
|
/// Event::Packet(packet) => match *packet {
|
||||||
|
/// ClientboundGamePacket::Login(_) => {
|
||||||
|
/// println!("login packet");
|
||||||
|
/// }
|
||||||
|
/// _ => {}
|
||||||
|
/// },
|
||||||
|
/// # _ => {}
|
||||||
|
/// # }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
Packet(Arc<ClientboundGamePacket>),
|
Packet(Arc<ClientboundGamePacket>),
|
||||||
/// A player joined the game (or more specifically, was added to the tab
|
/// A player joined the game (or more specifically, was added to the tab
|
||||||
/// list).
|
/// list).
|
||||||
|
@ -133,13 +150,14 @@ fn tick_listener(query: Query<&LocalPlayerEvents>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn packet_listener(query: Query<(&LocalPlayerEvents, &PacketReceiver), Changed<PacketReceiver>>) {
|
fn packet_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<PacketEvent>) {
|
||||||
for (local_player_events, packet_receiver) in &query {
|
for event in events.iter() {
|
||||||
for packet in packet_receiver.packets.lock().iter() {
|
let local_player_events = query
|
||||||
local_player_events
|
.get(event.entity)
|
||||||
.send(Event::Packet(packet.clone().into()))
|
.expect("Non-localplayer entities shouldn't be able to receive add player events");
|
||||||
.unwrap();
|
local_player_events
|
||||||
}
|
.send(Event::Packet(Arc::new(event.packet.clone())))
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -19,7 +19,7 @@ pub fn derive_resource(input: TokenStream) -> TokenStream {
|
||||||
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
|
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
impl #impl_generics #azalea_ecs_path::system::BevyResource for #struct_name #type_generics #where_clause {
|
impl #impl_generics #azalea_ecs_path::system::_BevyResource for #struct_name #type_generics #where_clause {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
|
||||||
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
|
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
impl #impl_generics #azalea_ecs_path::component::BevyComponent for #struct_name #type_generics #where_clause {
|
impl #impl_generics #azalea_ecs_path::component::_BevyComponent for #struct_name #type_generics #where_clause {
|
||||||
type Storage = #storage;
|
type Storage = #storage;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -151,13 +151,13 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
|
||||||
match field_kind {
|
match field_kind {
|
||||||
BundleFieldKind::Component => {
|
BundleFieldKind::Component => {
|
||||||
field_component_ids.push(quote! {
|
field_component_ids.push(quote! {
|
||||||
<#field_type as #ecs_path::bundle::BevyBundle>::component_ids(components, storages, &mut *ids);
|
<#field_type as #ecs_path::bundle::_BevyBundle>::component_ids(components, storages, &mut *ids);
|
||||||
});
|
});
|
||||||
field_get_components.push(quote! {
|
field_get_components.push(quote! {
|
||||||
self.#field.get_components(&mut *func);
|
self.#field.get_components(&mut *func);
|
||||||
});
|
});
|
||||||
field_from_components.push(quote! {
|
field_from_components.push(quote! {
|
||||||
#field: <#field_type as #ecs_path::bundle::BevyBundle>::from_components(ctx, &mut *func),
|
#field: <#field_type as #ecs_path::bundle::_BevyBundle>::from_components(ctx, &mut *func),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
/// SAFETY: ComponentId is returned in field-definition-order. [from_components] and [get_components] use field-definition-order
|
/// SAFETY: ComponentId is returned in field-definition-order. [from_components] and [get_components] use field-definition-order
|
||||||
unsafe impl #impl_generics #ecs_path::bundle::BevyBundle for #struct_name #ty_generics #where_clause {
|
unsafe impl #impl_generics #ecs_path::bundle::_BevyBundle for #struct_name #ty_generics #where_clause {
|
||||||
fn component_ids(
|
fn component_ids(
|
||||||
components: &mut #ecs_path::component::Components,
|
components: &mut #ecs_path::component::Components,
|
||||||
storages: &mut #ecs_path::storage::Storages,
|
storages: &mut #ecs_path::storage::Storages,
|
||||||
|
@ -488,7 +488,9 @@ pub fn derive_stage_label(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
let mut trait_path = azalea_ecs_path();
|
let mut trait_path = azalea_ecs_path();
|
||||||
trait_path.segments.push(format_ident!("schedule").into());
|
trait_path.segments.push(format_ident!("schedule").into());
|
||||||
trait_path.segments.push(format_ident!("StageLabel").into());
|
trait_path
|
||||||
|
.segments
|
||||||
|
.push(format_ident!("_BevyStageLabel").into());
|
||||||
derive_label(input, &trait_path, "stage_label")
|
derive_label(input, &trait_path, "stage_label")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ impl Default for BevyManifest {
|
||||||
impl BevyManifest {
|
impl BevyManifest {
|
||||||
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
|
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
|
||||||
const AZALEA: &str = "azalea";
|
const AZALEA: &str = "azalea";
|
||||||
|
const AZALEA_ECS: &str = "azalea_ecs";
|
||||||
const BEVY_ECS: &str = "bevy_ecs";
|
const BEVY_ECS: &str = "bevy_ecs";
|
||||||
const BEVY: &str = "bevy";
|
const BEVY: &str = "bevy";
|
||||||
|
|
||||||
|
@ -57,6 +58,8 @@ impl BevyManifest {
|
||||||
return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
|
return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
|
||||||
} else if let Some(dep) = deps.get(AZALEA) {
|
} else if let Some(dep) = deps.get(AZALEA) {
|
||||||
dep_package(dep).unwrap_or(AZALEA)
|
dep_package(dep).unwrap_or(AZALEA)
|
||||||
|
} else if let Some(dep) = deps.get(AZALEA_ECS) {
|
||||||
|
dep_package(dep).unwrap_or(AZALEA_ECS)
|
||||||
} else if let Some(dep) = deps.get(BEVY_ECS) {
|
} else if let Some(dep) = deps.get(BEVY_ECS) {
|
||||||
dep_package(dep).unwrap_or(BEVY_ECS)
|
dep_package(dep).unwrap_or(BEVY_ECS)
|
||||||
} else if let Some(dep) = deps.get(BEVY) {
|
} else if let Some(dep) = deps.get(BEVY) {
|
||||||
|
|
|
@ -29,14 +29,14 @@ pub mod component {
|
||||||
// we do this because re-exporting Component would re-export the macro as well,
|
// we do this because re-exporting Component would re-export the macro as well,
|
||||||
// which is bad (since we have our own Component macro)
|
// which is bad (since we have our own Component macro)
|
||||||
// instead, we have to do this so Component is a trait alias and the original
|
// instead, we have to do this so Component is a trait alias and the original
|
||||||
// impl-able trait is still available as BevyComponent
|
// impl-able trait is still available as _BevyComponent
|
||||||
pub trait Component = bevy_ecs::component::Component;
|
pub trait Component = bevy_ecs::component::Component;
|
||||||
pub use bevy_ecs::component::Component as BevyComponent;
|
pub use bevy_ecs::component::Component as _BevyComponent;
|
||||||
}
|
}
|
||||||
pub mod bundle {
|
pub mod bundle {
|
||||||
pub use azalea_ecs_macros::Bundle;
|
pub use azalea_ecs_macros::Bundle;
|
||||||
pub trait Bundle = bevy_ecs::bundle::Bundle;
|
pub trait Bundle = bevy_ecs::bundle::Bundle;
|
||||||
pub use bevy_ecs::bundle::Bundle as BevyBundle;
|
pub use bevy_ecs::bundle::Bundle as _BevyBundle;
|
||||||
}
|
}
|
||||||
pub mod system {
|
pub mod system {
|
||||||
pub use azalea_ecs_macros::Resource;
|
pub use azalea_ecs_macros::Resource;
|
||||||
|
@ -44,10 +44,19 @@ pub mod system {
|
||||||
Command, Commands, EntityCommands, Query, Res, ResMut, SystemState,
|
Command, Commands, EntityCommands, Query, Res, ResMut, SystemState,
|
||||||
};
|
};
|
||||||
pub trait Resource = bevy_ecs::system::Resource;
|
pub trait Resource = bevy_ecs::system::Resource;
|
||||||
pub use bevy_ecs::system::Resource as BevyResource;
|
pub use bevy_ecs::system::Resource as _BevyResource;
|
||||||
|
}
|
||||||
|
pub mod schedule {
|
||||||
|
pub use azalea_ecs_macros::StageLabel;
|
||||||
|
pub use bevy_ecs::schedule::{
|
||||||
|
IntoRunCriteria, IntoSystemDescriptor, ReportExecutionOrderAmbiguities, Schedule, Stage,
|
||||||
|
SystemSet, SystemStage,
|
||||||
|
};
|
||||||
|
pub trait StageLabel = bevy_ecs::schedule::StageLabel;
|
||||||
|
pub use bevy_ecs::schedule::StageLabel as _BevyStageLabel;
|
||||||
}
|
}
|
||||||
pub use bevy_app as app;
|
pub use bevy_app as app;
|
||||||
pub use bevy_ecs::{entity, event, ptr, query, schedule, storage};
|
pub use bevy_ecs::{entity, event, ptr, query, storage};
|
||||||
|
|
||||||
use app::{App, CoreStage, Plugin};
|
use app::{App, CoreStage, Plugin};
|
||||||
use bevy_ecs::schedule::*;
|
use bevy_ecs::schedule::*;
|
||||||
|
|
|
@ -9,6 +9,7 @@ use azalea::pathfinder::BlockPosGoal;
|
||||||
use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection};
|
use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection};
|
||||||
use azalea::{Account, Client, Event};
|
use azalea::{Account, Client, Event};
|
||||||
use azalea_protocol::packets::game::serverbound_client_command_packet::ServerboundClientCommandPacket;
|
use azalea_protocol::packets::game::serverbound_client_command_packet::ServerboundClientCommandPacket;
|
||||||
|
use azalea_protocol::packets::game::ClientboundGamePacket;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Default, Clone, Component)]
|
#[derive(Default, Clone, Component)]
|
||||||
|
@ -148,6 +149,12 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
|
||||||
action: azalea_protocol::packets::game::serverbound_client_command_packet::Action::PerformRespawn,
|
action: azalea_protocol::packets::game::serverbound_client_command_packet::Action::PerformRespawn,
|
||||||
}.get());
|
}.get());
|
||||||
}
|
}
|
||||||
|
Event::Packet(packet) => match *packet {
|
||||||
|
ClientboundGamePacket::Login(_) => {
|
||||||
|
println!("login packet");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue