-
-
Notifications
You must be signed in to change notification settings - Fork 72
Implementing optional arguments #393
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
cdb4c08
implementing optional arguments
DerEchtePilz 96a03ec
Add optional arguments to the Kotlin DSL. Rename commandapi-kotlin-bu…
DerEchtePilz 5b46749
add missing JavaDocs on AbstractArgument#setOptional(boolean)
DerEchtePilz 2ad5828
fixing build error
DerEchtePilz 3cf8297
adding documentation for optional arguments
DerEchtePilz 6eb09e5
fixing dependency in the Bukkit Kotlin DSL example
DerEchtePilz 7c4f635
address code review
DerEchtePilz a25547d
create optional_arguments.md
DerEchtePilz d1c0088
change /kill command to /sayhi
DerEchtePilz 5ad1a21
make Mardown Linter happy (hopefully)
DerEchtePilz c82feff
fix wrong enchor end
DerEchtePilz f777925
address code review
DerEchtePilz 33228d4
convert tabs to spaces in Examples.java
DerEchtePilz c226296
Merge branch 'dev/dev' into dev/dev
JorelAli 210bed6
refactor AbstractCommandAPICommand#register()
DerEchtePilz afbd40a
Merge branch 'DerEchtePilz:dev/dev' into 'dev/dev'
DerEchtePilz 7e36de1
simplifying AbstractCommandAPICommand#getArgumentsToRegister()
DerEchtePilz 458b8d8
refactor AbstractCommandAPICommand#register() again
DerEchtePilz 3e7c6ca
Add a section 'Setting existing arguments as optional arguments' to o…
DerEchtePilz d913acc
remove the List parameter from AbstractCommandAPICommand#getCommandsT…
DerEchtePilz 6266990
Merge branch 'dev/dev' into dev/dev
DerEchtePilz ce835c6
fix a bug where a required argument would be registered as an optiona…
DerEchtePilz 473bc75
implement multiple getOrDefault methods for the CommandArguments class
DerEchtePilz a31b755
adding documentation for the getOrDefault method
DerEchtePilz 572beb9
address code review
DerEchtePilz c8c4960
make CommandArguments#get(int) not throw an ArrayIndexOutOfBoundsExce…
DerEchtePilz 3008d60
Merge branch 'dev/dev' into dev/dev
DerEchtePilz a76530d
fix variable names after merge
DerEchtePilz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
create optional_arguments.md
- Loading branch information
commit a25547d07541f36c544c5c9826ba91302c93f09a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
## Optional Arguments | ||
|
||
Sometimes, you want to implement a command that has arguments that do not need to be entered. Take a `/kill` command for example. You may want to kill yourself, or another player. For that, we want this command syntax: | ||
|
||
```mccmd | ||
/kill - Kills yourself | ||
/kill <target> - Kills a target player | ||
``` | ||
|
||
To implement these commands, the CommandAPI provides two methods to help you with that: | ||
|
||
```java | ||
Argument withOptionalArguments(List<Argument<?>> args) | ||
Argument withOptionalArguments(Argument<?>... args) | ||
``` | ||
|
||
<div class="example"> | ||
|
||
### Example - /kill command with two separate arguments | ||
|
||
For example, say we're registering a command `/kill`: | ||
|
||
```mccmd | ||
/kill - Kills yourself | ||
/kill <target> - Kills a target player | ||
``` | ||
|
||
For that, we are going to register a command `/kill`. To add optional arguments, we are going to use the `withOptionalArguments(Argument... args)` method: | ||
|
||
<div class="multi-pre"> | ||
|
||
```java,Java | ||
{{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:argumentkillcmd}} | ||
``` | ||
|
||
```kotlin,Kotlin | ||
{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:argumentkillcmd}} | ||
``` | ||
|
||
```kotlin,Kotlin_DSL | ||
{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:argumentkillcmd}} | ||
``` | ||
|
||
</div> | ||
|
||
This gives us the ability to run both `/kill` and `/kill <target>` with the same command name "kill", but have different results based on the arguments used. | ||
|
||
You can notice two things: | ||
- We use the `withOptionalArguments` method to add an optional argument to a command | ||
- We use `args.get("target")` to get our player out of the arguments | ||
|
||
With optional arguments, there is a possibility of them being not present in the arguments of a command. The reason we use `args.get("target")` is that this will just return `null` and you can handle what should happen. | ||
|
||
If you use `args.get(0)` this will just throw an `ArrayIndexOutOfBoundsException` every time the command is executed without providing the optional argument. | ||
|
||
</div> | ||
|
||
One thing to note when using the `withOptionalArguments` method is that this calls the `setOptional()` method internally. This means that the following two examples are identical: | ||
|
||
```java | ||
new CommandAPICommand("optional") | ||
.withOptionalArguments(new PlayerArgument("target")) | ||
``` | ||
|
||
```java | ||
new CommandAPICommand("optional") | ||
.withArguments(new PlayerArgument("target").setOptional(true)) | ||
``` | ||
|
||
However, calling `withOptionalArguments` is safer because it makes sure that the argument is optional because of that internal call. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.