Pistons: a client's replay starts no piston and plays no sound

A client's copy of a block event (`client_block_event`) ran the server's
neighbour logic: a block it moved could start another piston on the client
alone (`PistonBaseBlock.checkIfExtend` is only called server-side), and the
re-run's `playSound(null, ...)` played the piston and note sounds a second
time next to the server's (`ClientLevel.playSeededSound` plays only for the
local player as the excluded one).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 16:14:21 +01:00
parent 48811b12f1
commit 2394d6e7dc
3 changed files with 48 additions and 1 deletions
+4
View File
@@ -867,6 +867,10 @@ impl LevelAccess for GameLevel<'_> {
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,
+6 -1
View File
@@ -257,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);
+38
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)
}
@@ -981,6 +987,38 @@ fn a_client_replays_a_pistons_block_event() {
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!();