Stop the jukebox's song and the beacon as their block entities go

LevelChunk.removeBlockEntity calls setRemoved: a jukebox reports its song
stopping (the jukebox_stop_play game event and level event 1011) and a
beacon plays its deactivation, whatever the flags. A record popped from a
replaced jukebox stops a playing song first and updates the neighbours as
JukeboxBlockEntity.onSongChanged does (a redstone wire beside it can pop).
The block update oracle removes block entities the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-12 17:44:54 +02:00
parent 4b776a45d8
commit ca1bc52a49
7 changed files with 122 additions and 34 deletions
+2 -2
View File
@@ -491,8 +491,8 @@ impl LevelAccess for GameLevel<'_> {
event => self.events.push(event),
}
}
fn remove_block_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) {
self.remove_entity(pos, old, side_effects);
fn remove_block_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) -> u8 {
self.remove_entity(pos, old, side_effects)
}
}
/// FallingBlockEntity carrying a block state.
+51 -6
View File
@@ -11,7 +11,10 @@ use horizon_client::{
use horizon_core::position::BlockPos;
use horizon_core::random::RandomSource;
use horizon_world::{
level::removal::{self, PreRemove},
level::{
LevelEvent,
removal::{self, PreRemove},
},
registry::BlockStateId,
};
use std::collections::HashMap;
@@ -280,23 +283,30 @@ impl GameLevel<'_> {
/// `BlockEntity.preRemoveSideEffects` and `removeBlockEntity` for the block
/// entity of `old` at `pos`. A block entity Horizon never created reads as
/// vanilla's fresh one: an empty container still draws a spot per slot.
pub(super) fn remove_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) {
pub(super) fn remove_entity(
&mut self,
pos: BlockPos,
old: BlockStateId,
side_effects: bool,
) -> u8 {
let key = (pos.x, pos.y, pos.z);
if self.remove_saved_entity(pos, old, side_effects) {
return;
return 0;
}
// Sculk blocks and creaking hearts notice their removal on their own tick.
if matches!(
self.world.block_entities.get(&key),
Some(BlockEntity::Sculk(_) | BlockEntity::CreakingHeart(_))
) {
return;
return 0;
}
let entity = self.world.block_entities.remove(&key);
self.world.block_names.remove(&key);
let name = self.facts.get(old).name.clone();
let kind = removal::pre_remove(&name);
let mut neighbour_updates = 0;
if side_effects {
let name = self.facts.get(old).name.clone();
match removal::pre_remove(&name) {
match kind {
PreRemove::Contents { slots, .. } => {
let items = entity.as_ref().map_or(&[][..], BlockEntity::slots);
for slot in 0..slots {
@@ -310,9 +320,16 @@ impl GameLevel<'_> {
}
}
PreRemove::Jukebox => {
// popOutTheItem: removeTheItem stops a playing song (its
// events and a neighbour update), the record pops, and
// onSongChanged updates the neighbours again.
if let Some(BlockEntity::Jukebox(j)) = &entity
&& let Some(record) = j.record.clone()
{
if j.playing {
self.jukebox_stopped(pos, old);
neighbour_updates += 1;
}
let at = removal::jukebox_pop_position(&mut self.world.random, pos);
self.spill.spawns.push(Spawn {
stack: record,
@@ -320,6 +337,7 @@ impl GameLevel<'_> {
motion: None,
delay: 10,
});
neighbour_updates += 1;
}
}
PreRemove::Lectern => {
@@ -348,12 +366,39 @@ impl GameLevel<'_> {
PreRemove::ShulkerBox
| PreRemove::SculkShrieker
| PreRemove::CreakingHeart
| PreRemove::Beacon
| PreRemove::Nothing => {}
}
}
// BlockEntity.setRemoved: a jukebox always reports its song stopping,
// a beacon plays its deactivation.
match kind {
PreRemove::Jukebox => self.jukebox_stopped(pos, old),
PreRemove::Beacon => self.events.push(LevelEvent::Sound {
pos,
sound: "minecraft:block.beacon.deactivate".into(),
volume: 1.0,
pitch: 1.0,
}),
_ => {}
}
if let Some(entity) = entity {
self.spill.removed.push(([pos.x, pos.y, pos.z], entity));
}
neighbour_updates
}
/// The jukebox's `JUKEBOX_STOP_PLAY` game event and level event 1011.
fn jukebox_stopped(&mut self, pos: BlockPos, state: BlockStateId) {
self.events.push(LevelEvent::GameEvent {
pos,
event: "minecraft:jukebox_stop_play",
state: Some(state),
});
self.events.push(LevelEvent::LevelEvent {
id: 1011,
pos,
data: 0,
});
}
/// A block entity Horizon keeps as it was loaded (campfires, decorated
/// pots, chiseled bookshelves, shelves): its items spill as vanilla's do.
@@ -115,26 +115,50 @@ impl LevelAccess for Cells {
/// The oracle level's block entities are fresh: containers spill only
/// empty slots (which still draw their spot), a lectern whose state has a
/// book drops its empty book, a fresh shrieker has no warning level.
fn remove_block_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) {
if !side_effects {
return;
}
let slots = match self.removals.get(old.0 as usize) {
Some(PreRemove::Contents { slots, .. }) => *slots,
Some(PreRemove::Campfire) => 4,
Some(PreRemove::Lectern) if self.lecterns_with_books.contains(&old) => {
self.events.push(LevelEvent::BlockEvent {
pos,
state: old,
kind: "spawn_item",
});
return;
fn remove_block_entity(&mut self, pos: BlockPos, old: BlockStateId, side_effects: bool) -> u8 {
let kind = self.removals.get(old.0 as usize).copied();
if side_effects {
let slots = match kind {
Some(PreRemove::Contents { slots, .. }) => slots,
Some(PreRemove::Campfire) => 4,
Some(PreRemove::Lectern) if self.lecterns_with_books.contains(&old) => {
self.events.push(LevelEvent::BlockEvent {
pos,
state: old,
kind: "spawn_item",
});
0
}
_ => 0,
};
for _ in 0..slots {
drop_item_stack(&mut self.random, pos, 0);
}
_ => return,
};
for _ in 0..slots {
drop_item_stack(&mut self.random, pos, 0);
}
// BlockEntity.setRemoved: a jukebox stops its (absent) song, a beacon
// plays its deactivation.
match kind {
Some(PreRemove::Jukebox) => {
self.events.push(LevelEvent::GameEvent {
pos,
event: "minecraft:jukebox_stop_play",
state: Some(old),
});
self.events.push(LevelEvent::LevelEvent {
id: 1011,
pos,
data: 0,
});
}
Some(PreRemove::Beacon) => self.events.push(LevelEvent::Sound {
pos,
sound: "minecraft:block.beacon.deactivate".into(),
volume: 1.0,
pitch: 1.0,
}),
_ => {}
}
0
}
}
+11 -2
View File
@@ -398,8 +398,17 @@ pub trait LevelAccess {
/// another block: run `BlockEntity.preRemoveSideEffects` when
/// `side_effects` (containers spill their contents, a jukebox pops its
/// record, a lectern its book...) and remove the block entity. Called
/// after the storage write, before `affectNeighborsAfterRemoval`.
fn remove_block_entity(&mut self, _pos: BlockPos, _old: BlockStateId, _side_effects: bool) {}
/// after the storage write, before `affectNeighborsAfterRemoval`. Returns
/// how many times the side effects called `updateNeighborsAt(pos, block)`
/// (a jukebox whose song changed), which the caller then runs.
fn remove_block_entity(
&mut self,
_pos: BlockPos,
_old: BlockStateId,
_side_effects: bool,
) -> u8 {
0
}
/// Biome identifier at `pos` (`minecraft:plains`), for features placed by
/// block behaviour (bone meal flowers). None when unknown.
fn biome_name(&self, _pos: BlockPos) -> Option<&str> {
@@ -26,6 +26,9 @@ pub enum PreRemove {
SculkShrieker,
/// `CreakingHeartBlockEntity.removeProtector`.
CreakingHeart,
/// No side effect, but `BeaconBlockEntity.setRemoved` plays the
/// deactivation sound whenever it goes.
Beacon,
}
/// The side effect of removing the block entity of block `name` (a
@@ -55,6 +58,7 @@ pub fn pre_remove(name: &str) -> PreRemove {
"lectern" => PreRemove::Lectern,
"sculk_shrieker" => PreRemove::SculkShrieker,
"creaking_heart" => PreRemove::CreakingHeart,
"beacon" => PreRemove::Beacon,
_ => PreRemove::Nothing,
}
}
+6 -1
View File
@@ -327,7 +327,12 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
// BlockEntity.preRemoveSideEffects (unless UPDATE_SKIP_BLOCK_ENTITY_SIDEEFFECTS)
// and removeBlockEntity, before the neighbours hear of the removal.
let side_effects = update_flags & flags::UPDATE_SKIP_BLOCK_ENTITY_SIDEEFFECTS == 0;
self.level.remove_block_entity(pos, old, side_effects);
let updates = self.level.remove_block_entity(pos, old, side_effects);
for _ in 0..updates {
// JukeboxBlockEntity.onSongChanged.
let block = self.block_of(old);
self.update_neighbors_at(pos, block);
}
}
let moved = update_flags & flags::UPDATE_MOVE_BY_PISTON != 0;
if (block_changed
@@ -81,13 +81,14 @@ public final class VanillaBlockUpdateOracle {
boolean movedByPiston = (flags & 64) != 0;
boolean sideEffects = (flags & 256) == 0;
if (blockChanged && old.hasBlockEntity() && !state.shouldChangedStateKeepBlockEntity(old)) {
if (sideEffects) {
BlockEntity blockEntity = world.blockEntityFor(pos.immutable(), old);
if (blockEntity != null) {
blockEntity.preRemoveSideEffects(pos, old);
}
// A real chunk always holds the block entity of an entity block.
BlockEntity blockEntity = world.blockEntityFor(pos.immutable(), old);
if (sideEffects && blockEntity != null) {
blockEntity.preRemoveSideEffects(pos, old);
}
// LevelChunk.removeBlockEntity.
if (world.blockEntities != null) world.blockEntities.remove(pos);
if (blockEntity != null) blockEntity.setRemoved();
}
if ((blockChanged || newBlock instanceof net.minecraft.world.level.block.BaseRailBlock) && ((flags & 1) != 0 || movedByPiston)) {
old.affectNeighborsAfterRemoval(world, pos, movedByPiston);