Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command API #351

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ff803cd
Some descriptions
Jenya705 May 7, 2023
27f11bf
Some bugs
Jenya705 May 11, 2023
12b1ce3
Some objects
Jenya705 May 20, 2023
365d1c6
primitives, vectors, relative values, angles
Jenya705 May 29, 2023
858892e
BlockPredicate
Jenya705 Jun 2, 2023
da177ba
Merge branch 'valence-rs:main' into brigadier
Jenya705 Jun 3, 2023
5258fb0
merge fix
Jenya705 Jun 3, 2023
f58bf16
Some more api and Block argument
Jenya705 Jun 4, 2023
de3179d
Merge branch 'main' into brigadier
Jenya705 Jun 4, 2023
6f87ba6
CEnum trait
Jenya705 Jun 4, 2023
5f06bff
Merge branch 'brigadier' of github.com:Jenya705/valence into brigadier
Jenya705 Jun 4, 2023
ffa9a3a
GameMode
Jenya705 Jun 4, 2023
838f0f1
argument.rs -> many modules
Jenya705 Jun 5, 2023
485c5e7
Cenums + fixes
Jenya705 Jun 6, 2023
3f10625
Range
Jenya705 Jun 7, 2023
3bfb1d1
StrCursor contains chars and bytes offsets
Jenya705 Jun 7, 2023
9a51965
EntitySelector
Jenya705 Jun 11, 2023
663b88d
Readme.md, selectors PROP_NAMES
Jenya705 Jun 12, 2023
79c0116
Item
Jenya705 Jun 15, 2023
275f5d8
More convenient parse trait
Jenya705 Jun 17, 2023
34970c4
query -> param
Jenya705 Jun 17, 2023
9167640
Const array and Dyn array
Jenya705 Jun 17, 2023
ba7ff7c
Suggestion change
Jenya705 Jun 18, 2023
8492d70
Merge branch 'valence-rs:main' into brigadier
Jenya705 Jun 30, 2023
dc61fd4
Command exec system
Jenya705 Jun 30, 2023
dbd358c
Right command node tree and suggestion's event
Jenya705 Jul 4, 2023
d205de1
Callback execution systems, real nodes, compilation
Jenya705 Jul 24, 2023
aa0a1b5
Integer's parsers and a small tp command
Jenya705 Jul 24, 2023
1c555c2
tp zfloat, to prove that multiple parsers work
Jenya705 Jul 24, 2023
6d5ce1b
Merge branch 'valence-rs:main' into brigadier
Jenya705 Jul 27, 2023
fbfe2b9
Suggestions
Jenya705 Aug 5, 2023
2ea8f51
path fixes
Jenya705 Aug 5, 2023
e3ba4be
Parse::Item, last_entity fix
Jenya705 Aug 5, 2023
30da824
Nodes are now in graph
Jenya705 Aug 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions crates/valence_command/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# valence_command

Command API for minecraft with supported brigadier arguments.

## Raw usage

To use command API 'raw', you need to know `Parse` trait.

```rust
// Anything what can be &str
let input = "gamemode @s creative";

// StrReader is needed for Parse trait to work with.
let mut reader = StrReader::new(input);

// Here we are using Parse trait. It returns an ParsingResult (it is not a standard result)
// It has 2 fields. suggestions and result
// Result contains error or an object.
// Suggestions contains suggestions.
let gamemode_literal_result = <&str>::parse(None, reader, ParsingPurpose::Reading);
Jenya705 marked this conversation as resolved.
Show resolved Hide resolved

if let Err((err_pos, err)) = gamemode_literal_result.result {
// Handle error

// Component of error (usually this need to be sent to an user)
let text = err.build();

}

// str itself
let gamemode_literal = gamemode_literal_result.result.unwrap();

// to parse next argument you would need to skip a whitespace, because each Parse parse only its own chars.
// for now only "gamemode" parsed

if !reader.skip_char(' ') {
// next char is not whitespace otherwise it is skipped and returned true.
}

// continue parsing.
```
43 changes: 41 additions & 2 deletions crates/valence_command/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::num::NonZeroI32;

use valence_core::game_mode::GameMode;
use valence_core::text::Text;
use valence_core::translation_key::{
ARGUMENT_ENTITY_OPTIONS_INAPPLICABLE, ARGUMENT_ENTITY_OPTIONS_LIMIT_TOOSMALL,
ARGUMENT_ENTITY_OPTIONS_SORT_IRREVERSIBLE, ARGUMENT_ENTITY_OPTIONS_UNKNOWN,
Expand Down Expand Up @@ -274,10 +275,48 @@ impl From<EntitySelectorFlagSuggestions<CEnumSuggestions<GameMode>>> for EntityS
}
}

#[ctor::ctor]
static PROP_NAMES: [Suggestion<'static>; 21] = {
macro_rules! els {
($($f: expr,)*) => {
[$(Suggestion {
message: Cow::Borrowed(concat!($f, "=")),
tooltip: Some(Text::translate(
Cow::Borrowed(concat!("argument.entity.options.", $f, ".description")),
vec![],
)),
},)*]
}
}

els!(
"x",
"y",
"z",
"distance",
"dx",
"dy",
"dz",
"x_rotation",
"y_rotation",
"scores",
"tag",
"team",
"name",
"type",
"predicate",
"nbt",
"level",
"gamemode",
"advancements",
"limit",
"sort",
)
};

impl<'a> ParsingBuild<ParsingSuggestions<'a>> for EntitySelectorSuggestions {
fn build(self) -> ParsingSuggestions<'a> {
const NEXT: &[Suggestion<'static>] = &[Suggestion::new_str(","), Suggestion::new_str("]")];
const PROP_NAMES: &[Suggestion<'static>] = &[];
const SELECTORS: &[Suggestion<'static>] = &[
Suggestion::new_str("p"),
Suggestion::new_str("r"),
Expand All @@ -290,7 +329,7 @@ impl<'a> ParsingBuild<ParsingSuggestions<'a>> for EntitySelectorSuggestions {
const OPTION_END: &[Suggestion<'static>] = &[Suggestion::new_str("}")];
match self {
Self::Next => ParsingSuggestions::Borrowed(NEXT),
Self::Name => ParsingSuggestions::Borrowed(PROP_NAMES),
Self::Name => ParsingSuggestions::Borrowed(PROP_NAMES.as_slice()),
Self::Selector => ParsingSuggestions::Borrowed(SELECTORS),
Self::EqSign => ParsingSuggestions::Borrowed(EQ_SIGN),
Self::OptionEnter => ParsingSuggestions::Borrowed(OPTION_ENTER),
Expand Down
2 changes: 2 additions & 0 deletions crates/valence_command/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

pub mod block;
pub mod cenum;
pub mod entity;
Expand Down
2 changes: 1 addition & 1 deletion crates/valence_command/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::reader::{StrCursor, StrReader};
#[derive(Clone, Debug)]
pub struct Suggestion<'a> {
pub message: Cow<'a, str>,
pub tooltip: Option<Cow<'a, Text>>,
pub tooltip: Option<Text>,
}

impl<'a> Suggestion<'a> {
Expand Down
20 changes: 20 additions & 0 deletions crates/valence_command/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,26 @@ where
}
}

// a.k.a Literal
impl<'a> Parse<'a> for &'a str {
type Data = ();

type Error = NoParsingBuild;

type Suggestions = NoParsingBuild;

fn parse(
_data: Option<&Self::Data>,
reader: &mut StrReader<'a>,
_purpose: ParsingPurpose,
) -> ParsingResult<Self, Self::Suggestions, Self::Error> {
ParsingResult {
suggestions: None,
result: Ok(Some(reader.read_unquoted_str())),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions crates/valence_command/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ impl<'a> BrigadierArgument<'a> for ResourceLocation<'a> {
Parser::ResourceLocation
}
}

// resource, resource_key, resource_tag, resource_tag_or_key should be
// implemented when registry system will be fully implemented.