diff options
| author | mat <github@matdoes.dev> | 2022-01-09 22:33:45 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-09 22:33:45 -0600 |
| commit | 315f2258190b33c63df7797a97178019f5aea02b (patch) | |
| tree | 1ec5f467e7bd42f7aed3aaadbcbc226f8a6ce4f2 /azalea-brigadier/src/context/command_context.rs | |
| parent | d56f60c05f316ab4cc37ebe7a9ad4caf91a75de6 (diff) | |
| download | azalea-drasl-315f2258190b33c63df7797a97178019f5aea02b.tar.xz | |
add some more stuff from brigadier
Diffstat (limited to 'azalea-brigadier/src/context/command_context.rs')
| -rw-r--r-- | azalea-brigadier/src/context/command_context.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index ddbb447e..c6210a88 100644 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -1,3 +1,90 @@ +use super::{ + parsed_argument::ParsedArgument, parsed_command_node::ParsedCommandNode, + string_range::StringRange, +}; +use crate::{ + arguments::argument_type::ArgumentType, command::Command, redirect_modifier::RedirectModifier, + tree::command_node::CommandNode, +}; +use std::collections::HashMap; + pub struct CommandContext<S> { source: S, + input: String, + command: dyn Command<S>, + arguments: HashMap<String, ParsedArgument<S, dyn ArgumentType>>, + root_node: dyn CommandNode<S>, + nodes: Vec<ParsedCommandNode<S>>, + range: StringRange, + child: Option<CommandContext<S>>, + modifier: Option<dyn RedirectModifier<S>>, + forks: bool, +} + +impl<S> CommandContext<S> { + pub fn clone_for(&self, source: S) -> Self { + if self.source == source { + return self.clone(); + } + Self { + source, + input: self.input.clone(), + command: self.command.clone(), + arguments: self.arguments.clone(), + root_node: self.root_node.clone(), + nodes: self.nodes.clone(), + range: self.range.clone(), + child: self.child.clone(), + modifier: self.modifier.clone(), + forks: self.forks, + } + } + + fn child(&self) -> &Option<CommandContext<S>> { + &self.child + } + + fn last_child(&self) -> &CommandContext<S> { + let mut result = self; + while result.child.is_some() { + result = result.child.as_ref().unwrap(); + } + result + } + + fn command(&self) -> &dyn Command<S> { + &self.command + } + + fn source(&self) -> &S { + &self.source + } + + // public <V> V getArgument(final String name, final Class<V> clazz) { + // final ParsedArgument<S, ?> argument = arguments.get(name); + + // if (argument == null) { + // throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); + // } + + // final Object result = argument.getResult(); + // if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) { + // return (V) result; + // } else { + // throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz); + // } + // } + fn get_argument<V>(&self, name: &str) -> Result<V, String> { + let argument = self.arguments.get(name); + + if argument.is_none() { + return Err(format!( + "No such argument '{}' exists on this command", + name + )); + } + + let result = argument.unwrap().result(); + Ok(result) + } } |
