186777110f
HopperBlockEntity.suckInItems scanned every item entity for each hopper that ticked without a container above. During the block-entity pass the game now keeps the set of block columns item boxes reach; a hopper whose column is not in it has nothing to suck, and the scan (same order, same item taken) runs only for the rest. Items do not move within the pass: removals leave a superset, and additions (pushed at the end with a new id) or removing the last item rebuild the set. It is dropped at the end of the pass. 500 hoppers, 200 items: 1.36 -> 0.05 ms per tick 2000 hoppers, 500 items: 13.52 -> 0.26 ms per tick 2000 hoppers, 2000 items: 60.6 -> 3.3 ms per tick Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE
135 lines
5.8 KiB
Rust
135 lines
5.8 KiB
Rust
//! `Level.tickBlockEntities` for the block entities ticked together here
|
|
//! (hoppers, crafters, jukeboxes, beacons, creaking hearts, spawners, trial
|
|
//! spawners, vaults, beehives and furnaces): only those whose state has a
|
|
//! ticker, in vanilla's list order (`horizon_client::block_entities`), in
|
|
//! chunks that tick blocks. Sculk, brewing stands and pistons tick in their
|
|
//! own passes.
|
|
use super::Game;
|
|
use horizon_client::{
|
|
Result,
|
|
block_entities::{Pos, Ticking},
|
|
containers::{BlockEntity, FurnaceKind, HOPPER_SLOTS},
|
|
};
|
|
use horizon_world::registry::BlockStateId;
|
|
|
|
impl Game<'_> {
|
|
pub(super) fn tick_block_entities(&mut self) -> Result<()> {
|
|
self.tick_mount_menu();
|
|
self.load_entity_types();
|
|
if !self.world.block_entities.has_states() {
|
|
let states = self.ticking_states();
|
|
self.world.block_entities.set_states(states);
|
|
}
|
|
self.forget_gone_hoppers();
|
|
let ticking = self.block_ticking_area();
|
|
let mut spawners = Vec::new();
|
|
let mut trial_spawners = Vec::new();
|
|
self.item_columns = Some(Default::default());
|
|
for slot in self.world.block_entity_tickers() {
|
|
let pos = slot.0;
|
|
// Ticks before this one can remove it (`TickingBlockEntity.isRemoved`).
|
|
if !self.world.block_entities.is_live(slot)
|
|
|| !ticking(pos.0, pos.2)
|
|
|| !self.border.contains(f64::from(pos.0), f64::from(pos.2))
|
|
{
|
|
continue;
|
|
}
|
|
match self.world.block_entities.get(&pos) {
|
|
Some(BlockEntity::Chest(items)) if items.len() == HOPPER_SLOTS => {
|
|
self.tick_hopper(pos)
|
|
}
|
|
Some(BlockEntity::Crafter(_)) => self.tick_crafter(pos),
|
|
Some(BlockEntity::Jukebox(_)) => self.tick_jukebox(pos),
|
|
Some(BlockEntity::Beacon(_)) => self.tick_beacon_at(pos),
|
|
Some(BlockEntity::CreakingHeart(_)) => self.tick_creaking_heart(pos)?,
|
|
Some(BlockEntity::Spawner(_)) => {
|
|
self.tick_spawner(pos)?;
|
|
spawners.push(pos);
|
|
}
|
|
Some(BlockEntity::TrialSpawner(_)) => {
|
|
self.tick_trial_spawner(pos)?;
|
|
trial_spawners.push(pos);
|
|
}
|
|
Some(BlockEntity::Vault(_)) => self.tick_vault(pos)?,
|
|
Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos),
|
|
Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?,
|
|
Some(BlockEntity::Brewing(_)) => self.tick_brewing_stand(pos)?,
|
|
_ => {}
|
|
}
|
|
}
|
|
// Items move between passes: the columns hold for this one only.
|
|
self.item_columns = None;
|
|
self.update_song_volumes();
|
|
self.scan_for_hearts();
|
|
// The client ticks (particles, sounds, the turning mob) are for a
|
|
// window; a dedicated or integrated server has none.
|
|
if self.options.dedicated.is_none() {
|
|
self.client_tick_spawners(&spawners);
|
|
for key in trial_spawners {
|
|
self.client_tick_trial_spawner(key);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
/// `BlockState.getTicker` of every state, for the block entities ticked
|
|
/// here. A creaking heart keeps its slot while uprooted (see
|
|
/// `tick_creaking_heart`).
|
|
fn ticking_states(&self) -> Vec<Ticking> {
|
|
let registry = self.pack.assets.registry;
|
|
(0..registry.len())
|
|
.map(|i| {
|
|
let state = BlockStateId(i as u32);
|
|
let name: &str = &self.res.facts().get(state).name;
|
|
match name {
|
|
"minecraft:hopper"
|
|
| "minecraft:crafter"
|
|
| "minecraft:beacon"
|
|
| "minecraft:creaking_heart"
|
|
| "minecraft:spawner"
|
|
| "minecraft:trial_spawner"
|
|
| "minecraft:vault"
|
|
| "minecraft:beehive"
|
|
| "minecraft:bee_nest"
|
|
| "minecraft:brewing_stand" => Ticking::Yes,
|
|
"minecraft:jukebox" => {
|
|
let record = registry
|
|
.state(state)
|
|
.and_then(|s| s.properties.get("has_record"))
|
|
.is_some_and(|v| v == "true");
|
|
if record { Ticking::Yes } else { Ticking::Idle }
|
|
}
|
|
n if FurnaceKind::from_block(n).is_some() => Ticking::Yes,
|
|
_ => Ticking::No,
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
/// `ServerLevel.shouldTickBlocksAt` in a block-ticking chunk
|
|
/// (`LevelChunk.isTicking`, block coordinates), from every player's
|
|
/// tickets (`update_chunk_tickets`).
|
|
fn block_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> {
|
|
let levels = self.tickets.levels();
|
|
move |x, z| levels.block_ticking_at(x, z)
|
|
}
|
|
/// `AbstractFurnaceBlockEntity.serverTick` of the furnace at `pos`, and
|
|
/// its block's `lit` following it.
|
|
fn tick_furnace(&mut self, pos: Pos) -> Result<()> {
|
|
let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else {
|
|
return Ok(());
|
|
};
|
|
let Some(lit) = f.tick(self.res.recipes(), self.res.items(), self.res.providers()) else {
|
|
return Ok(());
|
|
};
|
|
let state = self.world.block(pos.0, pos.1, pos.2);
|
|
if let Some(mut s) = self.pack.assets.registry.state(state).cloned()
|
|
&& s.properties.contains_key("lit")
|
|
{
|
|
s.properties.insert("lit".into(), lit.to_string());
|
|
if let Ok(id) = self.pack.assets.registry.resolve(&s) {
|
|
self.world.set(pos.0, pos.1, pos.2, id)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|