Update the Brigadier guide (#60)

* chore: Update the brigadier command docs and example

* fix: Rename brigardier to brigadier

* fix: Inline danger block
This commit is contained in:
kilian
2023-12-21 17:55:06 +01:00
committed by GitHub
parent 7ceee306c8
commit 5e2b8ca5c6
4 changed files with 63 additions and 39 deletions

View File

@@ -1,8 +1,8 @@
# Brigardier support # Brigadier support
???+ warning "Brigardier dependency for spigot-api users" ???+ warning "Brigadier 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> (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 [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) 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 ## Create a command
@@ -12,6 +12,9 @@ command("mycommand") {
} }
``` ```
???+ danger
Do not add the command in your plugin.yml, this will interfere with Brigadier
## Register the command ## Register the command
The command will be automatically registered by default, **if you are initializing it before the end of the `startup` call The command will be automatically registered by default, **if you are initializing it before the end of the `startup` call
@@ -27,13 +30,32 @@ which the `command(name)` function returns).
You can infinitely nest all these functions, resulting in complex command structures. <br> You can infinitely nest all these functions, resulting in complex command structures. <br>
### Execution handler ### Execution handler
You can define your execution logic using the `executes` (with a status code) or `runs` function.
You provide your execution logic using the `executes` (with status code) or `simpleExecutes` (status code is always 1) #### Runs
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
runs {
this.sender.bukkitSender.sendMessage("hey gamer ;)")
}
```
Automatically returning the status code 1.
#### Executes
```kotlin ```kotlin
simpleExecutes { context -> executes { context ->
context.source.bukkitSender.sendMessage("hey gamer ;)") context.bukkitSender.sendMessage("hey gamer ;)")
return@executes 1
} }
``` ```
@@ -43,12 +65,14 @@ simpleExecutes { context ->
#### The command 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.bukkitSender` to get the `CommandSender`
- `source.player` ensure that a player executed the command and get that `Player` - `source.player` ensure that a player executed the command and get that `Player`
- `source.bukkitWorld` to get the world of the executor - `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) ### Literals (subcommands)
```kotlin ```kotlin
@@ -70,8 +94,8 @@ execution handler.
#### Argument type #### Argument type
The second paramter of the argument function is the argument type. There are a lot of pre defined argument types by The second parameter of the argument function is the argument type. There are a lot of pre-defined argument types by
brigardier. brigadier.
The argument types for all primitives can be accessed in the following pattern: `NameArgumentType.name()` (where name is the name of the primitive) The argument types for all primitives can be accessed in the following pattern: `NameArgumentType.name()` (where name is the name of the primitive)
@@ -88,22 +112,22 @@ The value of the argument can be retrieved from the command context.
```kotlin ```kotlin
argument("argumentname", StringArgumentType.string()) { argument("argumentname", StringArgumentType.string()) {
simpleExecutes { runs {
val argValue = it.getArgument<String>("argumentname") val argValue = this.getArgument<String>("argumentname")
// or using reified you can omit the type sometimes // or using reified you can omit the type sometimes
mapWhereTheKeysAreStrings[it.getArgument("argumentname")] mapWhereTheKeysAreStrings[this.getArgument("argumentname")]
} }
} }
``` ```
### Suggestions ### 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. `suggests` function.
```kotlin ```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` If you want to do heavy operations inside the suggest functions, you should use `suggestListSuspending`
function is suspending, meaning you can use kotlinx.coroutines in it. The body of the `suggestListSuspending` function is suspending, meaning you can use kotlinx.coroutines in it.

View File

@@ -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 }
}
}
}
}
}
```

View File

@@ -1,19 +0,0 @@
The following command illustrates how to use commands, subcommands, arguments and the command context.
```kotlin
command("gaming") {
literal("set") {
argument("state", BoolArgumentType.bool()) {
simpleSuggests { listOf(true, false) }
simpleExecutes {
if (it.getArgument("state"))
it.source.bukkitSender.sendMessage("yoo gaming has been activated")
else {
it.source.player.kill()
it.source.player.sendText("gaming disabled") { color = KColors.INDIANRED }
}
}
}
}
}
```

View File

@@ -44,8 +44,8 @@ nav:
- Gui: - Gui:
- Compounds: gui/compounds.md - Compounds: gui/compounds.md
- Commands: - Commands:
- Brigardier: commands/brigardier.md - Brigadier: commands/brigadier.md
- Brigardier example: commands/brigardier_example.md - Brigadier example: commands/brigadier_example.md
- Migration: - Migration:
- "1.17 to 1.18": migration/1_17_to_1_18.md - "1.17 to 1.18": migration/1_17_to_1_18.md
- "1.16 to 1.17": migration/1_16_to_1_17.md - "1.16 to 1.17": migration/1_16_to_1_17.md