Skip to main content

steel_core/behavior/items/
bed_item.rs

1use steel_macros::item_behavior;
2use steel_registry::blocks::BlockRef;
3use steel_utils::{BlockStateId, types::UpdateFlags};
4
5use crate::behavior::ItemBehavior;
6use crate::behavior::context::{BlockPlaceContext, InteractionResult, UseOnContext};
7use crate::behavior::items::BlockItem;
8
9/// Behavior for beds
10#[item_behavior]
11pub struct BedItem {
12    #[json_arg(vanilla_blocks, json = "block")]
13    _block: BlockRef,
14    base: BlockItem,
15}
16
17impl BedItem {
18    const PLACE_BLOCK_FLAGS: UpdateFlags = UpdateFlags::UPDATE_CLIENTS
19        .union(UpdateFlags::UPDATE_IMMEDIATE)
20        .union(UpdateFlags::UPDATE_KNOWN_SHAPE);
21
22    /// New bed item behavior
23    #[must_use]
24    pub const fn new(block: BlockRef) -> Self {
25        Self {
26            _block: block,
27            base: BlockItem::new(block),
28        }
29    }
30
31    fn place_block(context: &BlockPlaceContext<'_>, state: BlockStateId) -> bool {
32        context
33            .world
34            .set_block(context.place_pos(), state, Self::PLACE_BLOCK_FLAGS)
35    }
36}
37
38impl ItemBehavior for BedItem {
39    fn use_on(&self, context: &mut UseOnContext) -> InteractionResult {
40        self.base
41            .place_with(context.build_place_context(), |place_context, state| {
42                Self::place_block(place_context, state)
43            })
44    }
45}