Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Creating your first command

AlignedCookie88 edited this page May 8, 2024 · 1 revision

This page will teach you how to create your first command with VCommandParser. Using an IDE that automatically imports classes will be helpful, as the article omits the imports.

You should already have added this plugin as a maven dependency, you can see how to do this on the wiki homepage or in the README.

Adding your command to plugin.yml

In plugin.yml add the follow section:

commands:
  mycommand:
    description: 'My VCommandParser Command'

Defining your command and registering it.

In your plugin's onEnable function:

// Create the command 'mycommand', allowing both the player & console to run it.
VCommand myCommand = new VCommand("mycommand", VCommandSender.Both, new MyCommandExecutor() /* We will define MyCommandExecutor in the next step! */);

// Add a greedy string argument to the command (Greedy strings will use the rest of the command as their input, like the /say command does)
myCommand.addArgument(new GreedyStringArgument());

// Register the command to the plugin.
myCommand.register(this);

Defining your command executor

The command executor is the code that runs when a player (or the console) runs your command.

Create a new class called MyCommandExecutor and write the following code:

public class MyCommandExecutor implements VCommandExecutor {
    
    public void onExecute(VCommandContext commandContext) {
        String myArgument = commandContext.getArguments().getStringArg(0); // Get the content from the greedy string argument we added earlier
        // The number represents which argument we want to get. It starts at 0 for the first argument, and increases by 1 each following argument.
        commandContext.sendMessage(String.format("You entered: %s", myArgument)); // TIP: You can also send text components from the Adventure Text API
    }
    
}

Done!

You have now created your first command. Try it out with /mycommand. VCommandParser will automatically send error messages for invalid arguments.