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

ReCentralize Packets #429

Merged
merged 12 commits into from
Aug 3, 2023
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ valence_network = { workspace = true, optional = true }
valence_player_list = { workspace = true, optional = true }
valence_registry.workspace = true
valence_world_border = { workspace = true, optional = true }
valence_packet.workspace = true
valence_weather = { workspace = true, optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -170,6 +171,7 @@ valence_block.path = "crates/valence_block"
valence_build_utils.path = "crates/valence_build_utils"
valence_client.path = "crates/valence_client"
valence_core_macros.path = "crates/valence_core_macros"
valence_packet_macros.path = "crates/valence_packet_macros"
valence_core.path = "crates/valence_core"
valence_dimension.path = "crates/valence_dimension"
valence_entity.path = "crates/valence_entity"
Expand All @@ -181,6 +183,7 @@ valence_player_list.path = "crates/valence_player_list"
valence_registry.path = "crates/valence_registry"
valence_world_border.path = "crates/valence_world_border"
valence_boss_bar.path = "crates/valence_boss_bar"
valence_packet.path = "crates/valence_packet"
valence_weather.path = "crates/valence_weather"
valence.path = "."
zip = "0.6.3"
3 changes: 1 addition & 2 deletions benches/many_players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ use rand::Rng;
use valence::testing::create_mock_client;
use valence::DefaultPlugins;
use valence_biome::BiomeRegistry;
use valence_client::hand_swing::HandSwingC2s;
use valence_client::keepalive::KeepaliveSettings;
use valence_client::movement::FullC2s;
use valence_core::chunk_pos::ChunkPos;
use valence_core::{ident, CoreSettings, Server};
use valence_dimension::DimensionTypeRegistry;
use valence_entity::Position;
use valence_layer::chunk::UnloadedChunk;
use valence_layer::LayerBundle;
use valence_network::NetworkPlugin;
use valence_packet::packets::play::{FullC2s, HandSwingC2s};

pub fn many_players(c: &mut Criterion) {
run_many_players(c, "many_players", 3000, 16, 16);
Expand Down
9 changes: 3 additions & 6 deletions benches/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ use std::hint::black_box;

use criterion::Criterion;
use valence::nbt::{compound, List};
use valence::packet::packets::play::{ChunkDataS2c, EntitySpawnS2c, PlayerListHeaderS2c};
use valence::packet::protocol::decode::PacketDecoder;
use valence::packet::protocol::encode::{PacketEncoder, PacketWriter, WritePacket};
use valence::prelude::*;
use valence::protocol::array::LengthPrefixedArray;
use valence::protocol::byte_angle::ByteAngle;
use valence::protocol::decode::PacketDecoder;
use valence::protocol::encode::PacketEncoder;
use valence::protocol::var_int::VarInt;
use valence::text::IntoText;
use valence_core::protocol::encode::{PacketWriter, WritePacket};
use valence_entity::packet::EntitySpawnS2c;
use valence_layer::packet::ChunkDataS2c;
use valence_player_list::packet::PlayerListHeaderS2c;

pub fn packet(c: &mut Criterion) {
let mut group = c.benchmark_group("packet");
Expand Down
1 change: 1 addition & 0 deletions crates/valence_advancement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition.workspace = true
[dependencies]
valence_core.workspace = true
valence_client.workspace = true
valence_packet.workspace = true
bevy_app.workspace = true
bevy_ecs.workspace = true
bevy_hierarchy.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions crates/valence_advancement/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bevy_ecs::prelude::*;
use valence_client::event_loop::PacketEvent;
use valence_core::ident::Ident;

use crate::packet::AdvancementTabC2s;
use valence_packet::packets::play::AdvancementTabC2s;

/// This event sends when the client changes or closes advancement's tab.
#[derive(Event, Clone, PartialEq, Eq, Debug)]
Expand Down
12 changes: 6 additions & 6 deletions crates/valence_advancement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![allow(clippy::type_complexity)]

pub mod event;
pub mod packet;

use std::borrow::Cow;
use std::io::Write;
Expand All @@ -14,16 +13,17 @@ use bevy_ecs::system::SystemParam;
pub use bevy_hierarchy;
use bevy_hierarchy::{Children, HierarchyPlugin, Parent};
use event::{handle_advancement_tab_change, AdvancementTabChangeEvent};
use packet::SelectAdvancementTabS2c;
use rustc_hash::FxHashMap;
use valence_client::{Client, FlushPacketsSet, SpawnClientsSet};
use valence_core::ident::Ident;
use valence_core::item::ItemStack;
use valence_core::protocol::encode::WritePacket;
use valence_core::protocol::raw::RawBytes;
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{packet_id, Encode, Packet, PacketSide, PacketState};
use valence_core::protocol::Encode;
use valence_core::text::Text;
use valence_packet::packets::play::{advancement_update_s2c as packet, SelectAdvancementTabS2c};
use valence_packet::protocol::encode::WritePacket;
use valence_packet::protocol::{packet_id, Packet, PacketSide, PacketState};

pub struct AdvancementPlugin;

Expand Down Expand Up @@ -54,8 +54,8 @@ impl Plugin for AdvancementPlugin {
.add_systems(
PostUpdate,
(
update_advancement_cached_bytes,
send_advancement_update_packet,
update_advancement_cached_bytes.in_set(WriteAdvancementToCacheSet),
send_advancement_update_packet.in_set(WriteAdvancementPacketToClientsSet),
),
);
}
Expand Down
1 change: 1 addition & 0 deletions crates/valence_boss_bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ valence_core.workspace = true
valence_network.workspace = true
valence_entity.workspace = true
valence_client.workspace = true
valence_packet.workspace = true
uuid.workspace = true
bitfield-struct.workspace = true
bevy_app.workspace = true
Expand Down
80 changes: 0 additions & 80 deletions crates/valence_boss_bar/src/components.rs
Original file line number Diff line number Diff line change
@@ -1,80 +0,0 @@
use std::collections::BTreeSet;

use bevy_ecs::prelude::{Bundle, Component, Entity};
use bitfield_struct::bitfield;
use valence_core::protocol::{Decode, Encode};
use valence_core::text::Text;
use valence_core::uuid::UniqueId;

/// The bundle of components that make up a boss bar.
#[derive(Bundle, Default)]
pub struct BossBarBundle {
pub id: UniqueId,
pub title: BossBarTitle,
pub health: BossBarHealth,
pub style: BossBarStyle,
pub flags: BossBarFlags,
pub viewers: BossBarViewers,
}

/// The title of a boss bar.
#[derive(Component, Clone, Default)]
pub struct BossBarTitle(pub Text);

/// The health of a boss bar.
#[derive(Component, Default)]
pub struct BossBarHealth(pub f32);

/// The style of a boss bar. This includes the color and division of the boss
/// bar.
#[derive(Component, Default)]
pub struct BossBarStyle {
pub color: BossBarColor,
pub division: BossBarDivision,
}

/// The color of a boss bar.
#[derive(Component, Copy, Clone, PartialEq, Eq, Default, Debug, Encode, Decode)]
pub enum BossBarColor {
#[default]
Pink,
Blue,
Red,
Green,
Yellow,
Purple,
White,
}

/// The division of a boss bar.
#[derive(Component, Copy, Clone, PartialEq, Eq, Default, Debug, Encode, Decode)]
pub enum BossBarDivision {
#[default]
NoDivision,
SixNotches,
TenNotches,
TwelveNotches,
TwentyNotches,
}

/// The flags of a boss bar (darken sky, dragon bar, create fog).
#[bitfield(u8)]
#[derive(Component, PartialEq, Eq, Default, Encode, Decode)]
pub struct BossBarFlags {
pub darken_sky: bool,
pub dragon_bar: bool,
pub create_fog: bool,
#[bits(5)]
_pad: u8,
}

/// The viewers of a boss bar.
#[derive(Component, Default)]
pub struct BossBarViewers {
/// The current viewers of the boss bar. It is the list that should be
/// updated.
pub viewers: BTreeSet<Entity>,
/// The viewers of the last tick in order to determine which viewers have
/// been added and removed.
pub(crate) old_viewers: BTreeSet<Entity>,
}
66 changes: 61 additions & 5 deletions crates/valence_boss_bar/src/lib.rs
Bafbi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,75 @@
)]

use std::borrow::Cow;
use std::collections::BTreeSet;

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use packet::{BossBarAction, BossBarS2c};
use valence_client::{Client, FlushPacketsSet};
use valence_core::despawn::Despawned;
use valence_core::protocol::encode::WritePacket;
use valence_core::text::Text;
use valence_core::uuid::UniqueId;
pub use valence_packet::packets::play::boss_bar_s2c::{
BossBarAction, BossBarColor, BossBarDivision, BossBarFlags,
};
use valence_packet::packets::play::BossBarS2c;
use valence_packet::protocol::encode::WritePacket;

mod components;
pub use components::*;
/// The bundle of components that make up a boss bar.
#[derive(Bundle, Debug, Default)]
pub struct BossBarBundle {
pub id: UniqueId,
pub title: BossBarTitle,
pub health: BossBarHealth,
pub style: BossBarStyle,
pub flags: BossBarFlags,
pub viewers: BossBarViewers,
}

impl BossBarBundle {
pub fn new(
title: Text,
color: BossBarColor,
division: BossBarDivision,
flags: BossBarFlags,
) -> BossBarBundle {
BossBarBundle {
id: UniqueId::default(),
title: BossBarTitle(title),
health: BossBarHealth(1.0),
style: BossBarStyle { color, division },
flags,
viewers: BossBarViewers::default(),
}
}
}

pub mod packet;
/// The title of a boss bar.
#[derive(Component, Clone, Debug, Default)]
pub struct BossBarTitle(pub Text);

/// The health of a boss bar.
#[derive(Component, Debug, Default)]
pub struct BossBarHealth(pub f32);

/// The style of a boss bar. This includes the color and division of the boss
/// bar.
#[derive(Component, Debug, Default)]
pub struct BossBarStyle {
pub color: BossBarColor,
pub division: BossBarDivision,
}

/// The viewers of a boss bar.
#[derive(Component, Default, Debug)]
pub struct BossBarViewers {
/// The current viewers of the boss bar. It is the list that should be
/// updated.
pub viewers: BTreeSet<Entity>,
/// The viewers of the last tick in order to determine which viewers have
/// been added and removed.
pub(crate) old_viewers: BTreeSet<Entity>,
}

pub struct BossBarPlugin;

Expand Down
30 changes: 0 additions & 30 deletions crates/valence_boss_bar/src/packet.rs

This file was deleted.

1 change: 1 addition & 0 deletions crates/valence_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ valence_biome.workspace = true
valence_core.workspace = true
valence_dimension.workspace = true
valence_entity.workspace = true
valence_packet.workspace = true
valence_layer.workspace = true
valence_nbt.workspace = true
valence_registry.workspace = true
10 changes: 2 additions & 8 deletions crates/valence_client/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use valence_core::block_pos::BlockPos;
use valence_core::direction::Direction;
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{packet_id, Decode, Encode, Packet};
use valence_packet::packets::play::player_action_c2s::PlayerAction;
use valence_packet::packets::play::{PlayerActionC2s, PlayerActionResponseS2c};

use super::*;
use crate::event_loop::{EventLoopPreUpdate, PacketEvent};
use crate::packet::{PlayerAction, PlayerActionC2s};

pub(super) fn build(app: &mut App) {
app.add_event::<DiggingEvent>()
Expand Down Expand Up @@ -99,9 +99,3 @@ fn acknowledge_player_actions(
}
}
}

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::PLAYER_ACTION_RESPONSE_S2C)]
pub struct PlayerActionResponseS2c {
pub sequence: VarInt,
}
25 changes: 2 additions & 23 deletions crates/valence_client/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use valence_core::protocol::var_int::VarInt;
use valence_core::protocol::{packet_id, Decode, Encode, Packet};
use valence_entity::entity::Flags;
use valence_entity::{entity, Pose};
pub use valence_packet::packets::play::client_command_c2s::ClientCommand;
use valence_packet::packets::play::ClientCommandC2s;

use crate::event_loop::{EventLoopPreUpdate, PacketEvent};

Expand Down Expand Up @@ -141,24 +141,3 @@ fn handle_client_command(
}
}
}

#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
#[packet(id = packet_id::CLIENT_COMMAND_C2S)]
pub struct ClientCommandC2s {
pub entity_id: VarInt,
pub action: ClientCommand,
pub jump_boost: VarInt,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub enum ClientCommand {
StartSneaking,
StopSneaking,
LeaveBed,
StartSprinting,
StopSprinting,
StartJumpWithHorse,
StopJumpWithHorse,
OpenHorseInventory,
StartFlyingWithElytra,
}
Loading
Loading