Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit configuration packets #164

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions azalea-client/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use std::sync::Arc;

use azalea_chat::FormattedText;
use azalea_core::tick::GameTick;
use azalea_protocol::packets::game::{
clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket, ClientboundGamePacket,
use azalea_protocol::packets::{
configuration::ClientboundConfigurationPacket,
game::{
clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket,
ClientboundGamePacket,
},
};
use azalea_world::{InstanceName, MinecraftEntityId};
use bevy_app::{App, Plugin, PreUpdate, Update};
Expand All @@ -23,13 +27,22 @@ use tokio::sync::mpsc;
use crate::{
chat::{ChatPacket, ChatReceivedEvent},
disconnect::DisconnectEvent,
packet_handling::game::{
AddPlayerEvent, DeathEvent, KeepAliveEvent, PacketEvent, RemovePlayerEvent,
UpdatePlayerEvent,
packet_handling::{
configuration::ConfigurationPacketEvent,
game::{
AddPlayerEvent, DeathEvent, GamePacketEvent, KeepAliveEvent, RemovePlayerEvent,
UpdatePlayerEvent,
},
},
PlayerInfo,
};

#[derive(Debug, Clone)]
pub enum ClientboundPacket {
Configuration(Arc<ClientboundConfigurationPacket>),
Game(Arc<ClientboundGamePacket>),
}

// (for contributors):
// HOW TO ADD A NEW (packet based) EVENT:
// - make a struct that contains an entity field and a data field (look in
Expand Down Expand Up @@ -82,7 +95,9 @@ pub enum Event {
/// # }
/// # }
/// ```
Packet(Arc<ClientboundGamePacket>),
ConfigurationPacket(Arc<ClientboundConfigurationPacket>),
GamePacket(Arc<ClientboundGamePacket>),
Packet(ClientboundPacket),
ShayBox marked this conversation as resolved.
Show resolved Hide resolved
/// A player joined the game (or more specifically, was added to the tab
/// list).
AddPlayer(PlayerInfo),
Expand Down Expand Up @@ -116,7 +131,8 @@ impl Plugin for EventPlugin {
(
chat_listener,
login_listener,
packet_listener,
configuration_packet_listener,
game_packet_listener,
add_player_listener,
update_player_listener,
remove_player_listener,
Expand Down Expand Up @@ -163,12 +179,27 @@ fn tick_listener(query: Query<&LocalPlayerEvents, With<InstanceName>>) {
}
}

fn packet_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<PacketEvent>) {
fn configuration_packet_listener(
query: Query<&LocalPlayerEvents>,
mut events: EventReader<ConfigurationPacketEvent>,
) {
for event in events.read() {
let local_player_events = query
.get(event.entity)
.expect("Non-local entities shouldn't be able to receive packet events");
let _ = local_player_events.send(Event::Packet(ClientboundPacket::Configuration(event.packet.clone())));
}
}

fn game_packet_listener(
query: Query<&LocalPlayerEvents>,
mut events: EventReader<GamePacketEvent>,
) {
for event in events.read() {
let local_player_events = query
.get(event.entity)
.expect("Non-local entities shouldn't be able to receive packet events");
let _ = local_player_events.send(Event::Packet(event.packet.clone()));
let _ = local_player_events.send(Event::Packet(ClientboundPacket::Game(event.packet.clone())));
}
}

Expand Down
11 changes: 7 additions & 4 deletions azalea-client/src/packet_handling/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Cursor;
use std::sync::Arc;

use azalea_entity::indexing::EntityIdIndex;
use azalea_protocol::packets::configuration::serverbound_finish_configuration_packet::ServerboundFinishConfigurationPacket;
Expand Down Expand Up @@ -27,7 +28,7 @@ pub struct ConfigurationPacketEvent {
/// The client entity that received the packet.
pub entity: Entity,
/// The packet that was actually received.
pub packet: ClientboundConfigurationPacket,
pub packet: Arc<ClientboundConfigurationPacket>,
}

pub fn send_packet_events(
Expand Down Expand Up @@ -55,7 +56,7 @@ pub fn send_packet_events(
};
packet_events.send(ConfigurationPacketEvent {
entity: player_entity,
packet,
packet: Arc::new(packet),
});
}
// clear the packets right after we read them
Expand All @@ -78,7 +79,7 @@ pub fn process_packet_events(ecs: &mut World) {
events_owned.push((*player_entity, packet.clone()));
}
for (player_entity, packet) in events_owned {
match packet {
match packet.as_ref() {
ClientboundConfigurationPacket::RegistryData(p) => {
let mut system_state: SystemState<Query<&mut InstanceHolder>> =
SystemState::new(ecs);
Expand All @@ -87,7 +88,9 @@ pub fn process_packet_events(ecs: &mut World) {
let mut instance = instance_holder.instance.write();

// add the new registry data
instance.registries.append(p.registry_id, p.entries);
instance
.registries
.append(p.registry_id.clone(), p.entries.clone());
}

ClientboundConfigurationPacket::CustomPayload(p) => {
Expand Down
10 changes: 5 additions & 5 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use crate::{
/// }
/// ```
#[derive(Event, Debug, Clone)]
pub struct PacketEvent {
pub struct GamePacketEvent {
/// The client entity that received the packet.
pub entity: Entity,
/// The packet that was actually received.
Expand Down Expand Up @@ -147,7 +147,7 @@ pub struct InstanceLoadedEvent {

pub fn send_packet_events(
query: Query<(Entity, &RawConnection), With<LocalEntity>>,
mut packet_events: ResMut<Events<PacketEvent>>,
mut packet_events: ResMut<Events<GamePacketEvent>>,
) {
// we manually clear and send the events at the beginning of each update
// since otherwise it'd cause issues with events in process_packet_events
Expand All @@ -168,7 +168,7 @@ pub fn send_packet_events(
continue;
}
};
packet_events.send(PacketEvent {
packet_events.send(GamePacketEvent {
entity: player_entity,
packet: Arc::new(packet),
});
Expand All @@ -182,9 +182,9 @@ pub fn send_packet_events(
pub fn process_packet_events(ecs: &mut World) {
let mut events_owned = Vec::<(Entity, Arc<ClientboundGamePacket>)>::new();
{
let mut system_state = SystemState::<EventReader<PacketEvent>>::new(ecs);
let mut system_state = SystemState::<EventReader<GamePacketEvent>>::new(ecs);
let mut events = system_state.get_mut(ecs);
for PacketEvent {
for GamePacketEvent {
entity: player_entity,
packet,
} in events.read()
Expand Down
2 changes: 1 addition & 1 deletion azalea-client/src/packet_handling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Plugin for PacketHandlerPlugin {
),
)
// we do this instead of add_event so we can handle the events ourselves
.init_resource::<Events<game::PacketEvent>>()
.init_resource::<Events<game::GamePacketEvent>>()
.init_resource::<Events<configuration::ConfigurationPacketEvent>>()
.add_event::<game::SendPacketEvent>()
.add_event::<configuration::SendConfigurationPacketEvent>()
Expand Down
11 changes: 11 additions & 0 deletions azalea-protocol/src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,17 @@ impl Connection<ClientboundConfigurationPacket, ServerboundConfigurationPacket>
}
}

impl Connection<ServerboundGamePacket, ClientboundGamePacket> {
/// Change our state from configuration to game. This is the state that's
/// used when the client is actually in the world.
#[must_use]
pub fn game(
self,
) -> Connection<ServerboundConfigurationPacket, ClientboundConfigurationPacket> {
Connection::from(self)
}
}

impl Connection<ClientboundGamePacket, ServerboundGamePacket> {
/// Change our state back to configuration.
#[must_use]
Expand Down
4 changes: 2 additions & 2 deletions azalea/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Formatter;

use azalea_client::{
inventory::{CloseContainerEvent, ContainerClickEvent, InventoryComponent},
packet_handling::game::PacketEvent,
packet_handling::game::GamePacketEvent,
Client,
};
use azalea_core::position::BlockPos;
Expand Down Expand Up @@ -240,7 +240,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<GamePacketEvent>) {
for event in events.read() {
if let ClientboundGamePacket::ContainerSetContent { .. } = event.packet.as_ref() {
commands
Expand Down
Loading