Skip to main content

steel_core/behavior/blocks/container/
grindstone_block.rs

1use std::sync::Arc;
2
3use steel_macros::block_behavior;
4use steel_registry::{blocks::BlockRef, items::item::BlockHitResult};
5use steel_utils::{BlockPos, BlockStateId, Direction, translations};
6use text_components::TextComponent;
7
8use crate::{
9    behavior::{
10        BlockBehavior, BlockPlaceContext, InteractionResult, InventoryAccess,
11        blocks::face_attached_horizontal_directional_block::FaceAttachedHorizontalDirectionalBlock,
12    },
13    inventory::menu::kinds::grindstone,
14    player::Player,
15    world::{LevelReader, ScheduledTickAccess, World},
16};
17
18/// Behavior for Grindstone
19#[block_behavior]
20pub struct GrindstoneBlock {
21    face_attached: FaceAttachedHorizontalDirectionalBlock,
22}
23
24impl GrindstoneBlock {
25    /// Creates a new Grindstone Block Behavior
26    #[must_use]
27    pub const fn new(block: BlockRef) -> Self {
28        Self {
29            face_attached: FaceAttachedHorizontalDirectionalBlock::new(block),
30        }
31    }
32}
33
34impl BlockBehavior for GrindstoneBlock {
35    fn get_state_for_placement(&self, context: &BlockPlaceContext<'_>) -> Option<BlockStateId> {
36        self.face_attached.state_for_placement(context)
37    }
38
39    fn can_survive(&self, _state: BlockStateId, _world: &dyn LevelReader, _pos: BlockPos) -> bool {
40        true
41    }
42
43    fn update_shape(
44        &self,
45        state: BlockStateId,
46        world: &dyn ScheduledTickAccess,
47        pos: BlockPos,
48        direction: Direction,
49        _neighbor_pos: BlockPos,
50        _neighbor_state: BlockStateId,
51    ) -> BlockStateId {
52        FaceAttachedHorizontalDirectionalBlock::update_shape(state, world, pos, direction)
53    }
54
55    fn use_without_item(
56        &self,
57        _state: BlockStateId,
58        _world: &Arc<World>,
59        pos: BlockPos,
60        player: &Player,
61        _hit_result: &BlockHitResult,
62        _inv: &mut InventoryAccess,
63    ) -> InteractionResult {
64        let inventory = player.inventory.clone();
65        player.open_menu(
66            TextComponent::translated(translations::CONTAINER_GRINDSTONE_TITLE.msg()),
67            move |context| grindstone(inventory, context.container_id, pos, context.world),
68        );
69        InteractionResult::Success
70    }
71}