From a5e7ff771d657258cedcc7a8b3ce265c655f0860 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 30 May 2025 20:07:28 -0330 Subject: implement missing brigadier features and cleanup some more --- azalea-brigadier/src/context/command_context.rs | 72 ++++++++++++++++++++----- 1 file changed, 59 insertions(+), 13 deletions(-) (limited to 'azalea-brigadier/src/context/command_context.rs') diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index 202d6f21..224f2d63 100644 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -10,16 +10,16 @@ use crate::{ /// A built `CommandContextBuilder`. pub struct CommandContext { - pub source: Arc, - pub input: String, - pub arguments: HashMap, - pub command: Command, - pub root_node: Arc>>, - pub nodes: Vec>, - pub range: StringRange, - pub child: Option>>, - pub modifier: Option>>, - pub forks: bool, + pub(super) source: Arc, + pub(super) input: String, + pub(super) arguments: HashMap, + pub(super) command: Command, + pub(super) root_node: Arc>>, + pub(super) nodes: Vec>, + pub(super) range: StringRange, + pub(super) child: Option>>, + pub(super) modifier: Option>>, + pub(super) forks: bool, } impl Clone for CommandContext { @@ -59,8 +59,10 @@ impl Debug for CommandContext { impl CommandContext { pub fn copy_for(&self, source: Arc) -> Self { if Arc::ptr_eq(&source, &self.source) { + // fast path return self.clone(); } + CommandContext { source, input: self.input.clone(), @@ -75,12 +77,56 @@ impl CommandContext { } } + pub fn child(&self) -> Option<&CommandContext> { + self.child.as_ref().map(|c| c.as_ref()) + } + + pub fn last_child(&self) -> &CommandContext { + let mut result = self; + while let Some(child) = result.child() { + result = child; + } + result + } + + pub fn command(&self) -> &Command { + &self.command + } + + pub fn source(&self) -> &Arc { + &self.source + } + + pub fn argument(&self, name: &str) -> Option<&dyn Any> { + let argument = self.arguments.get(name); + argument.map(|a| a.result.as_ref()) + } + + pub fn redirect_modifier(&self) -> Option<&RedirectModifier> { + self.modifier.as_ref().map(|m| m.as_ref()) + } + + pub fn range(&self) -> &StringRange { + &self.range + } + + pub fn input(&self) -> &str { + &self.input + } + + pub fn root_node(&self) -> &Arc>> { + &self.root_node + } + + pub fn nodes(&self) -> &[ParsedCommandNode] { + &self.nodes + } + pub fn has_nodes(&self) -> bool { !self.nodes.is_empty() } - pub fn argument(&self, name: &str) -> Option> { - let argument = self.arguments.get(name); - argument.map(|a| a.result.clone()) + pub fn is_forked(&self) -> bool { + self.forks } } -- cgit v1.2.3