diff options
| author | mat <git@matdoes.dev> | 2026-05-06 18:38:23 -0545 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-05-07 08:05:58 -1200 |
| commit | cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919 (patch) | |
| tree | 237fd12a9768fe7431ce42dfbdde60f4c7850e06 /azalea-brigadier/src/builder | |
| parent | 9ffd0e80bbb3feace231553d6539124585b03e3c (diff) | |
| download | azalea-drasl-cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919.tar.xz | |
azalea-brigadier now allows commands to return a Result
Diffstat (limited to 'azalea-brigadier/src/builder')
| -rw-r--r-- | azalea-brigadier/src/builder/argument_builder.rs | 48 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/literal_argument_builder.rs | 4 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/required_argument_builder.rs | 22 |
3 files changed, 37 insertions, 37 deletions
diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index 27320eba..7617d08a 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -14,11 +14,11 @@ use crate::{ }; #[derive(Debug)] -pub enum ArgumentBuilderType<S> { +pub enum ArgumentBuilderType<S, R> { Literal(Literal), - Argument(Argument<S>), + Argument(Argument<S, R>), } -impl<S> Clone for ArgumentBuilderType<S> { +impl<S, R> Clone for ArgumentBuilderType<S, R> { fn clone(&self) -> Self { match self { ArgumentBuilderType::Literal(literal) => ArgumentBuilderType::Literal(literal.clone()), @@ -30,20 +30,20 @@ impl<S> Clone for ArgumentBuilderType<S> { } /// A node that hasn't yet been built. -pub struct ArgumentBuilder<S> { - arguments: CommandNode<S>, +pub struct ArgumentBuilder<S, R = i32> { + arguments: CommandNode<S, R>, - command: Command<S>, + command: Command<S, R>, requirement: Arc<dyn Fn(&S) -> bool + Send + Sync>, - target: Option<Arc<RwLock<CommandNode<S>>>>, + target: Option<Arc<RwLock<CommandNode<S, R>>>>, forks: bool, - modifier: Option<Arc<RedirectModifier<S>>>, + modifier: Option<Arc<RedirectModifier<S, R>>>, } /// A node that isn't yet built. -impl<S> ArgumentBuilder<S> { - pub fn new(value: ArgumentBuilderType<S>) -> Self { +impl<S, R> ArgumentBuilder<S, R> { + pub fn new(value: ArgumentBuilderType<S, R>) -> Self { Self { arguments: CommandNode { value, @@ -65,14 +65,14 @@ impl<S> ArgumentBuilder<S> { /// literal("foo").then(literal("bar").executes(|ctx: &CommandContext<()>| 42)) /// # ; /// ``` - pub fn then(self, argument: ArgumentBuilder<S>) -> Self { + pub fn then(self, argument: ArgumentBuilder<S, R>) -> Self { self.then_built(argument.build()) } /// Add an already built child node to this node. /// /// You should usually use [`Self::then`] instead. - pub fn then_built(mut self, argument: CommandNode<S>) -> Self { + pub fn then_built(mut self, argument: CommandNode<S, R>) -> Self { self.arguments.add_child(&Arc::new(RwLock::new(argument))); self } @@ -90,9 +90,9 @@ impl<S> ArgumentBuilder<S> { /// ``` pub fn executes<F>(mut self, f: F) -> Self where - F: Fn(&CommandContext<S>) -> i32 + Send + Sync + 'static, + F: Fn(&CommandContext<S, R>) -> R + Send + Sync + 'static, { - self.command = Some(Arc::new(move |ctx: &CommandContext<S>| Ok(f(ctx)))); + self.command = Some(Arc::new(move |ctx: &CommandContext<S, R>| Ok(f(ctx)))); self } @@ -100,7 +100,7 @@ impl<S> ArgumentBuilder<S> { /// CommandSyntaxError>`. pub fn executes_result<F>(mut self, f: F) -> Self where - F: Fn(&CommandContext<S>) -> Result<i32, CommandSyntaxError> + Send + Sync + 'static, + F: Fn(&CommandContext<S, R>) -> Result<R, CommandSyntaxError> + Send + Sync + 'static, { self.command = Some(Arc::new(f)); self @@ -131,22 +131,22 @@ impl<S> ArgumentBuilder<S> { self } - pub fn redirect(self, target: Arc<RwLock<CommandNode<S>>>) -> Self { + pub fn redirect(self, target: Arc<RwLock<CommandNode<S, R>>>) -> Self { self.forward(target, None, false) } pub fn fork( self, - target: Arc<RwLock<CommandNode<S>>>, - modifier: Arc<RedirectModifier<S>>, + target: Arc<RwLock<CommandNode<S, R>>>, + modifier: Arc<RedirectModifier<S, R>>, ) -> Self { self.forward(target, Some(modifier), true) } pub fn forward( mut self, - target: Arc<RwLock<CommandNode<S>>>, - modifier: Option<Arc<RedirectModifier<S>>>, + target: Arc<RwLock<CommandNode<S, R>>>, + modifier: Option<Arc<RedirectModifier<S, R>>>, fork: bool, ) -> Self { if !self.arguments.children.is_empty() { @@ -158,13 +158,13 @@ impl<S> ArgumentBuilder<S> { self } - pub fn arguments(&self) -> &CommandNode<S> { + pub fn arguments(&self) -> &CommandNode<S, R> { &self.arguments } /// Manually build this node into a [`CommandNode`]. You probably don't need /// to do this yourself. - pub fn build(self) -> CommandNode<S> { + pub fn build(self) -> CommandNode<S, R> { let mut result = CommandNode { value: self.arguments.value, command: self.command, @@ -185,7 +185,7 @@ impl<S> ArgumentBuilder<S> { } } -impl<S> Debug for ArgumentBuilder<S> { +impl<S, R> Debug for ArgumentBuilder<S, R> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("ArgumentBuilder") .field("arguments", &self.arguments) @@ -197,7 +197,7 @@ impl<S> Debug for ArgumentBuilder<S> { .finish() } } -impl<S> Clone for ArgumentBuilder<S> { +impl<S, R> Clone for ArgumentBuilder<S, R> { fn clone(&self) -> Self { Self { arguments: self.arguments.clone(), diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index 7045b79f..c55fbeed 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -12,13 +12,13 @@ impl Literal { } } -impl<S> From<Literal> for ArgumentBuilderType<S> { +impl<S, R> From<Literal> for ArgumentBuilderType<S, R> { fn from(literal: Literal) -> Self { Self::Literal(literal) } } /// Shortcut for creating a new literal builder node. -pub fn literal<S>(value: &str) -> ArgumentBuilder<S> { +pub fn literal<S, R>(value: &str) -> ArgumentBuilder<S, R> { ArgumentBuilder::new(ArgumentBuilderType::Literal(Literal::new(value))) } diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs index 01bfc0bc..e0822db3 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -16,16 +16,16 @@ use crate::{ /// An argument node type. /// /// The `T` type parameter is the type of the argument, which can be anything. -pub struct Argument<S> { +pub struct Argument<S, R> { pub name: String, parser: Arc<dyn ArgumentType + Send + Sync>, - custom_suggestions: Option<Arc<dyn SuggestionProvider<S> + Send + Sync>>, + custom_suggestions: Option<Arc<dyn SuggestionProvider<S, R> + Send + Sync>>, } -impl<S> Argument<S> { +impl<S, R> Argument<S, R> { pub fn new( name: &str, parser: Arc<dyn ArgumentType + Send + Sync>, - custom_suggestions: Option<Arc<dyn SuggestionProvider<S> + Send + Sync>>, + custom_suggestions: Option<Arc<dyn SuggestionProvider<S, R> + Send + Sync>>, ) -> Self { Self { name: name.to_owned(), @@ -40,7 +40,7 @@ impl<S> Argument<S> { pub fn list_suggestions( &self, - context: CommandContext<S>, + context: CommandContext<S, R>, builder: SuggestionsBuilder, ) -> Suggestions { if let Some(s) = &self.custom_suggestions { @@ -55,13 +55,13 @@ impl<S> Argument<S> { } } -impl<S> From<Argument<S>> for ArgumentBuilderType<S> { - fn from(argument: Argument<S>) -> Self { +impl<S, R> From<Argument<S, R>> for ArgumentBuilderType<S, R> { + fn from(argument: Argument<S, R>) -> Self { Self::Argument(argument) } } -impl<S> Debug for Argument<S> { +impl<S, R> Debug for Argument<S, R> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Argument") .field("name", &self.name) @@ -71,14 +71,14 @@ impl<S> Debug for Argument<S> { } /// Shortcut for creating a new argument builder node. -pub fn argument<S>( +pub fn argument<S, R>( name: &str, parser: impl ArgumentType + Send + Sync + 'static, -) -> ArgumentBuilder<S> { +) -> ArgumentBuilder<S, R> { ArgumentBuilder::new(Argument::new(name, Arc::new(parser), None).into()) } -impl<S> Clone for Argument<S> { +impl<S, R> Clone for Argument<S, R> { fn clone(&self) -> Self { Self { name: self.name.clone(), |
