From 53d51a5ca92aa8ddea9d82b6b44ac7aaa06c2095 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 7 May 2023 02:50:52 -0500 Subject: more brigadier docs --- azalea-brigadier/src/command_dispatcher.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'azalea-brigadier/src/command_dispatcher.rs') diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index a14b5009..595bc57d 100755 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -11,6 +11,12 @@ use crate::{ use std::{cmp::Ordering, collections::HashMap, mem, rc::Rc, sync::Arc}; /// The root of the command tree. You need to make this to register commands. +/// +/// ``` +/// # use azalea_brigadier::prelude::*; +/// # struct CommandSource; +/// let mut subject = CommandDispatcher::::new(); +/// ``` pub struct CommandDispatcher where Self: Sync + Send, @@ -25,13 +31,22 @@ impl CommandDispatcher { } } + /// Add a new node to the root. + /// + /// ``` + /// # use azalea_brigadier::prelude::*; + /// # let mut subject = CommandDispatcher::<()>::new(); + /// subject.register(literal("foo").executes(|_| 42)); + /// ``` pub fn register(&mut self, node: ArgumentBuilder) -> Arc>> { let build = Arc::new(RwLock::new(node.build())); self.root.write().add_child(&build); build } - pub fn parse(&self, command: StringReader, source: Arc) -> ParseResults { + pub fn parse(&self, command: StringReader, source: S) -> ParseResults { + let source = Arc::new(source); + let context = CommandContextBuilder::new(self, source, self.root.clone(), command.cursor()); self.parse_nodes(&self.root, &command, context).unwrap() } @@ -140,11 +155,17 @@ impl CommandDispatcher { }) } + /// Parse and execute the command using the given input and context. The + /// number returned depends on the command, and may not be of significance. + /// + /// This is a shortcut for `Self::parse` and `Self::execute_parsed`. pub fn execute( &self, - input: StringReader, - source: Arc, + input: impl Into, + source: S, ) -> Result { + let input = input.into(); + let parse = self.parse(input, source); Self::execute_parsed(parse) } -- cgit v1.2.3