Skip to content

Commit

Permalink
fix: send other player flags on join
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Nov 3, 2024
1 parent c00e619 commit 2e0876b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
40 changes: 36 additions & 4 deletions crates/hyperion/src/egress/player_join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
net::{Compose, DataBundle, NetworkStreamRef},
simulation::{
command::{get_command_packet, Command, ROOT_COMMAND},
metadata::{EntityFlags, MetadataBuilder},
skin::PlayerSkin,
util::registry_codec_raw,
Comms, InGameName, Position, Uuid, Yaw,
Expand All @@ -59,7 +60,15 @@ pub fn player_join_world(
skin: &PlayerSkin,
system_id: SystemId,
root_command: Entity,
query: &Query<(&Uuid, &InGameName, &Position, &Yaw, &Pitch, &PlayerSkin)>,
query: &Query<(
&Uuid,
&InGameName,
&Position,
&Yaw,
&Pitch,
&PlayerSkin,
&EntityFlags,
)>,
crafting_registry: &CraftingRegistry,
config: &Config,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -184,7 +193,7 @@ pub fn player_join_world(
let _enter = scope.enter();
query
.iter_stage(world)
.each(|(uuid, name, _, _, _, _skin)| {
.each(|(uuid, name, _, _, _, _skin, _)| {
// todo: in future, do not clone

let entry = PlayerListEntry {
Expand Down Expand Up @@ -231,9 +240,11 @@ pub fn player_join_world(
// todo: could also be helped by denoting some packets as infallible for serialization
let mut query_errors = Vec::new();

let mut metadata = MetadataBuilder::default();

query
.iter_stage(world)
.each_iter(|it, idx, (uuid, _, position, yaw, pitch, _)| {
.each_iter(|it, idx, (uuid, _, position, yaw, pitch, _, flags)| {
let mut result = || {
let query_entity = it.entity(idx);

Expand All @@ -258,6 +269,19 @@ pub fn player_join_world(
.add_packet(show_all.borrow_packet(), world)
.context("failed to send player spawn packet")?;

metadata.encode(*flags);

if let Some(view) = metadata.get_and_clear() {
let pkt = play::EntityTrackerUpdateS2c {
entity_id: VarInt(query_entity.minecraft_id()),
tracked_values: RawBytes(&view),
};

bundle
.add_packet(&pkt, world)
.context("failed to send player spawn packet")?;
}

Ok(())
};

Expand Down Expand Up @@ -497,7 +521,15 @@ pub struct PlayerJoinModule;

impl Module for PlayerJoinModule {
fn module(world: &World) {
let query = world.new_query::<(&Uuid, &InGameName, &Position, &Yaw, &Pitch, &PlayerSkin)>();
let query = world.new_query::<(
&Uuid,
&InGameName,
&Position,
&Yaw,
&Pitch,
&PlayerSkin,
&EntityFlags,
)>();

let query = SendableQuery(query);

Expand Down
10 changes: 5 additions & 5 deletions crates/hyperion/src/egress/sync_entity_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
net::{agnostic, Compose, NetworkStreamRef},
simulation::{
animation::ActiveAnimation,
metadata::{ChangedMetadata, EntityFlags, Pose},
metadata::{MetadataBuilder, EntityFlags, Pose},
EntityReaction, Health, Pitch, Position, Yaw,
},
storage::ThreadLocal,
Expand All @@ -34,7 +34,7 @@ impl Module for EntityStateSyncModule {
fn module(world: &World) {
let system_id = SYNC_ENTITY_POSITION;

let metadata: ThreadLocal<UnsafeCell<ChangedMetadata>> = ThreadLocal::new_defaults();
let metadata: ThreadLocal<UnsafeCell<MetadataBuilder>> = ThreadLocal::new_defaults();

system!(
"entity_state_sync",
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Module for EntityStateSyncModule {
let pose_updated = *prev_pose != *pose;

if pose_updated {
observer.append(*pose);
observer.encode(*pose);
*prev_pose = *pose;
}

Expand All @@ -99,7 +99,7 @@ impl Module for EntityStateSyncModule {
let to = *health;
let from = *prev_health;

observer.append(*health);
observer.encode(*health);
*prev_health = *health;

if to < from {
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Module for EntityStateSyncModule {
let entity_flags_updated = *prev_entity_flags != *entity_flags;

if entity_flags_updated {
observer.append(*entity_flags);
observer.encode(*entity_flags);
*prev_entity_flags = *entity_flags;
}

Expand Down
12 changes: 6 additions & 6 deletions crates/hyperion/src/simulation/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use crate::simulation::metadata::r#type::MetadataType;
/// <https://wiki.vg/Entity_metadata>
///
/// Tracks updates within a gametick for the metadata
pub struct ChangedMetadata(Vec<u8>);
pub struct MetadataBuilder(Vec<u8>);

unsafe impl Send for ChangedMetadata {}
unsafe impl Send for MetadataBuilder {}

// technically not Sync but I mean do we really care? todo: Indra
unsafe impl Sync for ChangedMetadata {}
unsafe impl Sync for MetadataBuilder {}

mod status;

Expand Down Expand Up @@ -151,13 +151,13 @@ impl Metadata for Pose {
}
}

impl ChangedMetadata {
impl MetadataBuilder {
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn append<M: Metadata>(&mut self, metadata: M) {
pub fn encode<M: Metadata>(&mut self, metadata: M) {
let value_index = M::INDEX;
self.0.push(value_index);

Expand All @@ -180,7 +180,7 @@ impl ChangedMetadata {
}

#[derive(Debug)]
pub struct MetadataView<'a>(&'a mut ChangedMetadata);
pub struct MetadataView<'a>(&'a mut MetadataBuilder);

impl Deref for MetadataView<'_> {
type Target = [u8];
Expand Down

0 comments on commit 2e0876b

Please sign in to comment.