Skip to content

Commit

Permalink
Update the Brigadier guide (#60)
Browse files Browse the repository at this point in the history
* chore: Update the brigadier command docs and example

* fix: Rename brigardier to brigadier

* fix: Inline danger block
  • Loading branch information
DarkVanityOfLight committed Dec 21, 2023
1 parent 7ceee30 commit 5e2b8ca
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
@@ -1,8 +1,8 @@
# Brigardier support
# Brigadier support

???+ warning "Brigardier dependency for spigot-api users"
(You do only have to do the following if you are using the `spigot-api` instead of the `spigot` dependency!) <br>
Whilst Spigot itself depends on [Brigardier](https://github.com/Mojang/brigadier#gradle) the Spigot API doesn't so in order for this feature to work you need to add Brigardier as a `compileOnly` dependency. More information on that can be found here: [https://github.com/Mojang/brigadier#gradle](https://github.com/Mojang/brigadier#gradle)
???+ warning "Brigadier dependency for spigot-api users"
(You only have to do the following if you are using the `spigot-api` instead of the `spigot` dependency!) <br>
Whilst Spigot itself depends on [Brigadier](https://github.com/Mojang/brigadier#gradle) the Spigot API doesn't so in order for this feature to work you need to add Brigadier as a `compileOnly` dependency. More information on that can be found here: [https://github.com/Mojang/brigadier#gradle](https://github.com/Mojang/brigadier#gradle)

## Create a command

Expand All @@ -12,6 +12,9 @@ command("mycommand") {
}
```

???+ danger
Do not add the command in your plugin.yml, this will interfere with Brigadier

## Register the command

The command will be automatically registered by default, **if you are initializing it before the end of the `startup` call
Expand All @@ -27,13 +30,32 @@ which the `command(name)` function returns).
You can infinitely nest all these functions, resulting in complex command structures. <br>

### Execution handler
You can define your execution logic using the `executes` (with a status code) or `runs` function.

#### Runs

You provide your execution logic using the `executes` (with status code) or `simpleExecutes` (status code is always 1)
function.
Setting the status code explicitly:
```kotlin
runs { context ->
context.bukkitSender.sendMessage("hey gamer ;)")
return@runs 1
}
```

Alternatively you could use it as following:
```kotlin
simpleExecutes { context ->
context.source.bukkitSender.sendMessage("hey gamer ;)")
runs {
this.sender.bukkitSender.sendMessage("hey gamer ;)")
}
```
Automatically returning the status code 1.

#### Executes

```kotlin
executes { context ->
context.bukkitSender.sendMessage("hey gamer ;)")
return@executes 1
}
```

Expand All @@ -43,12 +65,14 @@ simpleExecutes { context ->

#### The command context

You can use the command context to get the command source. You can use the source for:
You can use the command context in `executes` to get the command source. You can use the source for:

- `source.bukkitSender` to get the `CommandSender`
- `source.player` ensure that a player executed the command and get that `Player`
- `source.bukkitWorld` to get the world of the executor

If you are using `runs`, you can access these by using `sender` instead of `source`.

### Literals (subcommands)

```kotlin
Expand All @@ -70,8 +94,8 @@ execution handler.

#### Argument type

The second paramter of the argument function is the argument type. There are a lot of pre defined argument types by
brigardier.
The second parameter of the argument function is the argument type. There are a lot of pre-defined argument types by
brigadier.

The argument types for all primitives can be accessed in the following pattern: `NameArgumentType.name()` (where name is the name of the primitive)

Expand All @@ -88,22 +112,22 @@ The value of the argument can be retrieved from the command context.

```kotlin
argument("argumentname", StringArgumentType.string()) {
simpleExecutes {
val argValue = it.getArgument<String>("argumentname")
runs {
val argValue = this.getArgument<String>("argumentname")
// or using reified you can omit the type sometimes
mapWhereTheKeysAreStrings[it.getArgument("argumentname")]
mapWhereTheKeysAreStrings[this.getArgument("argumentname")]
}
}
```

### Suggestions

You can provide argument suggestions using the `simpleSuggests` function. It is **not** recommended using the default
You can provide argument suggestions using the `suggestList` function. It is **not** recommended using the default
`suggests` function.

```kotlin
simpleSuggests { Material.values().toList() }
suggestList { Material.values().toList() }
```

It is okay to do heavy operations inside this function. Suggestions are asynchronous. The body of the `simpleSuggests`
function is suspending, meaning you can use kotlinx.coroutines in it.
If you want to do heavy operations inside the suggest functions, you should use `suggestListSuspending`
The body of the `suggestListSuspending` function is suspending, meaning you can use kotlinx.coroutines in it.
19 changes: 19 additions & 0 deletions guide/docs/commands/brigadier_example.md
@@ -0,0 +1,19 @@
The following command illustrates how to use commands, subcommands, arguments and the command context.

```kotlin
command("gaming") {
literal("set") {
argument("state", BoolArgumentType.bool()) {
suggestList { listOf(true, false) }
runs {
if (this.getArgument("state"))
this.player.sendMessage("yoo gaming has been activated")
else {
this.player.kill()
this.player.sendText("gaming disabled"){ color = KColors.INDIANRED }
}
}
}
}
}
```
19 changes: 0 additions & 19 deletions guide/docs/commands/brigardier_example.md

This file was deleted.

4 changes: 2 additions & 2 deletions guide/mkdocs.yml
Expand Up @@ -44,8 +44,8 @@ nav:
- Gui:
- Compounds: gui/compounds.md
- Commands:
- Brigardier: commands/brigardier.md
- Brigardier example: commands/brigardier_example.md
- Brigadier: commands/brigadier.md
- Brigadier example: commands/brigadier_example.md
- Migration:
- "1.17 to 1.18": migration/1_17_to_1_18.md
- "1.16 to 1.17": migration/1_16_to_1_17.md

0 comments on commit 5e2b8ca

Please sign in to comment.