Skip to main content

steel_core/behavior/blocks/vegetation/
coral_block.rs

1use std::sync::Arc;
2
3use steel_macros::block_behavior;
4use steel_registry::blocks::properties::Direction;
5use steel_utils::{BlockPos, BlockStateId, types::UpdateFlags};
6
7use crate::behavior::block::BlockBehavior;
8use crate::behavior::context::BlockPlaceContext;
9use crate::world::{ScheduledTickAccess, World};
10
11use super::{BlockRef, coral_scan_for_water, schedule_coral_die_tick};
12
13/// Vanilla `CoralBlock` survival.
14#[block_behavior]
15pub struct CoralBlock {
16    block: BlockRef,
17    #[json_arg(vanilla_blocks, json = "dead_block")]
18    dead_block: BlockRef,
19}
20
21impl CoralBlock {
22    /// Creates a new live coral block behavior.
23    #[must_use]
24    pub const fn new(block: BlockRef, dead_block: BlockRef) -> Self {
25        Self { block, dead_block }
26    }
27
28    fn dead_state(&self) -> BlockStateId {
29        self.dead_block.default_state()
30    }
31}
32
33impl BlockBehavior for CoralBlock {
34    fn update_shape(
35        &self,
36        state: BlockStateId,
37        world: &dyn ScheduledTickAccess,
38        pos: BlockPos,
39        _direction: Direction,
40        _neighbor_pos: BlockPos,
41        _neighbor_state: BlockStateId,
42    ) -> BlockStateId {
43        schedule_coral_die_tick(state, world, pos, self.block);
44
45        state
46    }
47
48    fn get_state_for_placement(&self, context: &BlockPlaceContext<'_>) -> Option<BlockStateId> {
49        let state = self.block.default_state();
50        schedule_coral_die_tick(state, context.world, context.place_pos(), self.block);
51        Some(state)
52    }
53
54    fn tick(&self, state: BlockStateId, world: &Arc<World>, pos: BlockPos) {
55        if !coral_scan_for_water(state, world, pos) {
56            world.set_block(pos, self.dead_state(), UpdateFlags::UPDATE_CLIENTS);
57        }
58    }
59}