Skip to main content

steel_core/behavior/blocks/building/
web_block.rs

1use crate::behavior::{BlockBehavior, BlockPlaceContext};
2use crate::entity::{Entity, InsideBlockEffectCollector};
3use crate::world::World;
4use glam::DVec3;
5use std::sync::Arc;
6use steel_macros::block_behavior;
7use steel_registry::blocks::BlockRef;
8use steel_registry::vanilla_mob_effects::WEAVING;
9use steel_utils::{BlockPos, BlockStateId};
10
11/// Behavior for cobwebs.
12#[block_behavior]
13pub struct WebBlock {
14    block: BlockRef,
15}
16
17impl WebBlock {
18    /// Creates a new web block behavior.
19    #[must_use]
20    pub const fn new(block: BlockRef) -> Self {
21        Self { block }
22    }
23}
24
25impl BlockBehavior for WebBlock {
26    fn get_state_for_placement(&self, _context: &BlockPlaceContext<'_>) -> Option<BlockStateId> {
27        Some(self.block.default_state())
28    }
29
30    fn entity_inside(
31        &self,
32        state: BlockStateId,
33        _world: &Arc<World>,
34        _pos: BlockPos,
35        entity: &dyn Entity,
36        _effect_collector: &mut InsideBlockEffectCollector,
37        _is_precise: bool,
38    ) {
39        // Entities with the Weaving mob effect move faster in a cobweb.
40        let multiplier = if let Some(living) = entity.as_living_entity()
41            && living.has_mob_effect(WEAVING)
42        {
43            DVec3::new(0.5, 0.25, 0.5)
44        } else {
45            DVec3::new(0.25, 0.05, 0.25)
46        };
47
48        entity.make_stuck_in_block(state, multiplier);
49    }
50}