//! Registered climbing tags and trapdoor/ladder support with entity state order. use crate::{ compression::invalid, registry::{BlockRegistry, BlockStateId}, }; use horizon_core::position::BlockPos; use std::{collections::BTreeSet, fmt, io}; #[derive(Clone, Copy)] struct State { climbable: bool, glide_through: bool, open_trapdoor: Option, ladder: Option, } pub struct ClimbingRegistry<'r> { registry: &'r BlockRegistry, states: Vec, } impl<'r> ClimbingRegistry<'r> { /// Trapdoor names are imported class identities, not inferred from spelling. /// Rebuild this immutable snapshot when either resolved block tag reloads. pub fn new( registry: &'r BlockRegistry, trapdoors: &BTreeSet, climbable: &BTreeSet, glide_through: &BTreeSet, ) -> io::Result { for names in [trapdoors, climbable, glide_through] { if names.iter().any(|n| registry.default_state(n).is_none()) { return Err(invalid("unknown climbing block member")); } } let mut states = Vec::with_capacity(registry.len()); for i in 0..registry.len() { let s = registry.state(BlockStateId(i as u32)).unwrap(); let facing = || -> io::Result { match s.properties.get("facing").map(String::as_str) { Some("north") => Ok(0), Some("south") => Ok(1), Some("west") => Ok(2), Some("east") => Ok(3), _ => Err(invalid("invalid climbing facing")), } }; let open_trapdoor = if trapdoors.contains(&s.name) { let f = facing()?; match s.properties.get("open").map(String::as_str) { Some("true") => Some(f), Some("false") => None, _ => return Err(invalid("invalid trapdoor open state")), } } else { None }; states.push(State { climbable: climbable.contains(&s.name), glide_through: glide_through.contains(&s.name), open_trapdoor, ladder: if s.name == "minecraft:ladder" { Some(facing()?) } else { None }, }); } Ok(Self { registry, states }) } pub fn registry(&self) -> &'r BlockRegistry { self.registry } fn state(&self, id: BlockStateId) -> io::Result { self.states .get(id.0 as usize) .copied() .ok_or_else(|| invalid("climbing state outside registry")) } pub fn is_climbable_tag(&self, id: BlockStateId) -> io::Result { Ok(self.state(id)?.climbable) } } pub trait ClimbingWorld { type Error; fn is_spectator(&mut self) -> Result; fn block_position(&mut self) -> Result; fn in_block_state(&mut self) -> Result; fn is_fall_flying(&mut self) -> Result; fn block_state(&mut self, position: BlockPos) -> Result; fn set_last_climbable_position(&mut self, position: BlockPos); } #[derive(Debug)] pub enum ClimbingError { Data(io::Error), World(E), } impl fmt::Display for ClimbingError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Data(e) => e.fmt(f), Self::World(e) => e.fmt(f), } } } impl std::error::Error for ClimbingError {} /// Failed/negative queries retain the existing last-climbable position. Only a /// positive result overwrites it, using the position captured before state reads. pub fn on_climbable( world: &mut W, registry: &ClimbingRegistry<'_>, ) -> Result> { use ClimbingError::{Data, World}; if world.is_spectator().map_err(World)? { return Ok(false); } let position = world.block_position().map_err(World)?; let state = registry .state(world.in_block_state().map_err(World)?) .map_err(Data)?; if world.is_fall_flying().map_err(World)? && state.glide_through { return Ok(false); } let climbable = if state.climbable { true } else if let Some(facing) = state.open_trapdoor { let below = BlockPos::new(position.x, position.y.wrapping_sub(1), position.z); registry .state(world.block_state(below).map_err(World)?) .map_err(Data)? .ladder == Some(facing) } else { false }; if climbable { world.set_last_climbable_position(position); } Ok(climbable) }