Pistons animate their moves on the clients, as Java's do
A remote client (single player's window on its server thread, a LAN guest, a dedicated server's client) never ran the client side of a piston's move: the block event reached block_entity_event, which has no piston ticker, so moving_piston blocks arrived with no moving block entity and pushed blocks teleported (#1704). Vanilla's runBlockEvents broadcasts a handled event to the clients, whose own level re-runs triggerEvent (PistonBaseBlock.moveBlocks) to build the PistonMovingBlockEntity it then ticks and renders interpolated. Do the same: - run_block_events emits a BlockEntityEvent for a handled event; the LAN and integrated hubs send it as ClientboundBlockEventPacket, before the tick's block changes, as vanilla's broadcast precedes ChunkHolder.broadcastChanges. - The remote client re-runs triggerEvent for pistons, note blocks and bells on its own level (dropResources stays server-side, as popResource checks !isClientSide), and ticks the resulting moving pistons each client tick so they animate and collide between the server's states. Block events a client-side move queues run right away, as ClientLevel.blockEvent does. Note blocks and bell rings reach the guests through the same packet, which they did not get before. Co-Authored-By: Devin <noreply@cognition.ai>
This commit is contained in:
@@ -563,6 +563,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,
|
||||
@@ -652,6 +655,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))
|
||||
}
|
||||
@@ -855,7 +861,12 @@ 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 { .. } => {}
|
||||
// Block.popResource of the block's own item (tall flowers, flower beds).
|
||||
LevelEvent::BlockEvent {
|
||||
pos,
|
||||
@@ -2492,6 +2503,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();
|
||||
|
||||
@@ -923,6 +923,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 = {
|
||||
@@ -1156,17 +1163,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));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1664,19 +1664,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
|
||||
@@ -3108,8 +3119,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();
|
||||
|
||||
@@ -287,6 +287,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
|
||||
|
||||
@@ -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);
|
||||
@@ -527,6 +543,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 +573,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
progress: 0.0,
|
||||
progress_o: 0.0,
|
||||
last_ticked: 0,
|
||||
death_ticks: 0,
|
||||
id: 0,
|
||||
},
|
||||
);
|
||||
@@ -576,6 +599,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
progress: 0.0,
|
||||
progress_o: 0.0,
|
||||
last_ticked: 0,
|
||||
death_ticks: 0,
|
||||
id: 0,
|
||||
},
|
||||
);
|
||||
@@ -624,13 +648,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 +692,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
progress: 0.0,
|
||||
progress_o: 0.0,
|
||||
last_ticked: 0,
|
||||
death_ticks: 0,
|
||||
id: 0,
|
||||
},
|
||||
);
|
||||
@@ -748,8 +777,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 +818,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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -919,6 +919,68 @@ 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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pistons_pop_fragile_blocks_and_refuse_obsidian() {
|
||||
let mut w = world!();
|
||||
|
||||
Reference in New Issue
Block a user