Skip to content

Commit 388f67e

Browse files
committed
docs: add more docs\!
1 parent 194fe93 commit 388f67e

20 files changed

+342
-22
lines changed

docs/annotated/annotated.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Annotated Commands
2+
3+
`command-flow` allows developers to create full command trees in a declarative way
4+
using annotated classes and methods.
5+
6+
The annotated command API is just an alternative way to create `Command` instances,
7+
we use this instead of the classic `Command.builder(String)` method.
8+
9+
### Elements of the Annotated Command API
10+
11+
The annotated command API is composed of 3 elements:
12+
- The `@Command` annotation and others.
13+
- The `AnnotatedCommandTreeBuilder` interface

docs/annotated/command-class.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
## Command Class
2+
3+
The annotated command API has a `CommandClass` interface used to mark a class as a container
4+
of commands or a command itself.
5+
6+
To declare the commands, we use annotations:
7+
8+
<!--@formatter:off-->
9+
```java
10+
@Command(names = "teleport")
11+
public class TeleportCommand implements CommandClass {
12+
13+
@Command(names = "") // empty name indicates the root command
14+
public void run(
15+
@Sender Player sender, // the sender player
16+
Player target // the target player (argument)
17+
) {
18+
sender.teleport(target);
19+
}
20+
21+
}
22+
```
23+
<!--@formatter:on-->
24+
25+
In this example we have created a `/teleport` command that takes a player name
26+
as an argument *(command-flow will parse it and find the right Player)* and
27+
teleports the sender to the target player.
28+
29+
The `@Command` annotation is used to mark a method as a command, it can be used
30+
on methods inside a `CommandClass` or on the class itself.
31+
32+
See this other example:
33+
34+
<!--@formatter:off-->
35+
```java
36+
@Command(names = { "friends", "friend", "f", "fr" }) // name, ...aliases
37+
public class FriendsCommand implements CommandClass {
38+
39+
@Command(names = "add")
40+
public void add(@Sender Player sender, Player target) {
41+
// add 'target' to 'sender' friend list
42+
}
43+
44+
@Command(names = "remove")
45+
public void remove(@Sender Player sender, Player target) {
46+
// remove 'target' from 'sender' friend list
47+
}
48+
49+
@Command(names = "list")
50+
public void list(@Sender Player sender) {
51+
// list all 'sender' friends and show
52+
}
53+
54+
@Command(names = "broadcast")
55+
public void broadcast(@Sender Player sender, @Text String message) {
56+
// send 'message' to all the friends of 'sender'
57+
}
58+
59+
}
60+
```
61+
<!--@formatter:on-->
62+
63+
In this example we have created a `/friends` command with 4 subcommands:
64+
- `/friends add <player>`: add a player to the sender friend list.
65+
- `/friends remove <player>`: remove a player from the sender friend list.
66+
- `/friends list`: list all the sender friends.
67+
- `/friends broadcast <...message>`: send a message to all the sender friends.
68+
69+
In this new example we also see these new stuff:
70+
- `@Command(names = {...})`: multiple names! The first one is considered the actual
71+
command name and the rest are aliases.
72+
- `@Text`: used to mark a parameter as a text argument, this means that it will
73+
consume all the arguments left in the command and join them with spaces.
74+
75+
### Registering them
76+
77+
To register command classes we must convert them to `Command` first, we can do
78+
that using the `AnnotatedCommandTreeBuilder` interface, check the next page!
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Command Builder
2+
3+
The `AnnotatedCommandTreeBuilder` is the interface responsible for building `Command`
4+
instances from annotated commands and classes.
5+
6+
### Creating an AnnotatedCommandTreeBuilder
7+
8+
To create a `AnnotatedCommandTreeBuilder` we must create a `PartInjector` first and
9+
*(optionally)* a `SubCommandInstanceCreator`. More about this in the next pages.
10+
11+
```java
12+
PartInjector injector = ...;
13+
SubCommandInstanceCreator instanceCreator = ...;
14+
15+
AnnotatedCommandTreeBuilder builder = AnnotatedCommandTreeBuilder.create(injector, instanceCreator);
16+
```
17+
18+
### Building a Command
19+
20+
Now that we have a `AnnotatedCommandTreeBuilder` we can build a command (or multiple commands).
21+
To do this we must call the `fromClass` method with the class of the command we want to build.
22+
23+
Note that this method returns a list of commands, since a single class may contain multiple
24+
root commands on it.
25+
26+
```java
27+
// create the builder
28+
AnnotatedCommandTreeBuilder builder = ...;
29+
30+
// build the Command list from our class
31+
List<Command> commands = builder.fromClass(MyCommand.class);
32+
33+
// now we can register them using the CommandManager#registerCommands convenience method
34+
commandManager.registerCommands(commands);
35+
```

docs/annotated/index.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
annotated.md
2+
command-class.md
3+
command-tree-builder.md
4+
part-injector.md
5+
subcommand-instance-creator.md

docs/annotated/part-injector.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Part Injector
2+
3+
The `PartInjector` is a registry which holds the registered PartFactories
4+
and PartModifiers.
5+
6+
- A `PartFactory` is a class that provides a way to create an specific type of part.
7+
- A `PartModifier` is like a PartFactory, the difference is that it may wrap the original part
8+
or modify it instead of creating a new one.
9+
10+
### Creating a PartInjector
11+
12+
Creating a `PartInjector` is simple, just do the following:
13+
14+
```java
15+
PartInjector partInjector = PartInjector.create();
16+
17+
// install the default bindings!
18+
// parts for native and core types like String, Boolean, Double, Float, Integer,
19+
// Text(String), ArgumentStack, CommandContext, also modifiers like LimitModifier,
20+
// OptionalModifier, ValueFlagModifier
21+
partInjector.install(new DefaultsModule());
22+
```
23+
24+
### Platform Specific
25+
26+
Some of the platform-specific subprojects include a `Module` for the `PartInjector`
27+
that can be easily installed to obtain the default bindings for that platform.
28+
29+
For example, for Bukkit (`commandflow-bukkit` subproject):
30+
```java
31+
// install bindings for the default bindings for Bukkit, such as
32+
// CommandSender, OfflinePlayer, Player, World, GameMode and @Sender Player
33+
partInjector.install(new BukkitModule());
34+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## SubCommand Instantiation
2+
3+
Sometimes we have a command that has subcommands declared with the `@SubCommandClasses`
4+
annotation. In this case, the command framework needs to know how to instantiate the
5+
subcommands.
6+
7+
We can specify how to instantiate the subcommands by implementing the `SubCommandInstanceCreator`
8+
interface.
9+
10+
### The `SubCommandInstanceCreator` interface
11+
12+
Functional interface that has a single method `createInstance(Class<? extends CommandClass>, CommandClass)`,
13+
that receives the subcommand class and its parent instance, and returns the sub command instance.
14+
15+
If we do not specify one, the annotated command tree builder will instantiate the subcommand
16+
using Reflection, by looking for constructor that accepts the parent's class as the single
17+
parameter an empty constructor.
18+
19+
The `SubCommandInstanceCreator` is set to the `AnnotatedCommandTreeBuilder` when instantiating
20+
it, for example:
21+
22+
<!--@formatter:off-->
23+
```java
24+
// will use the default SubCommandInstanceCreator
25+
AnnotatedCommandTreeBuilder commandBuilder = AnnotatedCommandTreeBuilder.create(partInjector);
26+
27+
// will use a custom SubCommandInstanceCreator
28+
AnnotatedCommandTreeBuilder commandBuilder = AnnotatedCommandTreeBuilder.create(
29+
partInjector,
30+
new MyCustomSubCommandInstanceCreator()
31+
);
32+
```
33+
<!--@formatter:on-->
34+
35+
### Common Implementations
36+
37+
It is common for `command-flow` to be used along with dependency injection frameworks, so
38+
you can just create a `SubCommandInstanceCreator` that does the following:
39+
40+
**For [Google's Guice](https://github.com/google/guice) or [Unnamed Team's inject](https://github.com/unnamed/inject):**
41+
```java
42+
// obtain the Injector instance
43+
Injector injector = ...;
44+
45+
// create the SubCommandInstanceCreator
46+
SubCommandInstanceCreator subCommandCreator = (clazz, parent) -> injector.getInstance(clazz);
47+
48+
// now set it when creating an AnnotatedCommandTreeBuilder
49+
AnnotatedCommandTreeBuilder commandBuilder = AnnotatedCommandTreeBuilder.create(
50+
partInjector,
51+
subCommandCreator
52+
);
53+
```

docs/concepts/command-context.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Command Context
2+
3+
A mutable object which contains the context for a command execution, including but not
4+
limited to the values for every parsed part, the raw arguments list and the raw arguments
5+
for every part, the labels and the Command execution path (which path of subcommands was taken).

docs/concepts/command-manager.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Command Manager
2+
3+
The CommandManager manages the command registration, parsing and execution. Also provides
4+
a way to obtain command suggestions *(which can be used for tab-completion in programs like
5+
CLI applications or Minecraft plugins)* for a given input.
6+
7+
### Creating a Command Manager
8+
9+
Depending on your platform, you may want to use a different implementation of the
10+
`CommandManager`. Check your [platform](../platforms/platforms.md)'s documentation
11+
to get more information.
12+
13+
However, there is a default implementation called `SimpleCommandManager` which can be
14+
used in any platform, but it doesn't provide any implementation-specific features.
15+
16+
<!--@formatter:off-->
17+
```java
18+
CommandManager commandManager = new SimpleCommandManager();
19+
```
20+
<!--@formatter:on-->
21+
22+
### Registering commands
23+
24+
To register a command, you need to create a `Command` object and register it using
25+
the `CommandManager.registerCommand(Command)` method.
26+
27+
<!--@formatter:off-->
28+
```java
29+
// create CommandManager
30+
CommandManager manager = ...;
31+
32+
// create Command using builder
33+
Command command = Command.builder("test")
34+
.description("A test command")
35+
.action(context -> {
36+
System.out.println("Hello world!");
37+
})
38+
.build();
39+
40+
// register the command
41+
manager.registerCommand(command);
42+
```
43+
<!--@formatter:on-->

docs/concepts/command-part.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Command Part
2+
3+
This is the second most fundamental component, it can be understood as every argument
4+
of a [Command](./command.md), including things like subcommands, flags, non positional
5+
arguments, etc.
6+
7+
It can use arguments from the argument list, or provide them using any other means. They
8+
can also forward the parsing responsibility to another part and just act as a modifier.
9+
10+
Most of the default parts can be found at the `Parts` class.

docs/concepts/command.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Command
2+
3+
Commands are the most fundamental component of the framework. It contains all the
4+
information related to a command, included but not limited to name, aliases, permission,
5+
parts, etc.
6+
7+
Commands are created using the `Command.builder(String)` method, which returns an
8+
`Command.Builder` instance where you can set all the information of the command.
9+
10+
Example:
11+
<!--@formatter:off-->
12+
```java
13+
Command command = Command.builder("Test")
14+
.action(context -> {
15+
System.out.println("Hi!");
16+
})
17+
.build();
18+
```
19+
<!--@formatter:on-->

docs/concepts/index.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
command.md
2+
command-manager.md
3+
command-part.md
4+
command-context.md

docs/getting-started.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
Welcome to the `command-flow` documentation
44

5-
CommandFlow is a flexible command framework which removes lots of
6-
boilerplate code used in commands
7-
8-
The command framework is divided into two parts. One is the actual
9-
command framework and the other one is the API to allow creation of
10-
a complete command tree based on annotations
11-
12-
### Components of the Command Framework
13-
14-
There are some basic components that you should at least be aware of to use **CommandFlow**
15-
- **Command:** This is the most fundamental component, the **Command**. It contains all the information related to a command, included but not limited to name, aliases, permission, parts, etc.
16-
Those are created using the `Command.builder(String)` method, which returns an `Command.Builder` instance where you can set all the information of the command.
17-
18-
- **CommandPart:** This is the second most fundamental component, it can be understood as every argument of a **Command**, including things like subcommands, flags, non positional arguments, etc. It can use arguments from the argument list, or provide them using any other means also they can forward the parsing to another part and just act as a modifier. Most of the default parts can be found at the class `Parts`
19-
20-
- **CommandContext**: This is a mutable object which contains the context for the called command including but not limited to the values for every part parsed, the raw arguments list and the raw arguments for every part, the labels and the Command execution path(which path of subcommands was taken).
21-
22-
23-
### Known bugs or misbehaviours
24-
The next list are the known bugs that must be resolved at some point:
25-
- No known bugs at the moment.
5+
`command-flow` is a flexible and platform-agnostic command framework
6+
for Java. With this framework you can *imperatively* create command trees
7+
using builders or *declaratively* using annotated classes and methods.
8+
9+
See the following example:
10+
```java
11+
@Command("test")
12+
public class TestCommand implements CommandClass {
13+
14+
@Command("hello")
15+
public void hello(CommandSender sender) {
16+
sender.sendMessage("Hello World!");
17+
}
18+
19+
}
20+
```
21+
22+
### Features
23+
- Easily create commands using builders or annotations

docs/index.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
getting-started.md
2-
installation.md
2+
installation.md
3+
concepts
4+
annotated
5+
platforms

docs/platforms/brigadier.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Bukkit with Brigadier

docs/platforms/bukkit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Bukkit

docs/platforms/bungeecord.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## BungeeCord

docs/platforms/discord-jda.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Discord (JDA)

docs/platforms/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
platforms.md

0 commit comments

Comments
 (0)