Skip to main content

steel_core/behavior/items/
dye.rs

1//! Vanilla `DyeItem` behavior: dyes an alive, unsheared sheep.
2
3use steel_macros::item_behavior;
4use steel_registry::data_components::vanilla_components::DYE;
5use steel_registry::item_stack::ItemStack;
6use steel_registry::sound_events::ITEM_DYE_USE;
7use steel_utils::Downcast as _;
8use steel_utils::types::InteractionHand;
9
10use crate::behavior::{InteractionResult, ItemBehavior};
11use crate::entity::entities::SheepEntity;
12use crate::entity::{Entity, LivingEntity};
13use crate::player::Player;
14
15/// Behavior for the sixteen dye items (`DyeItem`).
16///
17/// Ports vanilla `DyeItem.interactLivingEntity`: dying a sheep plays the `DYE_USE`
18/// sound, sets the sheep's wool color, and consumes one dye from the stack.
19#[item_behavior(class = "DyeItem")]
20pub struct DyeItem;
21
22impl ItemBehavior for DyeItem {
23    fn interact_living_entity(
24        &self,
25        stack: &mut ItemStack,
26        _player: &Player,
27        target: &dyn LivingEntity,
28        _hand: InteractionHand,
29    ) -> InteractionResult {
30        let Some(dye_color) = stack.get(DYE).copied() else {
31            return InteractionResult::Pass;
32        };
33        let Some(sheep) = target.downcast_ref::<SheepEntity>() else {
34            return InteractionResult::Pass;
35        };
36
37        if !Entity::is_alive(sheep) || sheep.is_sheared() || sheep.color() == dye_color {
38            return InteractionResult::Pass;
39        }
40
41        sheep.play_sound(&ITEM_DYE_USE, 1.0, 1.0);
42        sheep.set_color(dye_color);
43        stack.shrink_one();
44
45        InteractionResult::Success
46    }
47}