diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index f17fe4fa..3c9ae3ed 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -1,7 +1,7 @@ use crate::{context::CommandContext, modifier::RedirectModifier, tree::CommandNode}; use super::{literal_argument_builder::Literal, required_argument_builder::Argument}; -use std::{any::Any, cell::RefCell, collections::BTreeMap, fmt::Debug, rc::Rc}; +use std::{any::Any, cell::RefCell, fmt::Debug, rc::Rc}; #[derive(Debug, Clone)] pub enum ArgumentBuilderType { @@ -38,6 +38,7 @@ impl ArgumentBuilder { } } + // do we need to be cloning here? maybe we could return a ref to self? pub fn then(&mut self, argument: ArgumentBuilder) -> Self { self.arguments .add_child(&Rc::new(RefCell::new(argument.build()))); @@ -82,10 +83,10 @@ impl ArgumentBuilder { pub fn build(self) -> CommandNode { let mut result = CommandNode { value: self.arguments.value, - command: self.command.clone(), - requirement: self.requirement.clone(), - redirect: self.target.clone(), - modifier: self.modifier.clone(), + command: self.command, + requirement: self.requirement, + redirect: self.target, + modifier: self.modifier, forks: self.forks, ..Default::default() }; diff --git a/azalea-brigadier/src/dispatcher.rs b/azalea-brigadier/src/dispatcher.rs index a8ca2ec9..b62455f5 100644 --- a/azalea-brigadier/src/dispatcher.rs +++ b/azalea-brigadier/src/dispatcher.rs @@ -104,6 +104,11 @@ impl CommandDispatcher { .parse_nodes(redirect, &reader, child_context) .expect("Parsing nodes failed"); context.with_child(Rc::new(parse.context)); + return Ok(ParseResults { + context, + reader: parse.reader, + exceptions: parse.exceptions, + }); } else { let parse = self .parse_nodes(&child, &reader, context) @@ -629,25 +634,25 @@ mod tests { // verify(subCommand).run(any(CommandContext.class)); // verify(command, never()).run(any()); // } - // #[test] - // fn test_execute_ambiguious_parent_subcommand_via_redirect() { - // let mut subject = CommandDispatcher::new(); + #[test] + fn test_execute_ambiguious_parent_subcommand_via_redirect() { + let mut subject = CommandDispatcher::new(); - // let real = subject.register( - // literal("test") - // .then(argument("incorrect", integer()).executes(|_| 42)) - // .then( - // argument("right", integer()).then(argument("sub", integer()).executes(|_| 100)), - // ), - // ); + let real = subject.register( + literal("test") + .then(argument("incorrect", integer()).executes(|_| 42)) + .then( + argument("right", integer()).then(argument("sub", integer()).executes(|_| 100)), + ), + ); - // subject.register(literal("redirect").redirect(real)); + subject.register(literal("redirect").redirect(real)); - // assert_eq!( - // subject - // .execute("redirect 1 2".into(), Rc::new(CommandSource {})) - // .unwrap(), - // 100 - // ); - // } + assert_eq!( + subject + .execute("redirect 1 2".into(), Rc::new(CommandSource {})) + .unwrap(), + 100 + ); + } } diff --git a/azalea-brigadier/src/tree.rs b/azalea-brigadier/src/tree.rs index 5c33a879..b68fbdcf 100644 --- a/azalea-brigadier/src/tree.rs +++ b/azalea-brigadier/src/tree.rs @@ -32,15 +32,6 @@ pub struct CommandNode { } impl CommandNode { - // pub fn new() - // TODO: precalculate `literals` and `arguments` and include them in CommandNode - fn literals(&self) -> &BTreeMap>>> { - &self.literals - } - fn arguments(&self) -> &BTreeMap>>> { - &self.arguments - } - /// Gets the literal, or panics. You should use match if you're not certain about the type. pub fn literal(&self) -> &Literal { match self.value { @@ -57,7 +48,7 @@ impl CommandNode { } pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec>>> { - let literals = self.literals(); + let literals = &self.literals; if !literals.is_empty() { let cursor = input.cursor(); @@ -75,10 +66,10 @@ impl CommandNode { if let Some(literal) = literal { return vec![literal.clone()]; } else { - return self.arguments().values().cloned().collect(); + return self.arguments.values().cloned().collect(); } } else { - self.arguments().values().cloned().collect() + self.arguments.values().cloned().collect() } } @@ -245,8 +236,7 @@ impl PartialEq for CommandNode { } else { return false; } - } - else if other.command.is_some() { + } else if other.command.is_some() { return false; } true