From 6f3c41e01c466e4ab6abd1268149dd88686bbbc9 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 17 Apr 2022 16:52:34 -0500 Subject: add .requires --- azalea-brigadier/src/builder/argument_builder.rs | 8 ++++ azalea-brigadier/src/dispatcher.rs | 51 +++++++++++++++++----- .../src/exceptions/command_syntax_exception.rs | 2 +- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index 6f23457a..11c0062c 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -66,6 +66,14 @@ impl ArgumentBuilder { self.clone() } + pub fn requires(&mut self, requirement: F) -> Self + where + F: Fn(Rc) -> bool + 'static, + { + self.requirement = Rc::new(requirement); + self.clone() + } + pub fn build(self) -> CommandNode { CommandNode { value: self.value, diff --git a/azalea-brigadier/src/dispatcher.rs b/azalea-brigadier/src/dispatcher.rs index 029f0ed0..683348e3 100644 --- a/azalea-brigadier/src/dispatcher.rs +++ b/azalea-brigadier/src/dispatcher.rs @@ -330,18 +330,45 @@ mod tests { // assertThat(ex.getCursor(), is(0)); // } // } - // #[test] - // fn execute_unknown_command() { - // let mut subject = CommandDispatcher::>::new(); - // subject.register(literal("bar")); - // subject.register(literal("baz")); + #[test] + fn execute_unknown_command() { + let mut subject = CommandDispatcher::>::new(); + subject.register(literal("bar")); + subject.register(literal("baz")); + + let execute_result = subject.execute("foo".into(), Rc::new(CommandSource {})); - // assert_eq!( - // subject - // .execute("foo".into(), Rc::new(CommandSource {})) - // .err() - // .unwrap(), - // BuiltInExceptions::DispatcherUnknownCommand.create() - // ); + let err = execute_result.err().unwrap(); + match err.type_ { + BuiltInExceptions::DispatcherUnknownCommand => {} + _ => panic!("Unexpected error"), + } + assert_eq!(err.cursor().unwrap(), 0); + } + // @Test + // public void testExecuteImpermissibleCommand() throws Exception { + // subject.register(literal("foo").requires(s -> false)); + + // try { + // subject.execute("foo", source); + // fail(); + // } catch (final CommandSyntaxException ex) { + // assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand())); + // assertThat(ex.getCursor(), is(0)); + // } // } + #[test] + fn execute_impermissible_command() { + let mut subject = CommandDispatcher::>::new(); + subject.register(literal("foo").requires(|_| false)); + + let execute_result = subject.execute("foo".into(), Rc::new(CommandSource {})); + + let err = execute_result.err().unwrap(); + match err.type_ { + BuiltInExceptions::DispatcherUnknownCommand => {} + _ => panic!("Unexpected error"), + } + assert_eq!(err.cursor().unwrap(), 0); + } } diff --git a/azalea-brigadier/src/exceptions/command_syntax_exception.rs b/azalea-brigadier/src/exceptions/command_syntax_exception.rs index 93ac788c..4bfe9cda 100644 --- a/azalea-brigadier/src/exceptions/command_syntax_exception.rs +++ b/azalea-brigadier/src/exceptions/command_syntax_exception.rs @@ -5,7 +5,7 @@ use crate::message::Message; #[derive(Clone, PartialEq)] pub struct CommandSyntaxException { - type_: BuiltInExceptions, + pub type_: BuiltInExceptions, message: Message, input: Option, cursor: Option, -- cgit v1.2.3