-
Notifications
You must be signed in to change notification settings - Fork 0
Commands
We have completely redone the command system from Bukkit. Our main goal with this change was to provide an easy and fast way to add commands without having to deal with all the small checks.
Creating a command is very easy to do. We will begin by creating a HelloCommand class. We will also add a HelloCommand constructor with CoPlugin as its parameter.
package com.coalesce.example.HelloCommand;
import com.coalesce.plugin.CoPlugin;
public final class HelloCommand {
public HelloCommand(CoPlugin plugin) {
}
}Now that we have the basic class setup, let's start by creating the command method.
package com.coalesce.example.HelloCommand;
import com.coalesce.plugin.CoPlugin;
import com.coalesce.command.CommandContext;
public final class HelloCommand {
public HelloCommand(CoPlugin plugin) {
}
private void hello(CommandContext context) {
}
}You probably notice right off the bat that this command method is very different from the Bukkit command method. The way we have made this command system work is we don't need to return a boolean saying whether this command has failed or not. All we need to do is tell it when to stop by doing return;
You also must notice the parameter of the command method. The CommandContext is our quick replacement for Bukkit's command parameters. We provide almost everything you need for a command to work properly. If you want to read up on the CommandContext documentation, look here.
Next, we will send a basic message to the sender that is sending this command. We will also register this command.
package com.coalesce.example.HelloCommand;
import com.coalesce.plugin.CoPlugin;
import com.coalesce.command.CommandContext;
import com.coalesce.command.CoCommand;
import com.coalesce.command.CommandBuilder;
import org.bukkit.ChatColor;
public final class HelloCommand {
public HelloCommand(CoPlugin plugin) {
CoCommand command = new CommandBuilder(plugin, "hello")
.executor(this::hello)
.usage("/hello")
.description("Greets you!")
.maxArgs(0)
.playerOnly()
.build();
plugin.addCommand(command);
}
private void hello(CommandContext context) {
context.send(ChatColor.ITALICS + "Hello!");
}
}As seen from the snippet of code above, we also register our commands differently. We use a builder style to generate our commands and register them into the command map. We have chosen a builder style over annotations due to the wider range of possibilities we have at our feet. For more documentation on the command builder, please look here.
In addition to the new command system, we have also redone the TabComplete system. We designed the tab complete system to be quick and easy to add new completions.
package com.coalesce.example.HelloCommand;
import com.coalesce.plugin.CoPlugin;
import com.coalesce.command.CommandContext;
import com.coalesce.command.CoCommand;
import com.coalesce.command.CommandBuilder;
import org.bukkit.ChatColor;
public final class HelloCommand {
public HelloCommand(CoPlugin plugin) {
CoCommand command = new CommandBuilder(plugin, "hello")
.executor(this::hello)
.completion(this::helloTab)
.usage("/hello")
.description("Greets you!")
.maxArgs(0)
.playerOnly()
.build();
plugin.addCommand(command);
}
private void hello(CommandContext context) {
context.send(ChatColor.ITALICS + "Hello!");
}
private void helloTab(TabContext context) {
context.completionAt(1, context.getSender().getName())
}
}Tab completions are probably the largest change with the command system. As seen from the example above, we have completely overhauled it. A normal tab completion as simple as that could be 3-4 lines long (depending on how it was made). Our new tab completion context has methods like completionAt, completionAfter, or completion. These methods make this system quick and easy to use.