Skip to main content

steel_core/behavior/blocks/vegetation/
torchflower.rs

1use std::sync::Arc;
2
3use steel_macros::block_behavior;
4use steel_registry::{
5    blocks::{
6        BlockRef,
7        block_state_ext::BlockStateExt,
8        properties::{BlockStateProperties, IntProperty},
9    },
10    item_stack::ItemStack,
11    vanilla_blocks, vanilla_items,
12};
13use steel_utils::BlockStateId;
14
15use crate::{
16    behavior::blocks::vegetation::{
17        bonemealable::{Bonemealable, CropBonemealExt},
18        crop_block::CropLike,
19    },
20    world::{LevelReader, World},
21};
22
23/// Behavior for the Torchflower Block
24#[block_behavior]
25pub struct TorchflowerCropBlock {
26    block: BlockRef,
27}
28
29impl TorchflowerCropBlock {
30    /// Creates a new crop block behavior with a custom age property.
31    #[must_use]
32    pub const fn new(block: BlockRef) -> Self {
33        Self { block }
34    }
35}
36
37impl CropLike for TorchflowerCropBlock {
38    fn block(&self) -> BlockRef {
39        self.block
40    }
41
42    fn age_property(&self) -> &IntProperty {
43        &BlockStateProperties::AGE_1
44    }
45
46    fn max_age(&self) -> u8 {
47        2
48    }
49
50    fn get_state_for_age(&self, age: u8) -> BlockStateId {
51        if age == 2 {
52            vanilla_blocks::TORCHFLOWER.default_state()
53        } else {
54            self.block
55                .default_state()
56                .set_value(self.age_property(), age)
57        }
58    }
59
60    fn clone_item_stack(&self) -> ItemStack {
61        ItemStack::new(&vanilla_items::TORCHFLOWER_SEEDS)
62    }
63
64    fn should_random_tick(&self) -> bool {
65        rand::random_range(0..3) != 0
66    }
67}
68
69impl Bonemealable for TorchflowerCropBlock {
70    fn get_bonemeal_age_increase(&self, _world: &Arc<World>, _rng: &mut dyn rand::Rng) -> u8 {
71        1
72    }
73
74    fn is_valid_bonemeal_target(
75        &self,
76        state: BlockStateId,
77        _world: &dyn LevelReader,
78        _pos: steel_utils::BlockPos,
79    ) -> bool {
80        !self.is_max_age(state)
81    }
82
83    fn perform_bonemeal(
84        &self,
85        state: BlockStateId,
86        world: &Arc<World>,
87        rng: &mut dyn rand::Rng,
88        pos: steel_utils::BlockPos,
89    ) {
90        self.default_perform_bonemeal(state, world, rng, pos);
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use steel_registry::{init_vanilla_registry, vanilla_blocks};
97
98    use super::*;
99
100    #[test]
101    fn torchflower_age_two_becomes_flower_block() {
102        init_vanilla_registry();
103        let behavior = TorchflowerCropBlock::new(&vanilla_blocks::TORCHFLOWER_CROP);
104
105        let age_one = behavior.get_state_for_age(1);
106        assert_eq!(age_one.get_block(), &vanilla_blocks::TORCHFLOWER_CROP);
107        assert_eq!(age_one.get_value(&BlockStateProperties::AGE_1), 1);
108
109        let mature = behavior.get_state_for_age(2);
110        assert_eq!(mature.get_block(), &vanilla_blocks::TORCHFLOWER);
111    }
112}