diff options
| author | mat <git@matdoes.dev> | 2023-05-07 01:55:08 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-05-07 01:55:08 -0500 |
| commit | 2823e508b389194ee1d8b3f4180fc3a15a7e077e (patch) | |
| tree | d7133d1344b143cad7df1846efc12f092835513e | |
| parent | 84c0908f34b301c6ddb96ae4c314672f690f1bff (diff) | |
| download | azalea-drasl-2823e508b389194ee1d8b3f4180fc3a15a7e077e.tar.xz | |
more brigadier argument types
| -rw-r--r-- | azalea-brigadier/src/arguments/bool_argument_type.rs | 21 | ||||
| -rw-r--r-- | azalea-brigadier/src/arguments/double_argument_type.rs | 54 | ||||
| -rw-r--r-- | azalea-brigadier/src/arguments/float_argument_type.rs | 54 | ||||
| -rw-r--r--[-rwxr-xr-x] | azalea-brigadier/src/arguments/integer_argument_type.rs | 0 | ||||
| -rw-r--r-- | azalea-brigadier/src/arguments/long_argument_type.rs | 54 | ||||
| -rwxr-xr-x | azalea-brigadier/src/arguments/mod.rs | 5 | ||||
| -rw-r--r-- | azalea-brigadier/src/arguments/string_argument_type.rs | 53 | ||||
| -rwxr-xr-x | azalea-brigadier/src/lib.rs | 12 | ||||
| -rwxr-xr-x | azalea-protocol/src/packets/game/clientbound_login_packet.rs | 2 |
9 files changed, 255 insertions, 0 deletions
diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs new file mode 100644 index 00000000..01828c87 --- /dev/null +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -0,0 +1,21 @@ +use std::{any::Any, rc::Rc}; + +use crate::{ + context::CommandContext, exceptions::CommandSyntaxException, string_reader::StringReader, +}; + +use super::ArgumentType; + +impl ArgumentType for bool { + fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> { + Ok(Rc::new(reader.read_boolean())) + } +} + +pub fn get_bool<'a, S>(context: &'a CommandContext<S>, name: &str) -> Option<bool> { + context + .argument(name) + .unwrap() + .downcast_ref::<bool>() + .cloned() +} diff --git a/azalea-brigadier/src/arguments/double_argument_type.rs b/azalea-brigadier/src/arguments/double_argument_type.rs new file mode 100644 index 00000000..117a11cc --- /dev/null +++ b/azalea-brigadier/src/arguments/double_argument_type.rs @@ -0,0 +1,54 @@ +use std::{any::Any, rc::Rc}; + +use crate::{ + context::CommandContext, + exceptions::{BuiltInExceptions, CommandSyntaxException}, + string_reader::StringReader, +}; + +use super::ArgumentType; + +#[derive(Default)] +struct Double { + pub minimum: Option<f64>, + pub maximum: Option<f64>, +} + +impl ArgumentType for Double { + fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> { + let start = reader.cursor; + let result = reader.read_double()?; + if let Some(minimum) = self.minimum { + if result < minimum { + reader.cursor = start; + return Err(BuiltInExceptions::DoubleTooSmall { + found: result, + min: minimum, + } + .create_with_context(reader)); + } + } + if let Some(maximum) = self.maximum { + if result > maximum { + reader.cursor = start; + return Err(BuiltInExceptions::DoubleTooBig { + found: result, + max: maximum, + } + .create_with_context(reader)); + } + } + Ok(Rc::new(result)) + } +} + +pub fn double() -> impl ArgumentType { + Double::default() +} +pub fn get_integer<S>(context: &CommandContext<S>, name: &str) -> Option<f64> { + context + .argument(name) + .unwrap() + .downcast_ref::<f64>() + .copied() +} diff --git a/azalea-brigadier/src/arguments/float_argument_type.rs b/azalea-brigadier/src/arguments/float_argument_type.rs new file mode 100644 index 00000000..f335f11a --- /dev/null +++ b/azalea-brigadier/src/arguments/float_argument_type.rs @@ -0,0 +1,54 @@ +use std::{any::Any, rc::Rc}; + +use crate::{ + context::CommandContext, + exceptions::{BuiltInExceptions, CommandSyntaxException}, + string_reader::StringReader, +}; + +use super::ArgumentType; + +#[derive(Default)] +struct Float { + pub minimum: Option<f32>, + pub maximum: Option<f32>, +} + +impl ArgumentType for Float { + fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> { + let start = reader.cursor; + let result = reader.read_float()?; + if let Some(minimum) = self.minimum { + if result < minimum { + reader.cursor = start; + return Err(BuiltInExceptions::FloatTooSmall { + found: result, + min: minimum, + } + .create_with_context(reader)); + } + } + if let Some(maximum) = self.maximum { + if result > maximum { + reader.cursor = start; + return Err(BuiltInExceptions::FloatTooBig { + found: result, + max: maximum, + } + .create_with_context(reader)); + } + } + Ok(Rc::new(result)) + } +} + +pub fn float() -> impl ArgumentType { + Float::default() +} +pub fn get_integer<S>(context: &CommandContext<S>, name: &str) -> Option<f32> { + context + .argument(name) + .unwrap() + .downcast_ref::<f32>() + .copied() +} diff --git a/azalea-brigadier/src/arguments/integer_argument_type.rs b/azalea-brigadier/src/arguments/integer_argument_type.rs index 336046a7..336046a7 100755..100644 --- a/azalea-brigadier/src/arguments/integer_argument_type.rs +++ b/azalea-brigadier/src/arguments/integer_argument_type.rs diff --git a/azalea-brigadier/src/arguments/long_argument_type.rs b/azalea-brigadier/src/arguments/long_argument_type.rs new file mode 100644 index 00000000..435dd92a --- /dev/null +++ b/azalea-brigadier/src/arguments/long_argument_type.rs @@ -0,0 +1,54 @@ +use std::{any::Any, rc::Rc}; + +use crate::{ + context::CommandContext, + exceptions::{BuiltInExceptions, CommandSyntaxException}, + string_reader::StringReader, +}; + +use super::ArgumentType; + +#[derive(Default)] +struct Long { + pub minimum: Option<i64>, + pub maximum: Option<i64>, +} + +impl ArgumentType for Long { + fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> { + let start = reader.cursor; + let result = reader.read_long()?; + if let Some(minimum) = self.minimum { + if result < minimum { + reader.cursor = start; + return Err(BuiltInExceptions::LongTooSmall { + found: result, + min: minimum, + } + .create_with_context(reader)); + } + } + if let Some(maximum) = self.maximum { + if result > maximum { + reader.cursor = start; + return Err(BuiltInExceptions::LongTooBig { + found: result, + max: maximum, + } + .create_with_context(reader)); + } + } + Ok(Rc::new(result)) + } +} + +pub fn long() -> impl ArgumentType { + Long::default() +} +pub fn get_integer<S>(context: &CommandContext<S>, name: &str) -> Option<i64> { + context + .argument(name) + .unwrap() + .downcast_ref::<i64>() + .copied() +} diff --git a/azalea-brigadier/src/arguments/mod.rs b/azalea-brigadier/src/arguments/mod.rs index dec39297..9d2c2cd0 100755 --- a/azalea-brigadier/src/arguments/mod.rs +++ b/azalea-brigadier/src/arguments/mod.rs @@ -1,4 +1,9 @@ mod argument_type; +pub mod bool_argument_type; +pub mod double_argument_type; +pub mod float_argument_type; pub mod integer_argument_type; +pub mod long_argument_type; +pub mod string_argument_type; pub use argument_type::ArgumentType; diff --git a/azalea-brigadier/src/arguments/string_argument_type.rs b/azalea-brigadier/src/arguments/string_argument_type.rs new file mode 100644 index 00000000..f23af3ba --- /dev/null +++ b/azalea-brigadier/src/arguments/string_argument_type.rs @@ -0,0 +1,53 @@ +use std::{any::Any, rc::Rc}; + +use crate::{ + context::CommandContext, exceptions::CommandSyntaxException, string_reader::StringReader, +}; + +use super::ArgumentType; + +pub enum StringArgument { + /// Match up until the next space. + SingleWord, + /// Same as single word unless the argument is wrapped in quotes, in which + /// case it can contain spaces. + QuotablePhrase, + /// Match the rest of the input. + GreedyPhrase, +} + +impl ArgumentType for StringArgument { + fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> { + let result = match self { + StringArgument::SingleWord => reader.read_unquoted_string().to_string(), + StringArgument::QuotablePhrase => reader.read_string()?, + StringArgument::GreedyPhrase => { + let text = reader.remaining().to_string(); + reader.cursor = reader.total_length(); + text + } + }; + Ok(Rc::new(result)) + } +} + +/// Match up until the next space. +pub fn word() -> impl ArgumentType { + StringArgument::SingleWord +} +/// Same as single word unless the argument is wrapped in quotes, in which case +/// it can contain spaces. +pub fn string() -> impl ArgumentType { + StringArgument::QuotablePhrase +} +/// Match the rest of the input. +pub fn greedy_string() -> impl ArgumentType { + StringArgument::GreedyPhrase +} +pub fn get_string<'a, S>(context: &'a CommandContext<S>, name: &str) -> Option<String> { + context + .argument(name) + .unwrap() + .downcast_ref::<String>() + .cloned() +} diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs index eb670643..8b8e116e 100755 --- a/azalea-brigadier/src/lib.rs +++ b/azalea-brigadier/src/lib.rs @@ -10,3 +10,15 @@ pub mod parse_results; pub mod string_reader; pub mod suggestion; pub mod tree; + +pub mod prelude { + pub use crate::{ + arguments::{ + double_argument_type::double, float_argument_type::float, + integer_argument_type::integer, long_argument_type::long, string_argument_type::string, + }, + builder::{literal_argument_builder::literal, required_argument_builder::argument}, + command_dispatcher::CommandDispatcher, + context::CommandContext, + }; +} diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index bafead86..346c24fb 100755 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -253,6 +253,8 @@ pub mod registry { pub struct DimensionTypeElement { pub height: u32, pub min_y: i32, + #[serde(flatten)] + pub _extra: HashMap<String, Nbt>, } /// The light level at which monsters can spawn. |
