Skip to main content

steel_core/behavior/blocks/vegetation/
coral_fan_block.rs

1use std::sync::Arc;
2
3use steel_macros::block_behavior;
4use steel_registry::blocks::block_state_ext::BlockStateExt;
5use steel_registry::blocks::properties::{BlockStateProperties, Direction};
6use steel_registry::{vanilla_blocks, vanilla_fluids};
7use steel_utils::{BlockPos, BlockStateId, types::UpdateFlags};
8
9use crate::behavior::block::BlockBehavior;
10use crate::behavior::context::BlockPlaceContext;
11use crate::world::{LevelReader, ScheduledTickAccess, World};
12
13use super::{BlockRef, coral_plant_can_survive, coral_scan_for_water, schedule_coral_die_tick};
14
15/// Vanilla `CoralFanBlock` survival (live coral fans).
16///
17/// Inherits `canSurvive` from `BaseCoralPlantTypeBlock` via `BaseCoralFanBlock`.
18#[block_behavior]
19pub struct CoralFanBlock {
20    block: BlockRef,
21    #[json_arg(vanilla_blocks, json = "dead_block")]
22    dead_block: BlockRef,
23}
24
25impl CoralFanBlock {
26    /// Creates a new live coral fan block behavior.
27    #[must_use]
28    pub const fn new(block: BlockRef, dead_block: BlockRef) -> Self {
29        Self { block, dead_block }
30    }
31
32    fn dead_state(&self) -> BlockStateId {
33        self.dead_block
34            .default_state()
35            .set_value(&BlockStateProperties::WATERLOGGED, false)
36    }
37}
38
39impl BlockBehavior for CoralFanBlock {
40    fn can_survive(&self, _state: BlockStateId, world: &dyn LevelReader, pos: BlockPos) -> bool {
41        coral_plant_can_survive(world, pos)
42    }
43
44    fn on_place(
45        &self,
46        state: BlockStateId,
47        world: &Arc<World>,
48        pos: BlockPos,
49        _old_state: BlockStateId,
50        _moved_by_piston: bool,
51    ) {
52        schedule_coral_die_tick(state, world, pos, self.block);
53    }
54
55    fn update_shape(
56        &self,
57        state: BlockStateId,
58        world: &dyn ScheduledTickAccess,
59        pos: BlockPos,
60        direction: Direction,
61        _neighbor_pos: BlockPos,
62        _neighbor_state: BlockStateId,
63    ) -> BlockStateId {
64        if direction == Direction::Down && !self.can_survive(state, world, pos) {
65            return vanilla_blocks::AIR.default_state();
66        }
67
68        schedule_coral_die_tick(state, world, pos, self.block);
69
70        if state.get_value(&BlockStateProperties::WATERLOGGED) {
71            let delay = world.fluid_tick_delay(&vanilla_fluids::WATER);
72            let _ = world.schedule_fluid_tick_default(pos, &vanilla_fluids::WATER, delay);
73        }
74
75        state
76    }
77
78    fn get_state_for_placement(&self, context: &BlockPlaceContext<'_>) -> Option<BlockStateId> {
79        let state = self.block.default_state();
80        if !self.can_survive(state, context.world, context.place_pos()) {
81            return None;
82        }
83        Some(state.set_value(&BlockStateProperties::WATERLOGGED, context.is_full_water()))
84    }
85
86    fn tick(&self, state: BlockStateId, world: &Arc<World>, pos: BlockPos) {
87        if !coral_scan_for_water(state, world, pos) {
88            world.set_block(pos, self.dead_state(), UpdateFlags::UPDATE_CLIENTS);
89        }
90    }
91}