From 84c0908f34b301c6ddb96ae4c314672f690f1bff Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 6 May 2023 01:02:21 -0500 Subject: fix panic on disconnect --- azalea-client/src/interact.rs | 23 +++++++++-------------- azalea-client/src/local_player.rs | 7 ++++--- 2 files changed, 13 insertions(+), 17 deletions(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/interact.rs b/azalea-client/src/interact.rs index ec5ed87b..2da80760 100644 --- a/azalea-client/src/interact.rs +++ b/azalea-client/src/interact.rs @@ -6,7 +6,7 @@ use azalea_protocol::packets::game::{ }; use azalea_world::{ entity::{clamp_look_direction, view_vector, EyeHeight, LookDirection, Position, WorldName}, - InstanceContainer, + Instance, InstanceContainer, }; use bevy_app::{App, Plugin}; use bevy_ecs::{ @@ -153,13 +153,13 @@ fn update_hit_result_component( y: position.y + **eye_height as f64, z: position.z, }; - let hit_result = pick( - look_direction, - &eye_position, - world_name, - &instance_container, - pick_range, - ); + + let Some(instance_lock) = instance_container.get(world_name) else { + continue; + }; + let instance = instance_lock.read(); + + let hit_result = pick(look_direction, &eye_position, &instance, pick_range); if let Some(mut hit_result_ref) = hit_result_ref { **hit_result_ref = hit_result; } else { @@ -178,16 +178,11 @@ fn update_hit_result_component( pub fn pick( look_direction: &LookDirection, eye_position: &Vec3, - world_name: &WorldName, - instance_container: &InstanceContainer, + instance: &Instance, pick_range: f64, ) -> BlockHitResult { let view_vector = view_vector(look_direction); let end_position = eye_position + &(view_vector * pick_range); - let instance_lock = instance_container - .get(world_name) - .expect("entities must always be in a valid world"); - let instance = instance_lock.read(); azalea_physics::clip::clip( &instance.chunks, ClipContext { diff --git a/azalea-client/src/local_player.rs b/azalea-client/src/local_player.rs index 423b4308..691f9ced 100644 --- a/azalea-client/src/local_player.rs +++ b/azalea-client/src/local_player.rs @@ -139,9 +139,10 @@ pub fn update_in_loaded_chunk( ) { for (entity, local_player, position) in &query { let player_chunk_pos = ChunkPos::from(position); - let instance_lock = instance_container - .get(local_player) - .expect("local player should always be in an instance"); + let Some(instance_lock) = instance_container.get(local_player) else { + continue; + }; + let in_loaded_chunk = instance_lock.read().chunks.get(&player_chunk_pos).is_some(); if in_loaded_chunk { commands.entity(entity).insert(LocalPlayerInLoadedChunk); -- cgit v1.2.3 From 741a1f65d669c83710e8c33082b120c189b28f1d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 12 May 2023 22:29:39 -0500 Subject: handle ping packet and resource pack correctly --- azalea-client/src/packet_handling.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/packet_handling.rs b/azalea-client/src/packet_handling.rs index 8ffff870..50887096 100644 --- a/azalea-client/src/packet_handling.rs +++ b/azalea-client/src/packet_handling.rs @@ -9,7 +9,9 @@ use azalea_protocol::{ serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, serverbound_keep_alive_packet::ServerboundKeepAlivePacket, serverbound_move_player_pos_rot_packet::ServerboundMovePlayerPosRotPacket, - ClientboundGamePacket, ServerboundGamePacket, + serverbound_pong_packet::ServerboundPongPacket, + serverbound_resource_pack_packet::ServerboundResourcePackPacket, ClientboundGamePacket, + ServerboundGamePacket, }, read::ReadPacketError, }; @@ -995,7 +997,15 @@ fn process_packet_events(ecs: &mut World) { }) } ClientboundGamePacket::OpenSignEditor(_) => {} - ClientboundGamePacket::Ping(_) => {} + ClientboundGamePacket::Ping(p) => { + debug!("Got ping packet {:?}", p); + + let mut system_state: SystemState> = SystemState::new(ecs); + let mut query = system_state.get_mut(ecs); + + let local_player = query.get_mut(player_entity).unwrap(); + local_player.write_packet(ServerboundPongPacket { id: p.id }.get()); + } ClientboundGamePacket::PlaceGhostRecipe(_) => {} ClientboundGamePacket::PlayerCombatEnd(_) => {} ClientboundGamePacket::PlayerCombatEnter(_) => {} @@ -1023,7 +1033,16 @@ fn process_packet_events(ecs: &mut World) { } ClientboundGamePacket::PlayerLookAt(_) => {} ClientboundGamePacket::RemoveMobEffect(_) => {} - ClientboundGamePacket::ResourcePack(_) => {} + ClientboundGamePacket::ResourcePack(p) => { + debug!("Got resource pack packet {:?}", p); + + let mut system_state: SystemState> = SystemState::new(ecs); + let mut query = system_state.get_mut(ecs); + + let local_player = query.get_mut(player_entity).unwrap(); + // always accept resource pack + local_player.write_packet(ServerboundResourcePackPacket { action: azalea_protocol::packets::game::serverbound_resource_pack_packet::Action::Accepted }.get()); + } ClientboundGamePacket::Respawn(p) => { debug!("Got respawn packet {:?}", p); -- cgit v1.2.3 From e977391b0413aaa62ea725a53e4a78c287844f4c Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 12 May 2023 23:20:23 -0500 Subject: auto respawn --- azalea-client/src/client.rs | 2 ++ azalea-client/src/lib.rs | 1 + azalea-client/src/respawn.rs | 38 ++++++++++++++++++++++++++++++++++++++ azalea/examples/testbot.rs | 9 ++------- azalea/src/auto_respawn.rs | 24 ++++++++++++++++++++++++ azalea/src/bot.rs | 2 ++ azalea/src/lib.rs | 1 + 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 azalea-client/src/respawn.rs create mode 100644 azalea/src/auto_respawn.rs (limited to 'azalea-client/src') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 7a4285e6..f2e91dfa 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -11,6 +11,7 @@ use crate::{ movement::{LastSentLookDirection, PlayerMovePlugin}, packet_handling::{self, PacketHandlerPlugin, PacketReceiver}, player::retroactively_add_game_profile_component, + respawn::RespawnPlugin, task_pool::TaskPoolPlugin, Account, PlayerInfo, }; @@ -717,6 +718,7 @@ impl PluginGroup for DefaultPlugins { .add(DisconnectPlugin) .add(PlayerMovePlugin) .add(InteractPlugin) + .add(RespawnPlugin) .add(TickBroadcastPlugin) } } diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs index c198ced3..2c08033b 100644 --- a/azalea-client/src/lib.rs +++ b/azalea-client/src/lib.rs @@ -25,6 +25,7 @@ mod movement; pub mod packet_handling; pub mod ping; mod player; +pub mod respawn; pub mod task_pool; pub use account::{Account, AccountOpts}; diff --git a/azalea-client/src/respawn.rs b/azalea-client/src/respawn.rs new file mode 100644 index 00000000..4e42157c --- /dev/null +++ b/azalea-client/src/respawn.rs @@ -0,0 +1,38 @@ +use azalea_protocol::packets::game::serverbound_client_command_packet::{ + self, ServerboundClientCommandPacket, +}; +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::*; + +use crate::LocalPlayer; + +/// Tell the server that we're respawning. +#[derive(Debug, Clone)] +pub struct PerformRespawnEvent { + pub entity: Entity, +} + +/// A plugin that makes [`PerformRespawnEvent`] send the packet to respawn. +pub struct RespawnPlugin; +impl Plugin for RespawnPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .add_system(perform_respawn); + } +} + +pub fn perform_respawn( + mut events: EventReader, + mut query: Query<&mut LocalPlayer>, +) { + for event in events.iter() { + if let Ok(local_player) = query.get_mut(event.entity) { + local_player.write_packet( + ServerboundClientCommandPacket { + action: serverbound_client_command_packet::Action::PerformRespawn, + } + .get(), + ); + } + } +} diff --git a/azalea/examples/testbot.rs b/azalea/examples/testbot.rs index 3fe9253c..ae6d293b 100644 --- a/azalea/examples/testbot.rs +++ b/azalea/examples/testbot.rs @@ -8,10 +8,10 @@ use azalea::entity::{EyeHeight, Position}; use azalea::interact::HitResultComponent; use azalea::inventory::ItemSlot; use azalea::pathfinder::BlockPosGoal; +use azalea::protocol::packets::game::serverbound_client_command_packet::ServerboundClientCommandPacket; +use azalea::protocol::packets::game::ClientboundGamePacket; use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection}; use azalea::{Account, Client, Event}; -use azalea_protocol::packets::game::serverbound_client_command_packet::ServerboundClientCommandPacket; -use azalea_protocol::packets::game::ClientboundGamePacket; use std::time::Duration; #[derive(Default, Clone, Component)] @@ -212,11 +212,6 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result< } } } - Event::Death(_) => { - bot.write_packet(ServerboundClientCommandPacket { - action: azalea_protocol::packets::game::serverbound_client_command_packet::Action::PerformRespawn, - }.get()); - } Event::Packet(packet) => match *packet { ClientboundGamePacket::Login(_) => { println!("login packet"); diff --git a/azalea/src/auto_respawn.rs b/azalea/src/auto_respawn.rs new file mode 100644 index 00000000..c5dd12a4 --- /dev/null +++ b/azalea/src/auto_respawn.rs @@ -0,0 +1,24 @@ +use crate::app::{App, Plugin}; +use azalea_client::packet_handling::DeathEvent; +use azalea_client::respawn::PerformRespawnEvent; +use bevy_ecs::prelude::*; + +/// A plugin that makes [`DeathEvent`]s send [`PerformRespawnEvent`]s. +#[derive(Clone, Default)] +pub struct AutoRespawnPlugin; +impl Plugin for AutoRespawnPlugin { + fn build(&self, app: &mut App) { + app.add_system(auto_respawn); + } +} + +fn auto_respawn( + mut events: EventReader, + mut perform_respawn_events: EventWriter, +) { + for event in events.iter() { + perform_respawn_events.send(PerformRespawnEvent { + entity: event.entity, + }); + } +} diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs index e5ea4c28..13b33bb0 100644 --- a/azalea/src/bot.rs +++ b/azalea/src/bot.rs @@ -1,4 +1,5 @@ use crate::app::{App, CoreSchedule, IntoSystemAppConfig, Plugin, PluginGroup, PluginGroupBuilder}; +use crate::auto_respawn::AutoRespawnPlugin; use crate::container::ContainerPlugin; use crate::ecs::{ component::Component, @@ -135,5 +136,6 @@ impl PluginGroup for DefaultBotPlugins { .add(BotPlugin) .add(PathfinderPlugin) .add(ContainerPlugin) + .add(AutoRespawnPlugin) } } diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index bde94634..604961f4 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -3,6 +3,7 @@ #![allow(incomplete_features)] #![feature(async_fn_in_trait)] +mod auto_respawn; mod bot; mod container; pub mod pathfinder; -- cgit v1.2.3