diff --git a/crates/horizon-client/examples/probe/block_entity_tickers_fixture.rs b/crates/horizon-client/examples/probe/block_entity_tickers_fixture.rs index d375f1b74..d0cb2d839 100644 --- a/crates/horizon-client/examples/probe/block_entity_tickers_fixture.rs +++ b/crates/horizon-client/examples/probe/block_entity_tickers_fixture.rs @@ -87,7 +87,11 @@ pub fn main() { } "tick" => { out.push_str("tick"); - for (p, _) in entities.tickers(|p| states.get(&p).copied().unwrap_or(0)) { + for (p, _) in entities + .tickers(|p| states.get(&p).copied().unwrap_or(0)) + .iter() + .copied() + { out.push_str(&format!(" {},{},{}", p.0, p.1, p.2)); } out.push('\n'); diff --git a/crates/horizon-client/src/block_entities.rs b/crates/horizon-client/src/block_entities.rs index 944579b83..c0303c726 100644 --- a/crates/horizon-client/src/block_entities.rs +++ b/crates/horizon-client/src/block_entities.rs @@ -26,6 +26,7 @@ use crate::chunk::block_entity::BlockEntity; use crate::fast_hash::{FxHashMap, FxHashSet}; use std::collections::HashMap; +use std::sync::Arc; pub type Pos = (i32, i32, i32); @@ -46,7 +47,7 @@ pub enum Ticking { pub struct BlockEntities { map: HashMap, /// `Level.blockEntityTickers`: positions and the slot each was given. - tickers: Vec<(Pos, u32)>, + tickers: Arc>, /// `LevelChunk.tickersInLevel`: the live slot of each ticking position. live: FxHashMap, /// Positions that may need a slot, in the order vanilla would have made @@ -233,7 +234,7 @@ impl BlockEntities { } pub fn clear(&mut self) { self.map.clear(); - self.tickers.clear(); + self.tickers = Arc::default(); self.live.clear(); self.added.clear(); self.pending.clear(); @@ -274,7 +275,7 @@ impl BlockEntities { /// and freed ones leave it. `state` reads the block state at a position. /// Returns the slots to tick, in order; check each with `is_live` before /// ticking it, as the ticks before it can free it. - pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Vec<(Pos, u32)> { + pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Arc> { for (pos, token) in std::mem::take(&mut self.added) { if self.pending.get(&pos) != Some(&token) { continue; @@ -288,21 +289,21 @@ impl BlockEntities { } let slot = self.next(); self.live.insert(pos, slot); - self.tickers.push((pos, slot)); + Arc::make_mut(&mut self.tickers).push((pos, slot)); } if !std::mem::take(&mut self.freed) && self.out.is_empty() { - return self.tickers.clone(); + return Arc::clone(&self.tickers); } self.out.clear(); let (live, map) = (&mut self.live, &self.map); - self.tickers.retain(|(p, slot)| { + Arc::make_mut(&mut self.tickers).retain(|(p, slot)| { let mine = live.get(p) == Some(slot); if mine && !map.contains_key(p) { live.remove(p); } mine && map.contains_key(p) }); - self.tickers.clone() + Arc::clone(&self.tickers) } /// Whether a slot from `tickers` still ticks. pub fn is_live(&self, (pos, slot): (Pos, u32)) -> bool { @@ -329,8 +330,8 @@ mod tests { } fn order(b: &mut BlockEntities, states: &HashMap) -> Vec { b.tickers(|p| states.get(&p).copied().unwrap_or(0)) - .into_iter() - .map(|(p, _)| p) + .iter() + .map(|(p, _)| *p) .collect() } diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 9172a5fd4..c5c36f751 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -33,7 +33,7 @@ impl Game<'_> { 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() { + for slot in self.world.block_entity_tickers().iter().copied() { let pos = slot.0; // Ticks before this one can remove it (`TickingBlockEntity.isRemoved`). if !self.world.block_entities.is_live(slot) diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index 302be6aa3..1ef570451 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -65,14 +65,13 @@ impl Game<'_> { .unwrap_or_default(); self.spawners.entity_types = Some(types); } - /// The server tick of the spawner at `key`, if its block is still a - /// spawner. Its client tick comes after every block entity's - /// (`client_tick_spawners`). + /// The server tick of the spawner at `key`. Its client tick comes after + /// every block entity's (`client_tick_spawners`). + /// + /// The caller only reaches this for a `BlockEntity::Spawner`, and changing + /// the block removes or replaces that entity, so the block is not read + /// again here. pub(super) fn tick_spawner(&mut self, key: Pos) -> Result<()> { - let state = self.world.block(key.0, key.1, key.2); - if &*self.res.facts().get(state).name != "minecraft:spawner" { - return Ok(()); - } let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.take(&key) else { return Ok(()); }; diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 27e1417d6..9b3876288 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -355,7 +355,9 @@ impl World { false } /// The block entities to tick this tick, in order (`BlockEntities::tickers`). - pub fn block_entity_tickers(&mut self) -> Vec<(crate::block_entities::Pos, u32)> { + pub fn block_entity_tickers( + &mut self, + ) -> std::sync::Arc> { let (chunks, air) = (&self.chunks, self.air); self.block_entities.tickers(|(x, y, z)| { chunks