Fix player bugs (#318)

## Description

- Make sure player list packets get sent before player entity spawn
packets are sent to prevent invisible players.
- Correctly update pose and hand swing.

## Test Plan

Steps:
1. Load any example with two players.
2. See that players are visible.
3. See that crouching and hand swings are functioning.
This commit is contained in:
Ryan Johnson 2023-04-10 03:15:27 -07:00 committed by GitHub
parent d78627e478
commit f2bf1d97dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 deletions

View file

@ -4,6 +4,7 @@ use valence_protocol::packet::c2s::play::client_command::Action;
use valence_protocol::packet::c2s::play::ClientCommandC2s;
use crate::entity::entity::Flags;
use crate::entity::{entity, Pose};
use crate::event_loop::{EventLoopSchedule, EventLoopSet, PacketEvent};
pub(super) fn build(app: &mut App) {
@ -64,7 +65,7 @@ pub struct LeaveBed {
fn handle_client_command(
mut packets: EventReader<PacketEvent>,
mut clients: Query<&mut Flags>,
mut clients: Query<(&mut entity::Pose, &mut Flags)>,
mut sprinting_events: EventWriter<Sprinting>,
mut sneaking_events: EventWriter<Sneaking>,
mut jump_with_horse_events: EventWriter<JumpWithHorse>,
@ -74,7 +75,8 @@ fn handle_client_command(
if let Some(pkt) = packet.decode::<ClientCommandC2s>() {
match pkt.action {
Action::StartSneaking => {
if let Ok(mut flags) = clients.get_mut(packet.client) {
if let Ok((mut pose, mut flags)) = clients.get_mut(packet.client) {
pose.0 = Pose::Sneaking;
flags.set_sneaking(true);
}
@ -84,7 +86,8 @@ fn handle_client_command(
})
}
Action::StopSneaking => {
if let Ok(mut flags) = clients.get_mut(packet.client) {
if let Ok((mut pose, mut flags)) = clients.get_mut(packet.client) {
pose.0 = Pose::Standing;
flags.set_sneaking(false);
}
@ -97,7 +100,7 @@ fn handle_client_command(
client: packet.client,
}),
Action::StartSprinting => {
if let Ok(mut flags) = clients.get_mut(packet.client) {
if let Ok((_, mut flags)) = clients.get_mut(packet.client) {
flags.set_sprinting(true);
}
@ -107,7 +110,7 @@ fn handle_client_command(
});
}
Action::StopSprinting => {
if let Ok(mut flags) = clients.get_mut(packet.client) {
if let Ok((_, mut flags)) = clients.get_mut(packet.client) {
flags.set_sprinting(false);
}
@ -126,8 +129,14 @@ fn handle_client_command(
client: packet.client,
state: JumpWithHorseState::Stop,
}),
Action::OpenHorseInventory => {} // TODO
Action::StartFlyingWithElytra => {} // TODO
Action::OpenHorseInventory => {} // TODO
Action::StartFlyingWithElytra => {
if let Ok((mut pose, _)) = clients.get_mut(packet.client) {
pose.0 = Pose::FallFlying;
}
// TODO.
}
}
}
}

View file

@ -9,6 +9,7 @@ use valence_protocol::packet::c2s::play::{
use valence_protocol::types::{Direction, Hand};
use super::action::ActionSequence;
use crate::entity::{EntityAnimation, EntityAnimations};
use crate::event_loop::{EventLoopSchedule, EventLoopSet, PacketEvent};
pub(super) fn build(app: &mut App) {
@ -86,7 +87,7 @@ pub struct ResourcePackStatusChange {
#[allow(clippy::too_many_arguments)]
fn handle_misc_packets(
mut packets: EventReader<PacketEvent>,
mut clients: Query<&mut ActionSequence>,
mut clients: Query<(&mut ActionSequence, &mut EntityAnimations)>,
mut hand_swing_events: EventWriter<HandSwing>,
mut interact_block_events: EventWriter<InteractBlock>,
mut chat_message_events: EventWriter<ChatMessage>,
@ -96,12 +97,19 @@ fn handle_misc_packets(
) {
for packet in packets.iter() {
if let Some(pkt) = packet.decode::<HandSwingC2s>() {
if let Ok((_, mut animations)) = clients.get_mut(packet.client) {
animations.trigger(match pkt.hand {
Hand::Main => EntityAnimation::SwingMainHand,
Hand::Off => EntityAnimation::SwingOffHand,
});
}
hand_swing_events.send(HandSwing {
client: packet.client,
hand: pkt.hand,
});
} else if let Some(pkt) = packet.decode::<PlayerInteractBlockC2s>() {
if let Ok(mut action_seq) = clients.get_mut(packet.client) {
if let Ok((mut action_seq, _)) = clients.get_mut(packet.client) {
action_seq.update(pkt.sequence.0);
}
@ -115,7 +123,7 @@ fn handle_misc_packets(
sequence: pkt.sequence.0,
});
} else if let Some(pkt) = packet.decode::<PlayerInteractItemC2s>() {
if let Ok(mut action_seq) = clients.get_mut(packet.client) {
if let Ok((mut action_seq, _)) = clients.get_mut(packet.client) {
action_seq.update(pkt.sequence.0);
}

View file

@ -7,8 +7,9 @@ use valence_protocol::packet::s2c::play::player_list::{Actions, Entry, PlayerLis
use valence_protocol::packet::s2c::play::{PlayerListHeaderS2c, PlayerRemoveS2c};
use valence_protocol::text::Text;
use crate::client::{Client, FlushPacketsSet};
use crate::client::Client;
use crate::component::{Despawned, GameMode, Ping, Properties, UniqueId, Username};
use crate::instance::WriteUpdatePacketsToInstancesSet;
use crate::packet::{PacketWriter, WritePacket};
use crate::server::Server;
@ -27,7 +28,7 @@ impl Plugin for PlayerListPlugin {
write_player_list_changes,
)
.chain()
.before(FlushPacketsSet)
.before(WriteUpdatePacketsToInstancesSet)
.in_base_set(CoreSet::PostUpdate),
);
}