aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--azalea-brigadier/src/builder/argument_builder.rs8
-rw-r--r--azalea-brigadier/src/dispatcher.rs51
-rw-r--r--azalea-brigadier/src/exceptions/command_syntax_exception.rs2
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<S: Any + Clone> ArgumentBuilder<S> {
self.clone()
}
+ pub fn requires<F>(&mut self, requirement: F) -> Self
+ where
+ F: Fn(Rc<S>) -> bool + 'static,
+ {
+ self.requirement = Rc::new(requirement);
+ self.clone()
+ }
+
pub fn build(self) -> CommandNode<S> {
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::<Rc<CommandSource>>::new();
- // subject.register(literal("bar"));
- // subject.register(literal("baz"));
+ #[test]
+ fn execute_unknown_command() {
+ let mut subject = CommandDispatcher::<Rc<CommandSource>>::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::<Rc<CommandSource>>::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<String>,
cursor: Option<usize>,