Skip to main content

steel_core/command/execution/
swing_animation.rs

1use steel_protocol::packets::game::{ArgumentType, SuggestionType};
2use steel_registry::data_components::vanilla_components::SwingAnimationType;
3use steel_utils::{DowncastType, DowncastTypeKey, translations};
4
5use super::{
6    CommandArgumentSource, SteelArgumentParser, SteelArgumentSuggestionContext, suggest_list,
7};
8use crate::command::brigadier::{
9    CommandSyntaxError, CommandSyntaxErrorKind, StringReader, SuggestionsBuilder,
10};
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub(super) struct SwingAnimationParser;
14
15// SAFETY: this key is steel owned and uniqely identifies this concrete parser
16unsafe impl DowncastType for SwingAnimationParser {
17    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:command/parser/swing_animation");
18}
19
20#[derive(Debug)]
21pub(super) struct SwingAnimationValue(pub(super) SwingAnimationType);
22
23// SAFETY: this key is steel owned and uniqely identifies this concrete parser
24unsafe impl DowncastType for SwingAnimationValue {
25    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:command/value/swing_animation");
26}
27
28impl SteelArgumentParser for SwingAnimationParser {
29    type Value = SwingAnimationValue;
30
31    fn parse(
32        &self,
33        reader: &mut StringReader<'_>,
34        _source: &dyn CommandArgumentSource,
35    ) -> Result<Self::Value, CommandSyntaxError> {
36        let name = reader.read_unquoted_string();
37        SwingAnimationType::from_serialized_name(name)
38            .map(SwingAnimationValue)
39            .ok_or_else(|| {
40                let message = translations::ARGUMENT_SWING_ANIMATION_INVALID
41                    .message([name.to_owned()])
42                    .component();
43                reader.error(CommandSyntaxErrorKind::Dynamic(Box::new(message)))
44            })
45    }
46
47    fn list_suggestions(
48        &self,
49        _context: &dyn SteelArgumentSuggestionContext,
50        builder: &mut SuggestionsBuilder<'_>,
51    ) {
52        suggest_list(
53            builder,
54            [
55                SwingAnimationType::None,
56                SwingAnimationType::Whack,
57                SwingAnimationType::Stab,
58            ]
59            .map(SwingAnimationType::serialized_name),
60        );
61    }
62
63    fn protocol_argument(&self) -> (ArgumentType, Option<SuggestionType>) {
64        (ArgumentType::SwingAnimation, None)
65    }
66}