Struct valence_command::graph::CommandGraphBuilder

source ·
pub struct CommandGraphBuilder<'a, T> { /* private fields */ }
Expand description

Ergonomic builder pattern for adding executables, literals and arguments to a command graph. See the derive macro for a more ergonomic way of doing this for a basic command with an enum or struct.

§Type Parameters

  • T - the type that should be constructed by an executable when the command is executed

§Example

use std::collections::HashMap;
use petgraph::visit::{EdgeCount, NodeCount};
use valence_command::graph::{
    CommandGraph, CommandGraphBuilder
};
use valence_command::{CommandRegistry};
use valence_command::parsers::CommandArg;

struct TestCommand {
   test: i32,
}

let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder = CommandGraphBuilder::<TestCommand>::new(&mut command_graph, &mut executable_map, &mut parser_map, &mut modifier_map);

// simple command
let simple_command = command_graph_builder
   .root() // transition to the root node
   .literal("test") // add a literal node then transition to it
   .argument("test")
   // a player needs one of these scopes to execute the command
   // (note: if you want an admin scope you should use the link method on the scope registry.)
   .with_scopes(vec!["test:admin", "command:test"])
   .with_parser::<i32>()
   // it is reasonably safe to unwrap here because we know that the argument is an integer
   .with_executable(|args| TestCommand { test: i32::parse_arg(args).unwrap() })
   .id();

// complex command (redirects back to the simple command)
command_graph_builder
    .root()
    .literal("test")
    .literal("command")
    .redirect_to(simple_command);

assert_eq!(command_graph.graph.graph.node_count(), 5); // root, test, command, <test>, test
// 5 edges, 2 for the simple command, 2 for the complex command and 1 for the redirect
assert_eq!(command_graph.graph.graph.edge_count(), 5);

in this example we can execute either of the following commands for the same result:

  • /test test 1
  • /test command test 1

the executables from these commands will both return a TestCommand with the value 1

Implementations§

source§

impl<'a, T> CommandGraphBuilder<'a, T>

source

pub fn new( registry: &'a mut CommandRegistry, executables: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> T>, parsers: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> bool>, modifiers: &'a mut HashMap<NodeIndex, fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>)>, ) -> Self

Creates a new command graph builder

§Arguments
  • registry - the command registry to add the commands to
  • executables - the map of node indices to executable parser functions
source

pub fn root(&mut self) -> &mut Self

Transitions to the root node. Use this to start building a new command from root.

source

pub fn literal<S: Into<String>>(&mut self, literal: S) -> &mut Self

Creates a new literal node and transitions to it.

§Default Values
  • executable - false
  • scopes - Vec::new()
source

pub fn argument<A: Into<String>>(&mut self, argument: A) -> &mut Self

Creates a new argument node and transitions to it.

§Default Values
  • executable - false
  • scopes - Vec::new()
  • parser - StringArg::SingleWord
  • suggestion - None
source

pub fn redirect_to(&mut self, node: NodeIndex) -> &mut Self

Creates a new redirect edge from the current node to the node specified. For info on what a redirect edge is, see the module level documentation.

§Example
use std::collections::HashMap;

use valence_command::graph::CommandGraphBuilder;
use valence_command::CommandRegistry;

struct TestCommand;

let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder = CommandGraphBuilder::<TestCommand>::new(
    &mut command_graph,
    &mut executable_map,
    &mut parser_map,
    &mut modifier_map,
);

let simple_command = command_graph_builder
  .root() // transition to the root node
  .literal("test") // add a literal node then transition to it
  .id(); // get the id of the literal node

command_graph_builder
    .root() // transition to the root node
    .literal("test") // add a literal node then transition to it
    .literal("command") // add a literal node then transition to it
    .redirect_to(simple_command); // redirect to the simple command
source

pub fn with_executable( &mut self, executable: fn(_: &mut ParseInput<'_>) -> T, ) -> &mut Self

Sets up the executable function for the current node. This function will be called when the command is executed and should parse the args and return the T type.

§Arguments
  • executable - the executable function to add
§Example

have a look at the example for CommandGraphBuilder

source

pub fn with_modifier( &mut self, modifier: fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>), ) -> &mut Self

Adds a modifier to the current node

§Arguments
  • modifier - the modifier function to add
§Example
use std::collections::HashMap;

use valence_command::graph::CommandGraphBuilder;
use valence_command::CommandRegistry;

struct TestCommand;

let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder =
   CommandGraphBuilder::<TestCommand>::new(&mut command_graph, &mut executable_map, &mut parser_map, &mut modifier_map);

command_graph_builder
    .root() // transition to the root node
    .literal("test") // add a literal node then transition to it
    .with_modifier(|_, modifiers| {
       modifiers.insert("test".into(), "test".into()); // this will trigger when the node is passed
    })
    .literal("command") // add a literal node then transition to it
    .with_executable(|_| TestCommand);
source

pub fn with_scopes<S: Into<String>>(&mut self, scopes: Vec<S>) -> &mut Self

Sets the required scopes for the current node

§Arguments
  • scopes - a list of scopes for that are aloud to access a command node and its children (list of strings following the system described in scopes)
source

pub fn apply_scopes(&mut self, registry: &mut CommandScopeRegistry) -> &mut Self

Applies the scopes to the registry

§Arguments
  • registry - the registry to apply the scopes to
source

pub fn with_parser<P: CommandArg>(&mut self) -> &mut Self

Sets the parser for the current node. This will decide how the argument is parsed client side and will be used to check the argument before it is passed to the executable. The node should be an argument node or nothing will happen.

§Type Parameters
  • P - the parser to use for the current node (must be CommandArg)
source

pub fn at(&mut self, node: NodeIndex) -> &mut Self

Transitions to the node specified.

source

pub fn id(&self) -> NodeIndex

Gets the id of the current node (useful for commands that have multiple children).

Auto Trait Implementations§

§

impl<'a, T> Freeze for CommandGraphBuilder<'a, T>

§

impl<'a, T> RefUnwindSafe for CommandGraphBuilder<'a, T>

§

impl<'a, T> Send for CommandGraphBuilder<'a, T>

§

impl<'a, T> Sync for CommandGraphBuilder<'a, T>

§

impl<'a, T> Unpin for CommandGraphBuilder<'a, T>

§

impl<'a, T> !UnwindSafe for CommandGraphBuilder<'a, T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ConditionalSend for T
where T: Send,