Merge pull request #2206 from socksprox/fix/piston-moving-block-anim

🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `ServerLevel.runBlockEvents` broadcasts each handled block event, and `ClientLevel.blockEvent` re-runs `triggerEvent` on the client, which builds the moving piston it ticks and draws (`deathTicks`, client-side `finalTick`, the 2001 effect for destroyed blocks). I pushed one fix with a test. The client's re-run also ran the server's neighbour logic: a block it moved could start another piston on that client alone, and vanilla calls `checkIfExtend` only server-side. Its `playSound(null, ...)` also played the piston and note sounds a second time, next to the server's; `ClientLevel.playSeededSound` plays only when the excluded player is the local one. In game on the server thread, a piston that pushes a redstone block under a second piston leaves both clients' blocks as the server has them. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @socksprox!
This commit is contained in:
Ludovic Marie
2026-10-26 18:39:19 +01:00
committed by GitHub
6 changed files with 322 additions and 40 deletions
+17 -1
View File
@@ -565,6 +565,9 @@ struct GameLevel<'w> {
random_tick_sections: &'w mut horizon_client::random_tick_sections::RandomTickSections,
/// Players with each chest open (a trapped chest's signal).
lid_openers: &'w HashMap<(i32, i32, i32), i32>,
/// `Level.isClientSide`: a client's copy of the level (a guest of a LAN
/// world, a window on its server thread, a dedicated server's client).
client_side: bool,
}
struct PlayerCollision {
y: f64,
@@ -654,6 +657,9 @@ impl GameLevel<'_> {
}
}
impl LevelAccess for GameLevel<'_> {
fn is_client_side(&self) -> bool {
self.client_side
}
fn world_surface(&self, x: i32, z: i32) -> i32 {
self.world.world_surface(x, z, |s| self.facts.is_air(s))
}
@@ -857,7 +863,16 @@ impl LevelAccess for GameLevel<'_> {
fn emit(&mut self, event: LevelEvent) {
match event {
// Block.dropResources draws its loot and spots right away.
LevelEvent::DropResources { pos, state } => self.drop_resources_now(pos, state),
// `popResource` only drops server-side (`!level.isClientSide`):
// a client's moveBlocks (a piston's block event) skips it.
LevelEvent::DropResources { pos, state } if !self.client_side => {
self.drop_resources_now(pos, state)
}
LevelEvent::DropResources { .. } => {}
// `ClientLevel.playSeededSound` plays only when the excluded
// player is this client's own: the block's `playSound(null, ...)`
// (a piston's move, a note) reaches it from the server instead.
LevelEvent::Sound { .. } if self.client_side => {}
// Block.popResource of the block's own item (tall flowers, flower beds).
LevelEvent::BlockEvent {
pos,
@@ -2505,6 +2520,7 @@ impl Game<'_> {
tickets: self.tickets.levels(),
random_tick_sections: &mut self.random_tick_sections,
lid_openers: &self.lid_openers,
client_side: self.remote.is_some(),
};
let result = f(self.res.behaviours(), &mut level, &mut self.block_ticks);
self.flush_spill();
+30 -11
View File
@@ -944,6 +944,13 @@ impl Game<'_> {
for m in &mut self.mobs {
m.needs_sync = false;
}
// `runBlockEvents`' broadcast precedes the tick's block changes
// (`ChunkHolder.broadcastChanges` in `tick_level` below): the
// guest re-runs a piston's `triggerEvent` while its level still
// has the blocks as they were before the move.
for (pos, b0, b1, state) in std::mem::take(&mut self.lan_block_events) {
host.hub.block_event(pos, b0, b1, state);
}
let entities = states.as_slice();
let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]);
let events = {
@@ -1177,17 +1184,29 @@ impl Game<'_> {
return;
}
for e in events {
if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = e {
// The acting guest does not predict the egg's teleport
// particles, bone meal's growth, or anvil result sounds
// (these levelEvent calls name no excluded player).
let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) {
None
} else {
self.lan_acting
};
self.lan_events
.push((*id, [pos.x, pos.y, pos.z], *data, except));
match e {
horizon_world::level::LevelEvent::LevelEvent { id, pos, data } => {
// The acting guest does not predict the egg's teleport
// particles, bone meal's growth, or anvil result sounds
// (these levelEvent calls name no excluded player).
let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) {
None
} else {
self.lan_acting
};
self.lan_events
.push((*id, [pos.x, pos.y, pos.z], *data, except));
}
// `ServerLevel.runBlockEvents` broadcasts a handled block
// event (`ClientboundBlockEventPacket`): the guest re-runs
// the block's `triggerEvent` — a piston's `moveBlocks` is
// what animates the move there. `state` is the block the
// event named, read before it ran (the packet's `block`).
horizon_world::level::LevelEvent::BlockEntityEvent { pos, state, a, b } => {
self.lan_block_events
.push(([pos.x, pos.y, pos.z], *a, *b, state.0));
}
_ => {}
}
}
}
+29 -12
View File
@@ -1697,19 +1697,30 @@ impl Game<'_> {
}
}
C::BlockChangedAck(p) => self.end_predictions(p.sequence),
// ClientLevel.blockEvent: BlockEntity.triggerEvent (chest lids,
// shulker boxes, bells) when the block is still the one it names.
// Level.blockEvent on the client level: the block's triggerEvent
// runs on the client's copy of whatever is at pos (a piston's
// moveBlocks builds the moving block entities it animates, a note
// block plays); what it doesn't handle reaches the client copy of
// the block entity (chest lids, shulker boxes).
C::BlockEvent(p) => {
let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]);
let block = &self.res.facts().get(state).name;
let named = self.remote.as_ref().and_then(|r| {
r.builtin
.get("minecraft:block")?
.name(p.block)
.map(str::to_owned)
let pos = horizon_core::position::BlockPos::new(p.pos[0], p.pos[1], p.pos[2]);
let triggered = self.with_level(|b, level, ticks| {
b.client_block_event(level, ticks, pos, i32::from(p.b0), i32::from(p.b1))
});
if named.as_deref() == Some(&**block) {
self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1));
self.sync_moving_pistons();
self.process_level_events()?;
if !triggered {
let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]);
let block = &self.res.facts().get(state).name;
let named = self.remote.as_ref().and_then(|r| {
r.builtin
.get("minecraft:block")?
.name(p.block)
.map(str::to_owned)
});
if named.as_deref() == Some(&**block) {
self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1));
}
}
}
// ClientLevel.levelEvent: LevelEventHandler, as this client's own
@@ -3162,8 +3173,14 @@ impl Game<'_> {
self.border.tick();
self.player_border();
self.tick_remote_entities(running);
// ClientLevel.tickBlockEntities: chest lids, shulker boxes, bells.
// ClientLevel.tickBlockEntities: chest lids, shulker boxes, bells,
// and the moving pistons a block event's client-side moveBlocks made
// (their progress advances between the server's block updates).
self.tick_block_entity_anims();
self.with_level(|b, level, ticks| {
b.client_tick_block_entities(level, ticks);
});
self.sync_moving_pistons();
self.tick_hand();
self.tick_particles();
self.tick_client_ambient();
+5
View File
@@ -288,6 +288,11 @@ pub enum LevelEvent {
/// The world as seen by block behaviour. Implemented by the game and tests.
pub trait LevelAccess {
/// `Level.isClientSide`: this level is a client's copy (false on a
/// server, a host or the test harness).
fn is_client_side(&self) -> bool {
false
}
/// `getBlockState`. Unloaded or out-of-bounds positions read as air.
fn block_state(&self, pos: BlockPos) -> BlockStateId;
/// Storage write (`LevelChunkSection.setBlockState`). Returns the previous
+141 -16
View File
@@ -27,6 +27,9 @@ pub struct MovingPiston {
pub progress: f32,
pub progress_o: f32,
pub last_ticked: i64,
/// `PistonMovingBlockEntity.deathTicks`: on a client the entity waits
/// five ticks past progress 1.0 before placing the block itself.
pub death_ticks: i32,
/// Identity of this block entity (a replaced entity is a new ticker).
pub id: u64,
}
@@ -126,11 +129,8 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
self.note_trigger_event(pos, state);
true
} else if data.is(super::classes::Class::Bell) {
// BellBlockEntity.triggerEvent handles event 1; the server
// broadcasts handled events to the clients.
if a == 1 {
self.emit(LevelEvent::BlockEntityEvent { pos, state, a, b });
}
// BellBlockEntity.triggerEvent handles event 1; the caller
// reports handled events (`runBlockEvents` broadcasts them).
a == 1
} else {
false
@@ -164,7 +164,9 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
let Some(piston) = self.moving_piston(pos) else {
return;
};
if piston.progress_o >= 1.0 {
// finalTick: `progressO < 1.0 || level.isClientSide` — a client
// finishes its copy even at full progress.
if piston.progress_o >= 1.0 && !self.level.is_client_side() {
return;
}
self.remove_moving_piston(pos);
@@ -189,6 +191,20 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
piston.last_ticked = self.level.game_time();
piston.progress_o = piston.progress;
if piston.progress_o >= 1.0 {
if self.level.is_client_side() && piston.death_ticks < 5 {
// The client's copy waits for the server's state before
// placing the block itself (deathTicks < 5).
piston.death_ticks += 1;
if let Some(entry) = self
.ticks
.moving_pistons
.iter_mut()
.find(|(p, _)| *p == pos)
{
entry.1 = piston;
}
return;
}
self.remove_moving_piston(pos);
if b.is_block(self.state(pos), b.named.moving_piston) {
let mut new = self.update_from_neighbour_shapes(piston.moved_state, pos);
@@ -241,8 +257,13 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
direction != Direction::Down && self.has_signal(rel(up, direction), direction)
})
}
/// `PistonBaseBlock.checkIfExtend`.
/// `PistonBaseBlock.checkIfExtend`, which `neighborChanged`, `onPlace`
/// and `setPlacedBy` call only server-side: a client moves its pistons on
/// the server's block events alone.
pub(crate) fn piston_check_if_extend(&mut self, pos: BlockPos, state: BlockStateId) {
if self.level.is_client_side() {
return;
}
let b = self.b;
let direction = b.facing(state);
let extend = self.piston_neighbor_signal(pos, direction);
@@ -527,6 +548,12 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
for pos in to_destroy.iter().rev() {
let state = self.state(*pos);
self.drop_resources(state, *pos);
// The client's copy plays the destroy effect itself (the
// server sends no 2001 for piston moves).
let fire = b.is_block(state, b.named.fire) || b.is_block(state, b.named.soul_fire);
if !fire && self.level.is_client_side() {
self.level_event(2001, *pos, state.0 as i32);
}
self.set_block(*pos, b.air(), 18);
self.game_event(*pos, "minecraft:block_destroy");
to_update.push(state);
@@ -551,6 +578,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
progress: 0.0,
progress_o: 0.0,
last_ticked: 0,
death_ticks: 0,
id: 0,
},
);
@@ -576,6 +604,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
progress: 0.0,
progress_o: 0.0,
last_ticked: 0,
death_ticks: 0,
id: 0,
},
);
@@ -624,13 +653,17 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
let direction = b.facing(state);
let extended_state = b.table.set_bool(state, p.extended, true);
let sticky = self.is_sticky_piston(state);
let extend = self.piston_neighbor_signal(pos, direction);
if extend && (a == 1 || a == 2) {
self.set_block(pos, extended_state, 2);
return false;
}
if !extend && a == 0 {
return false;
// The signal re-check is server-only (`!level.isClientSide`): a
// client trusts the event as sent.
if !self.level.is_client_side() {
let extend = self.piston_neighbor_signal(pos, direction);
if extend && (a == 1 || a == 2) {
self.set_block(pos, extended_state, 2);
return false;
}
if !extend && a == 0 {
return false;
}
}
if a == 0 {
if !self.piston_move_blocks(pos, direction, true, sticky) {
@@ -664,6 +697,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
progress: 0.0,
progress_o: 0.0,
last_ticked: 0,
death_ticks: 0,
id: 0,
},
);
@@ -748,8 +782,18 @@ impl Behaviours {
}
let mut sim = self.sim(level, ticks);
let state = sim.state(event.pos);
if self.block(state) == event.block {
sim.trigger_event(state, event.pos, event.param_a, event.param_b);
if self.block(state) == event.block
&& sim.trigger_event(state, event.pos, event.param_a, event.param_b)
{
// ServerLevel.runBlockEvents broadcasts a handled event to
// the clients (`ClientboundBlockEventPacket`); their copy of
// the block re-runs `triggerEvent` (a piston's `moveBlocks`).
sim.emit(LevelEvent::BlockEntityEvent {
pos: event.pos,
state,
a: event.param_a,
b: event.param_b,
});
}
}
for event in reschedule {
@@ -779,4 +823,85 @@ impl Behaviours {
self.sim(level, ticks).moving_piston_tick(pos);
}
}
/// `ClientLevel.blockEvent`: the client re-runs the block's
/// `triggerEvent` on its own level (a piston's `moveBlocks` builds the
/// moving block entities it then animates) instead of waiting for the
/// server's states. No queue and no ticking-chunk gate: a client runs
/// the event where the packet lands.
pub fn client_block_event<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
pos: BlockPos,
a: i32,
b: i32,
) -> bool {
let triggered = {
let mut sim = self.sim(level, ticks);
let state = sim.state(pos);
let triggered = sim.trigger_event(state, pos, a, b);
if triggered {
// The client's copy of a handled event reaches the client
// copy of the block entity (a bell's ring).
sim.emit(LevelEvent::BlockEntityEvent { pos, state, a, b });
}
triggered
};
self.client_drain_block_events(level, ticks);
triggered
}
/// `ClientLevel.tickBlockEntities` for moving pistons: the client copy of
/// [`Behaviours::tick_block_entities`], without the server's
/// ticking-chunk gate (a client ticks every loaded block entity). One
/// whose block was replaced is dropped (`TickingBlockEntity.isRemoved`).
pub fn client_tick_block_entities<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
) {
let tickers: Vec<(BlockPos, u64)> = ticks
.moving_pistons
.iter()
.map(|(p, m)| (*p, m.id))
.collect();
for (pos, id) in tickers {
if !ticks
.moving_pistons
.iter()
.any(|(p, m)| *p == pos && m.id == id)
{
continue;
}
{
let mut sim = self.sim(level, ticks);
let state = sim.state(pos);
if sim.b.is_block(state, sim.b.named.moving_piston) {
sim.moving_piston_tick(pos);
} else {
sim.remove_moving_piston(pos);
}
}
self.client_drain_block_events(level, ticks);
}
}
/// Block events a client-side `triggerEvent` queued
/// (`PistonBaseBlock.checkIfExtend` in a neighbour update): a client
/// runs them as they come (`ClientLevel.blockEvent`), not in a tick pass.
fn client_drain_block_events<L: LevelAccess + ?Sized>(&self, level: &mut L, ticks: &mut Ticks) {
while let Some(event) = ticks.block_events.first().copied() {
ticks.block_events.remove(0);
let mut sim = self.sim(level, ticks);
let state = sim.state(event.pos);
if self.block(state) == event.block
&& sim.trigger_event(state, event.pos, event.param_a, event.param_b)
{
sim.emit(LevelEvent::BlockEntityEvent {
pos: event.pos,
state,
a: event.param_a,
b: event.param_b,
});
}
}
}
}
+100
View File
@@ -47,6 +47,8 @@ pub(crate) struct TestLevel {
pub day_time: i64,
pub events: Vec<LevelEvent>,
pub block_light: HashMap<BlockPos, i32>,
/// `Level.isClientSide`.
pub client_side: bool,
}
impl TestLevel {
fn new(b: &Behaviours) -> Self {
@@ -58,10 +60,14 @@ impl TestLevel {
day_time: 0,
events: Vec::new(),
block_light: HashMap::new(),
client_side: false,
}
}
}
impl LevelAccess for TestLevel {
fn is_client_side(&self) -> bool {
self.client_side
}
fn block_state(&self, pos: BlockPos) -> BlockStateId {
self.blocks.get(&pos).copied().unwrap_or(self.air)
}
@@ -919,6 +925,100 @@ fn pistons_push_and_sticky_pistons_pull() {
assert_eq!(w.get(BlockPos::new(2, 1, 0)), air);
}
/// `ServerLevel.runBlockEvents` reports a handled event for the clients
/// (`ClientboundBlockEventPacket`), carrying the block the event named.
#[test]
fn a_handled_block_event_reaches_the_clients() {
let mut w = world!();
w.floor(0, 10, "stone");
let piston = w.id("piston", &[("facing", "east")]);
let block = w.id("redstone_block", &[]);
w.set(BlockPos::new(0, 1, 0), piston);
w.set(BlockPos::new(-1, 1, 0), block);
w.level.events.clear();
w.full_tick();
assert!(w.level.events.iter().any(|e| matches!(
e,
LevelEvent::BlockEntityEvent {
pos,
state,
a: 0,
..
} if *pos == BlockPos::new(0, 1, 0) && *state == piston
)));
}
/// `ClientLevel.blockEvent`: the client re-runs `triggerEvent` on its own
/// level, which builds the moving block entities a piston's `moveBlocks`
/// makes, and ticks them to the placed block.
#[test]
fn a_client_replays_a_pistons_block_event() {
let mut w = world!();
w.floor(0, 10, "stone");
let piston = w.id("sticky_piston", &[("facing", "east")]);
let dirt = w.id("dirt", &[]);
let block = w.id("redstone_block", &[]);
w.put(BlockPos::new(0, 1, 0), piston);
w.put(BlockPos::new(1, 1, 0), dirt);
w.put(BlockPos::new(-1, 1, 0), block);
w.f.behaviours.client_block_event(
&mut w.level,
&mut w.ticks,
BlockPos::new(0, 1, 0),
0,
Direction::East as i32,
);
assert!(
w.ticks
.moving_pistons
.iter()
.any(|(p, _)| *p == BlockPos::new(2, 1, 0))
);
for _ in 0..3 {
w.level.time += 1;
w.f.behaviours
.client_tick_block_entities(&mut w.level, &mut w.ticks);
}
assert_eq!(w.get(BlockPos::new(2, 1, 0)), dirt);
assert_eq!(
w.name(BlockPos::new(1, 1, 0)),
"piston_head[facing=east,short=false,type=sticky]"
);
assert!(w.ticks.moving_pistons.is_empty());
}
/// A client's copy of a move doesn't start the pistons the moved block
/// powers (`checkIfExtend` is server-only): their own block events come from
/// the server.
#[test]
fn a_client_replay_starts_no_other_piston() {
let mut w = world!();
w.floor(0, 10, "stone");
w.level.client_side = true;
let piston = w.id("piston", &[("facing", "east")]);
let block = w.id("redstone_block", &[]);
let up = w.id("piston", &[("facing", "up")]);
w.put(BlockPos::new(0, 1, 0), piston);
w.put(BlockPos::new(1, 1, 0), block);
w.put(BlockPos::new(2, 2, 0), up);
w.f.behaviours.client_block_event(
&mut w.level,
&mut w.ticks,
BlockPos::new(0, 1, 0),
0,
Direction::East as i32,
);
for _ in 0..10 {
w.level.time += 1;
w.f.behaviours
.client_tick_block_entities(&mut w.level, &mut w.ticks);
}
assert_eq!(w.get(BlockPos::new(2, 1, 0)), block);
assert_eq!(w.get(BlockPos::new(2, 2, 0)), up);
assert!(w.ticks.moving_pistons.is_empty());
assert!(w.ticks.block_events.is_empty());
}
#[test]
fn pistons_pop_fragile_blocks_and_refuse_obsidian() {
let mut w = world!();