Skip to content

Commit

Permalink
Merge branch 'main' into 1.20.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Oct 27, 2023
2 parents 51df0aa + ce81ae9 commit 4523064
Show file tree
Hide file tree
Showing 67 changed files with 1,582 additions and 197 deletions.
14 changes: 13 additions & 1 deletion azalea-brigadier/src/arguments/argument_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use std::{any::Any, sync::Arc};

use crate::{exceptions::CommandSyntaxException, string_reader::StringReader};
use crate::{
exceptions::CommandSyntaxException,
string_reader::StringReader,
suggestion::{Suggestions, SuggestionsBuilder},
};

pub trait ArgumentType {
fn parse(&self, reader: &mut StringReader) -> Result<Arc<dyn Any>, CommandSyntaxException>;

fn list_suggestions(&self, _builder: SuggestionsBuilder) -> Suggestions {
Suggestions::default()
}

fn examples(&self) -> Vec<String> {
vec![]
}
}
19 changes: 18 additions & 1 deletion azalea-brigadier/src/arguments/bool_argument_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{any::Any, sync::Arc};

use crate::{
context::CommandContext, exceptions::CommandSyntaxException, string_reader::StringReader,
context::CommandContext,
exceptions::CommandSyntaxException,
string_reader::StringReader,
suggestion::{Suggestions, SuggestionsBuilder},
};

use super::ArgumentType;
Expand All @@ -13,6 +16,20 @@ impl ArgumentType for Boolean {
fn parse(&self, reader: &mut StringReader) -> Result<Arc<dyn Any>, CommandSyntaxException> {
Ok(Arc::new(reader.read_boolean()?))
}

fn list_suggestions(&self, mut builder: SuggestionsBuilder) -> Suggestions {
if "true".starts_with(builder.remaining_lowercase()) {
builder = builder.suggest("true");
}
if "false".starts_with(builder.remaining_lowercase()) {
builder = builder.suggest("false");
}
builder.build()
}

fn examples(&self) -> Vec<String> {
vec!["true".to_string(), "false".to_string()]
}
}

pub fn bool() -> impl ArgumentType {
Expand Down
7 changes: 7 additions & 0 deletions azalea-brigadier/src/arguments/double_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ impl ArgumentType for Double {
}
Ok(Arc::new(result))
}

fn examples(&self) -> Vec<String> {
vec!["0", "1.2", ".5", "-1", "-.5", "-1234.56"]
.into_iter()
.map(|s| s.to_string())
.collect()
}
}

pub fn double() -> impl ArgumentType {
Expand Down
7 changes: 7 additions & 0 deletions azalea-brigadier/src/arguments/float_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ impl ArgumentType for Float {
}
Ok(Arc::new(result))
}

fn examples(&self) -> Vec<String> {
vec!["0", "1.2", ".5", "-1", "-.5", "-1234.56"]
.into_iter()
.map(|s| s.to_string())
.collect()
}
}

pub fn float() -> impl ArgumentType {
Expand Down
7 changes: 7 additions & 0 deletions azalea-brigadier/src/arguments/integer_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ impl ArgumentType for Integer {
}
Ok(Arc::new(result))
}

fn examples(&self) -> Vec<String> {
vec!["0", "123", "-123"]
.into_iter()
.map(|s| s.to_string())
.collect()
}
}

pub fn integer() -> impl ArgumentType {
Expand Down
7 changes: 7 additions & 0 deletions azalea-brigadier/src/arguments/long_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ impl ArgumentType for Long {
}
Ok(Arc::new(result))
}

fn examples(&self) -> Vec<String> {
vec!["0", "123", "-123"]
.into_iter()
.map(|s| s.to_string())
.collect()
}
}

pub fn long() -> impl ArgumentType {
Expand Down
11 changes: 11 additions & 0 deletions azalea-brigadier/src/arguments/string_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ impl ArgumentType for StringArgument {
};
Ok(Arc::new(result))
}

fn examples(&self) -> Vec<String> {
match self {
StringArgument::SingleWord => vec!["word", "words_with_underscores"],
StringArgument::QuotablePhrase => vec!["\"quoted phrase\"", "word", "\"\""],
StringArgument::GreedyPhrase => vec!["word", "words with spaces", "\"and symbols\""],
}
.into_iter()
.map(|s| s.to_string())
.collect()
}
}

/// Match up until the next space.
Expand Down
11 changes: 8 additions & 3 deletions azalea-brigadier/src/builder/argument_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ pub enum ArgumentBuilderType {
}

/// A node that hasn't yet been built.
#[derive(Clone)]
pub struct ArgumentBuilder<S> {
arguments: CommandNode<S>,

command: Command<S>,
requirement: Arc<dyn Fn(Arc<S>) -> bool + Send + Sync>,
requirement: Arc<dyn Fn(&S) -> bool + Send + Sync>,
target: Option<Arc<RwLock<CommandNode<S>>>>,

forks: bool,
Expand Down Expand Up @@ -95,13 +96,13 @@ impl<S> ArgumentBuilder<S> {
/// # let mut subject = CommandDispatcher::<CommandSource>::new();
/// # subject.register(
/// literal("foo")
/// .requires(|s: Arc<CommandSource>| s.opped)
/// .requires(|s: &CommandSource| s.opped)
/// // ...
/// # .executes(|ctx: &CommandContext<CommandSource>| 42)
/// # );
pub fn requires<F>(mut self, requirement: F) -> Self
where
F: Fn(Arc<S>) -> bool + Send + Sync + 'static,
F: Fn(&S) -> bool + Send + Sync + 'static,
{
self.requirement = Arc::new(requirement);
self
Expand Down Expand Up @@ -134,6 +135,10 @@ impl<S> ArgumentBuilder<S> {
self
}

pub fn arguments(&self) -> &CommandNode<S> {
&self.arguments
}

/// Manually build this node into a [`CommandNode`]. You probably don't need
/// to do this yourself.
pub fn build(self) -> CommandNode<S> {
Expand Down
16 changes: 15 additions & 1 deletion azalea-brigadier/src/builder/required_argument_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::argument_builder::{ArgumentBuilder, ArgumentBuilderType};
use crate::{
arguments::ArgumentType, exceptions::CommandSyntaxException, string_reader::StringReader,
arguments::ArgumentType,
exceptions::CommandSyntaxException,
string_reader::StringReader,
suggestion::{Suggestions, SuggestionsBuilder},
};
use std::{any::Any, fmt::Debug, sync::Arc};

Expand All @@ -22,6 +25,17 @@ impl Argument {
pub fn parse(&self, reader: &mut StringReader) -> Result<Arc<dyn Any>, CommandSyntaxException> {
self.parser.parse(reader)
}

pub fn list_suggestions(&self, builder: SuggestionsBuilder) -> Suggestions {
// TODO: custom suggestions
// https://github.com/Mojang/brigadier/blob/master/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java#L71

self.parser.list_suggestions(builder)
}

pub fn examples(&self) -> Vec<String> {
self.parser.examples()
}
}

impl From<Argument> for ArgumentBuilderType {
Expand Down
Loading

0 comments on commit 4523064

Please sign in to comment.