Skip to content

Commit 82a3fe3

Browse files
authoredMay 21, 2024··
Update commands.md
1 parent 11b3122 commit 82a3fe3

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed
 

‎docs/commands.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Commands
2-
## First steps
2+
### First steps
33

44
Do these steps the **first time** you add Caramel to your plugin.
55
- Add listeners pointing to Caramel.
@@ -31,7 +31,7 @@ public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotN
3131
</details>
3232

3333

34-
## A new command
34+
### A new command
3535
First, lets make a brand new class and have it implement a Caramel Command. For this tutorial, I'll be using the class name "MyCommand" and main file "MyPlugin".
3636
```java
3737
public class MyCommand implements CaramelCommand {
@@ -63,7 +63,7 @@ public CaramelCommandDetail getDetails() {
6363
}
6464
```
6565

66-
# Registering
66+
### Registering
6767
1. Make sure your command is added to your `plugin.yml` file under `commands`. It should have the basic layout of `commands > my-command-name > description+usage`.
6868
2. Add a new variable in your main class to store your command:
6969
```java
@@ -84,3 +84,51 @@ public class MyPlugin extends JavaPlugin {
8484
}
8585
```
8686
4. All set! Your command is now registered.
87+
88+
<br/><br/><br/>
89+
90+
## The garnish on top
91+
### (OPTIONAL) Tab Completion
92+
Step 1 - Dead simple - Add this code to your command file:
93+
```java
94+
@Override
95+
public List<String> complete(String[] args) {
96+
...
97+
}
98+
```
99+
100+
#### Tab completion (the boring way)
101+
Simply `return` a List<> of your choices.
102+
```java
103+
return Arrays.asList("my", "choices");
104+
```
105+
106+
#### With a little spice
107+
Caramel comes bundled with a tab completion handler for some no-hassle satisfying tab completion.
108+
```java
109+
// Both of these example only work for one argument, as we only use "args[0]". See below for more.
110+
111+
return CaramelUtility.tabComplete(args[0], Arrays.asList("example", "choices"));
112+
113+
// OR, use Caramel's tabCompletePlayers option.
114+
115+
return CaramelUtility.tabCompletePlayers(args[0], Bukkit.getOnlinePlayers());
116+
```
117+
This will shorten the tab completion options based on how much you have already typed (like autocomplete). To further the complexity (aka if you have multiple args) you can handle it based on what argument you are on. Example:
118+
119+
Lets say you have a command with the usage /command <pet> <name> <player>.
120+
Your args would be ["pet", "name", "player"] (0,1,2). Lets try this:
121+
122+
```java
123+
if(args.length == 1) { // first argument - pet
124+
String arg = args[0]; // [0] is the first argument in an array.
125+
return CaramelUtility.tabComplete(arg, Arrays.asList("cat", "dog"));
126+
} else if(args.length == 2) { // next argument - name, no tab completion
127+
return Collections.emptyList();
128+
} else if(args.length == 3) { // last argument - player, player completion
129+
String arg = args[2];
130+
return CaramelUtility.tabCompletePlayers(arg, Bukkit.getOnlinePlayers());
131+
}
132+
```
133+
134+
Nice!

0 commit comments

Comments
 (0)
Please sign in to comment.