Skip to main content

steel_core/behavior/items/
food_on_a_stick.rs

1//! Food-on-a-stick item behavior implementation.
2
3use steel_macros::item_behavior;
4use steel_registry::entity_type::EntityTypeRef;
5use steel_registry::item_stack::ItemStack;
6use steel_registry::stat::vanilla_stat_types;
7use steel_registry::vanilla_items;
8
9use crate::behavior::{InteractionResult, ItemBehavior, UseItemContext};
10use crate::entity::Entity as _;
11use crate::player::Player;
12
13/// Behavior for vanilla `FoodOnAStickItem`.
14#[item_behavior]
15pub struct FoodOnAStickItem {
16    #[json_arg(vanilla_entities)]
17    can_interact_with: EntityTypeRef,
18    #[json_arg(value)]
19    consume_item_damage: i32,
20}
21
22impl FoodOnAStickItem {
23    /// Creates a food-on-a-stick behavior for one controlled vehicle type.
24    #[must_use]
25    pub const fn new(can_interact_with: EntityTypeRef, consume_item_damage: i32) -> Self {
26        Self {
27            can_interact_with,
28            consume_item_damage,
29        }
30    }
31    fn pass_without_boost(player: &Player, item: ItemStack) -> InteractionResult {
32        player.award_stat(&vanilla_stat_types::ITEM_USED, item.item());
33        InteractionResult::Pass
34    }
35}
36
37impl ItemBehavior for FoodOnAStickItem {
38    fn use_item(&self, context: &mut UseItemContext) -> InteractionResult {
39        let item = context.inv.with_item(|item| item.clone());
40        let Some(vehicle) = context.player.controlled_vehicle() else {
41            return Self::pass_without_boost(context.player, item);
42        };
43        if vehicle.entity_type() != self.can_interact_with {
44            return Self::pass_without_boost(context.player, item);
45        }
46        let Some(steerable) = vehicle.as_item_steerable() else {
47            return Self::pass_without_boost(context.player, item);
48        };
49        if !steerable.boost() {
50            return Self::pass_without_boost(context.player, item);
51        }
52
53        let has_infinite_materials = context.player.has_infinite_materials();
54        context.inv.with_inventory(|inventory| {
55            inventory.hurt_and_convert_item_in_hand_on_break(
56                context.hand,
57                self.consume_item_damage,
58                &vanilla_items::FISHING_ROD,
59                has_infinite_materials,
60            );
61        });
62
63        InteractionResult::SuccessServer
64    }
65}