Apply pending player attribute callbacks during resident simulation

Refresh scale dimensions and clamp health and absorption after movement, preserving native callback order and queued synchronization. Replay per-reference JVM identities in the scene comparison and cover changing limits, modifiers and low tunnels. Waypoint callbacks remain explicit requests for the future manager runtime.
This commit is contained in:
Ludovic Marie
2026-10-07 18:27:58 +02:00
parent 4e01780255
commit 082cbb4077
11 changed files with 2703 additions and 22 deletions
@@ -0,0 +1,142 @@
//! Resident ServerPlayer pending attribute callbacks over its owned live map.
//! Waypoint requests are observable boundaries; manager/network delivery is
//! still supplied by the eventual multiplayer runtime.
use crate::{
Result,
health::Updates,
motion::{MotionEvent, PlayerMotion},
};
use horizon_world::{
attribute_map::ValueKind,
attribute_updates::{self, UpdateWorld, UpdatedAttribute},
attributes::live::{self, Handle, Key, LiveAttributes},
live_attribute_updates::{self as live_updates, LiveAttributeWorld},
living_health::{self, HealthWorld},
object_open_set::CursorError,
};
use std::io;
impl PlayerMotion {
/// LivingEntity.tick refreshes these callbacks after aiStep and before the
/// enclosing Player.tick selects its pose. Synchronization stays queued.
pub fn refresh_attributes(&mut self) -> Result<()> {
live_updates::refresh(self)
}
}
impl HealthWorld for PlayerMotion {
type Error = Box<dyn std::error::Error>;
type Data = ();
fn health(&mut self) -> Result<f32> {
Ok(self.health.health())
}
fn health_data(&self) {}
fn max_health_attribute(&mut self) -> Result<f64> {
self.attribute("minecraft:max_health")
}
fn max_absorption_attribute(&mut self) -> Result<f64> {
self.attribute("minecraft:max_absorption")
}
fn store_health(&mut self, _: (), value: f32) -> Result<()> {
self.health.data.set(
self.health.ids.health,
value,
false,
&mut Updates(&mut self.events),
)?;
Ok(())
}
fn store_absorption(&mut self, value: f32) -> Result<()> {
// Player.internalSetAbsorptionAmount stores the already clamped value
// through its current synchronized data, without another limit query.
self.health.data.set(
self.health.ids.absorption,
value,
false,
&mut Updates(&mut self.events),
)?;
Ok(())
}
fn dead_flag(&self) -> bool {
self.health.dead
}
fn dispatch_set_health(&mut self, value: f32) -> Result<()> {
living_health::set_health(self, value)
}
}
impl UpdateWorld<Key> for PlayerMotion {
type Level = ();
type Manager = ();
type Attributes = ();
fn attribute_kind(&self, attribute: &Key) -> UpdatedAttribute {
live_updates::kind(attribute)
}
fn absorption(&mut self) -> Result<f32> {
Ok(self.health.absorption())
}
fn refresh_dimensions(&mut self) -> Result<()> {
PlayerMotion::refresh_dimensions(self)
}
fn level(&mut self) -> Result<()> {
Ok(())
}
fn is_server_level(&self, _: ()) -> bool {
true
}
fn waypoint_manager(&mut self, _: ()) -> Result<()> {
Ok(())
}
fn attributes_field(&self) {}
fn attribute_value_in(&mut self, _: (), attribute: Key) -> Result<f64> {
self.attributes
.value(Some(attribute), ValueKind::Computed)
.map_err(Self::attribute_error)
}
fn track_waypoint(&mut self, _: (), track: bool) -> Result<()> {
self.events.push(MotionEvent::Waypoint {
receiver: false,
enabled: track,
});
Ok(())
}
}
impl LiveAttributeWorld for PlayerMotion {
type Identity = Box<dyn FnMut() -> i32>;
fn get_attributes(&mut self) -> Result<()> {
Ok(())
}
fn live_attributes(&self, _: ()) -> &LiveAttributes<Self::Identity> {
&self.attributes
}
fn live_attributes_mut(&mut self, _: ()) -> &mut LiveAttributes<Self::Identity> {
&mut self.attributes
}
fn instance_attribute(&self, instance: Option<Handle>) -> Result<Key> {
let instance =
instance.ok_or_else(|| io::Error::other("null pending attribute instance"))?;
self.attributes
.attribute(&instance)
.cloned()
.map_err(Self::attribute_error)
}
fn attribute_error(error: live::Error) -> Self::Error {
io::Error::other(format!("resident attribute: {error:?}")).into()
}
fn cursor_error(error: CursorError) -> Self::Error {
io::Error::other(format!("resident attribute cursor: {error:?}")).into()
}
fn dispatch_attribute_updated(&mut self, attribute: Key) -> Result<()> {
// ServerPlayer.onAttributeUpdated handles receive range, then calls
// the inherited LivingEntity handler, including for this same holder.
if attribute.name() == "minecraft:waypoint_receive_range" {
let value = self
.attributes
.value(Some(attribute.clone()), ValueKind::Computed)
.map_err(Self::attribute_error)?;
self.events.push(MotionEvent::Waypoint {
receiver: true,
enabled: value > 0.0,
});
}
attribute_updates::updated(self, attribute)
}
}
+1
View File
@@ -1,6 +1,7 @@
//! Shared resident-scene loading and queries for the native development client.
#![forbid(unsafe_code)]
pub mod assets;
pub mod attribute_updates;
pub mod clock;
pub mod collisions;
pub mod health;
+17 -3
View File
@@ -98,6 +98,10 @@ impl MotionResources {
/// hurt dispatch updates stored health synchronously before these are drained.
#[derive(Clone, Copy, Debug)]
pub enum MotionEvent {
Waypoint {
receiver: bool,
enabled: bool,
},
Health(crate::health::HealthEvent),
Fall(crate::landing::FallEvent),
Recorded {
@@ -154,8 +158,8 @@ fn attribute_error(error: impl std::fmt::Debug) -> io::Error {
}
impl PlayerMotion {
pub fn new(resources: &MotionResources, position: Vec3) -> Result<Self> {
// Identity hashes affect map order, not queried movement values. These
// are process-owned identities; no JVM allocation order is assumed.
// Process-owned identities determine native collection callback order.
// Differential fixtures can replay a reference process's identities.
let hashes: Vec<_> = resources
.attributes
.attributes()
@@ -167,10 +171,19 @@ impl PlayerMotion {
identity = identity.wrapping_add(1);
identity
});
Self::with_attribute_identities(resources, position, &hashes, next)
}
/// Explicit identity allocation for deterministic reference replay.
pub fn with_attribute_identities(
resources: &MotionResources,
position: Vec3,
hashes: &[i32],
next: Box<dyn FnMut() -> i32>,
) -> Result<Self> {
let mut attributes = LiveAttributes::new(
resources.attributes.clone(),
"minecraft:player",
&hashes,
hashes,
next,
)
.map_err(attribute_error)?;
@@ -393,6 +406,7 @@ impl MotionWorld<'_, '_, '_> {
self.player.health.advance_clock();
let previous = self.player.position;
self.player_movement_tick(direction, jumping)?;
self.player.refresh_attributes()?;
self.update_player_pose()?;
// The resident scene accepts the resolved local movement directly.
// Vanilla dispatches this separately from travel, on packet acceptance.