more brigadier argument types

This commit is contained in:
mat 2023-05-07 01:55:08 -05:00
parent 84c0908f34
commit 2823e508b3
9 changed files with 255 additions and 0 deletions

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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()
}

View file

View file

@ -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()
}

View file

@ -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;

View file

@ -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()
}

View file

@ -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,
};
}

View file

@ -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.