Skip to main content

steel_core/behavior/items/
egg.rs

1//! Egg item behavior (`EggItem`).
2//!
3//! Throwing an egg spawns a [`ThrownEggEntity`] from the player's eye, shot
4//! along their look direction, and consumes one egg unless the player
5//! has infinite materials. Mirrors vanilla `EggItem.use`.
6
7use std::sync::Arc;
8
9use steel_macros::item_behavior;
10use steel_protocol::packets::game::SoundSource;
11use steel_registry::{sound_events, vanilla_entities};
12
13use super::throw_projectile::{ThrowParams, throw_item_projectile};
14use crate::behavior::context::{InteractionResult, UseItemContext};
15use crate::behavior::item::ItemBehavior;
16use crate::entity::entities::ThrownEggEntity;
17use crate::entity::next_entity_id;
18
19/// Sound, shoot power and spread used when throwing an egg.
20const THROW: ThrowParams = ThrowParams {
21    sound: &sound_events::ENTITY_EGG_THROW,
22    sound_source: SoundSource::Players,
23    sound_volume: 0.5,
24    y_offset: 0.0,
25    power: 1.5,
26    uncertainty: 1.0,
27};
28
29/// Behavior for the egg item.
30#[item_behavior(class = "EggItem")]
31pub struct EggItem;
32
33impl ItemBehavior for EggItem {
34    fn use_item(&self, context: &mut UseItemContext) -> InteractionResult {
35        let world = context.world;
36        throw_item_projectile(context, &THROW, |spawn_pos| {
37            ThrownEggEntity::new(
38                &vanilla_entities::EGG,
39                next_entity_id(),
40                spawn_pos,
41                Arc::downgrade(world),
42            )
43        });
44        InteractionResult::Success
45    }
46}