mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
add support for tickend and playerinput
This commit is contained in:
parent
befcec9b3a
commit
6d8d937d47
6 changed files with 84 additions and 4 deletions
|
@ -78,6 +78,7 @@ use crate::{
|
||||||
player::retroactively_add_game_profile_component,
|
player::retroactively_add_game_profile_component,
|
||||||
raw_connection::RawConnection,
|
raw_connection::RawConnection,
|
||||||
respawn::RespawnPlugin,
|
respawn::RespawnPlugin,
|
||||||
|
send_client_end::TickEndPlugin,
|
||||||
task_pool::TaskPoolPlugin,
|
task_pool::TaskPoolPlugin,
|
||||||
Account, PlayerInfo,
|
Account, PlayerInfo,
|
||||||
};
|
};
|
||||||
|
@ -927,6 +928,7 @@ impl PluginGroup for DefaultPlugins {
|
||||||
.add(MinePlugin)
|
.add(MinePlugin)
|
||||||
.add(AttackPlugin)
|
.add(AttackPlugin)
|
||||||
.add(ChunkPlugin)
|
.add(ChunkPlugin)
|
||||||
|
.add(TickEndPlugin)
|
||||||
.add(ConfigurationPlugin)
|
.add(ConfigurationPlugin)
|
||||||
.add(TickBroadcastPlugin);
|
.add(TickBroadcastPlugin);
|
||||||
#[cfg(feature = "log")]
|
#[cfg(feature = "log")]
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub mod ping;
|
||||||
mod player;
|
mod player;
|
||||||
pub mod raw_connection;
|
pub mod raw_connection;
|
||||||
pub mod respawn;
|
pub mod respawn;
|
||||||
|
pub mod send_client_end;
|
||||||
pub mod task_pool;
|
pub mod task_pool;
|
||||||
|
|
||||||
pub use account::{Account, AccountOpts};
|
pub use account::{Account, AccountOpts};
|
||||||
|
|
|
@ -5,7 +5,7 @@ use azalea_core::tick::GameTick;
|
||||||
use azalea_entity::{metadata::Sprinting, Attributes, Jumping};
|
use azalea_entity::{metadata::Sprinting, Attributes, Jumping};
|
||||||
use azalea_entity::{InLoadedChunk, LastSentPosition, LookDirection, Physics, Position};
|
use azalea_entity::{InLoadedChunk, LastSentPosition, LookDirection, Physics, Position};
|
||||||
use azalea_physics::{ai_step, PhysicsSet};
|
use azalea_physics::{ai_step, PhysicsSet};
|
||||||
use azalea_protocol::packets::game::ServerboundPlayerCommand;
|
use azalea_protocol::packets::game::{ServerboundPlayerCommand, ServerboundPlayerInput};
|
||||||
use azalea_protocol::packets::{
|
use azalea_protocol::packets::{
|
||||||
game::{
|
game::{
|
||||||
s_move_player_pos::ServerboundMovePlayerPos,
|
s_move_player_pos::ServerboundMovePlayerPos,
|
||||||
|
@ -19,6 +19,7 @@ use azalea_world::{MinecraftEntityId, MoveEntityError};
|
||||||
use bevy_app::{App, Plugin, Update};
|
use bevy_app::{App, Plugin, Update};
|
||||||
use bevy_ecs::prelude::{Event, EventWriter};
|
use bevy_ecs::prelude::{Event, EventWriter};
|
||||||
use bevy_ecs::schedule::SystemSet;
|
use bevy_ecs::schedule::SystemSet;
|
||||||
|
use bevy_ecs::system::Commands;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
component::Component, entity::Entity, event::EventReader, query::With,
|
component::Component, entity::Entity, event::EventReader, query::With,
|
||||||
schedule::IntoSystemConfigs, system::Query,
|
schedule::IntoSystemConfigs, system::Query,
|
||||||
|
@ -68,6 +69,7 @@ impl Plugin for PlayerMovePlugin {
|
||||||
.before(ai_step)
|
.before(ai_step)
|
||||||
.before(azalea_physics::fluids::update_in_water_state_and_do_fluid_pushing),
|
.before(azalea_physics::fluids::update_in_water_state_and_do_fluid_pushing),
|
||||||
send_sprinting_if_needed.after(azalea_entity::update_in_loaded_chunk),
|
send_sprinting_if_needed.after(azalea_entity::update_in_loaded_chunk),
|
||||||
|
send_player_input_packet,
|
||||||
send_position.after(PhysicsSet),
|
send_position.after(PhysicsSet),
|
||||||
)
|
)
|
||||||
.chain(),
|
.chain(),
|
||||||
|
@ -251,6 +253,40 @@ pub fn send_position(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Component, Clone, PartialEq, Eq)]
|
||||||
|
pub struct LastSentInput(pub ServerboundPlayerInput);
|
||||||
|
pub fn send_player_input_packet(
|
||||||
|
mut query: Query<(Entity, &PhysicsState, &Jumping, Option<&LastSentInput>)>,
|
||||||
|
mut send_packet_events: EventWriter<SendPacketEvent>,
|
||||||
|
mut commands: Commands,
|
||||||
|
) {
|
||||||
|
for (entity, physics_state, jumping, last_sent_input) in query.iter_mut() {
|
||||||
|
let input = ServerboundPlayerInput {
|
||||||
|
forward: physics_state.move_direction == WalkDirection::Forward,
|
||||||
|
backward: physics_state.move_direction == WalkDirection::Backward,
|
||||||
|
left: physics_state.move_direction == WalkDirection::Left,
|
||||||
|
right: physics_state.move_direction == WalkDirection::Right,
|
||||||
|
jump: **jumping,
|
||||||
|
// TODO: implement sneaking
|
||||||
|
shift: false,
|
||||||
|
sprint: physics_state.trying_to_sprint,
|
||||||
|
};
|
||||||
|
|
||||||
|
// if LastSentInput isn't present, we default to assuming we're not pressing any
|
||||||
|
// keys and insert it anyways every time it changes
|
||||||
|
let last_sent_input = last_sent_input.cloned().unwrap_or_default();
|
||||||
|
|
||||||
|
if input != last_sent_input.0 {
|
||||||
|
send_packet_events.send(SendPacketEvent {
|
||||||
|
sent_by: entity,
|
||||||
|
packet: input.clone().into_variant(),
|
||||||
|
});
|
||||||
|
println!("sent input packet {:?}", input);
|
||||||
|
commands.entity(entity).insert(LastSentInput(input));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn send_sprinting_if_needed(
|
fn send_sprinting_if_needed(
|
||||||
mut query: Query<(Entity, &MinecraftEntityId, &Sprinting, &mut PhysicsState)>,
|
mut query: Query<(Entity, &MinecraftEntityId, &Sprinting, &mut PhysicsState)>,
|
||||||
mut send_packet_events: EventWriter<SendPacketEvent>,
|
mut send_packet_events: EventWriter<SendPacketEvent>,
|
||||||
|
@ -510,7 +546,7 @@ pub fn handle_knockback(mut query: Query<&mut Physics>, mut events: EventReader<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
pub enum WalkDirection {
|
pub enum WalkDirection {
|
||||||
#[default]
|
#[default]
|
||||||
None,
|
None,
|
||||||
|
|
36
azalea-client/src/send_client_end.rs
Normal file
36
azalea-client/src/send_client_end.rs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
//! Clients send a [`ServerboundClientTickEnd`] packet every tick.
|
||||||
|
|
||||||
|
use azalea_core::tick::GameTick;
|
||||||
|
use azalea_entity::LocalEntity;
|
||||||
|
use azalea_physics::PhysicsSet;
|
||||||
|
use azalea_protocol::packets::game::ServerboundClientTickEnd;
|
||||||
|
use azalea_world::InstanceName;
|
||||||
|
use bevy_app::{App, Plugin};
|
||||||
|
use bevy_ecs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{mining::MiningSet, packet_handling::game::SendPacketEvent};
|
||||||
|
|
||||||
|
/// A plugin that makes clients send a [`ServerboundClientTickEnd`] packet every
|
||||||
|
/// tick.
|
||||||
|
pub struct TickEndPlugin;
|
||||||
|
impl Plugin for TickEndPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(
|
||||||
|
GameTick,
|
||||||
|
// this has to happen after every other event that might send packets
|
||||||
|
game_tick_packet
|
||||||
|
.after(PhysicsSet)
|
||||||
|
.after(MiningSet)
|
||||||
|
.after(crate::movement::send_position),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn game_tick_packet(
|
||||||
|
query: Query<Entity, (With<LocalEntity>, With<InstanceName>)>,
|
||||||
|
mut send_packets: EventWriter<SendPacketEvent>,
|
||||||
|
) {
|
||||||
|
for entity in query.iter() {
|
||||||
|
send_packets.send(SendPacketEvent::new(entity, ServerboundClientTickEnd));
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ use azalea_buf::{AzaleaRead, AzaleaWrite};
|
||||||
use azalea_core::bitset::FixedBitSet;
|
use azalea_core::bitset::FixedBitSet;
|
||||||
use azalea_protocol_macros::ServerboundGamePacket;
|
use azalea_protocol_macros::ServerboundGamePacket;
|
||||||
|
|
||||||
#[derive(Clone, Debug, ServerboundGamePacket)]
|
#[derive(Clone, Debug, Default, PartialEq, Eq, ServerboundGamePacket)]
|
||||||
pub struct ServerboundPlayerInput {
|
pub struct ServerboundPlayerInput {
|
||||||
pub forward: bool,
|
pub forward: bool,
|
||||||
pub backward: bool,
|
pub backward: bool,
|
||||||
|
|
|
@ -43,7 +43,12 @@ impl Plugin for BotPlugin {
|
||||||
jump_listener,
|
jump_listener,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.add_systems(GameTick, stop_jumping.after(PhysicsSet));
|
.add_systems(
|
||||||
|
GameTick,
|
||||||
|
stop_jumping
|
||||||
|
.after(PhysicsSet)
|
||||||
|
.after(azalea_client::movement::send_player_input_packet),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue