Register Commands¶
In this section, we'll walk you through creating a simple command. By the end of this section, you'll have a basic understanding of how to define and register commands with allay.
Create a Command¶
Let's start with a simple command /hello
that greets the command sender.
And it's just as simple as that! You've just created a new command with description. Then you need to register the command into the server:
Add permissions¶
By the default, only the one who has operator
permission is able to use the newly created commands.
Since we are creating a simple command here, we might want to change its permission for everyone to use.
Let's make the following changes to our code:
- See tips
Tip
The getPermissions()
method returns a list of permissions that the command requires,
and DefaultPermissions.MEMBER is the default permission tree for new players.
DefaultPermissions class contains the following values:
DefaultPermissions.OPERATOR
: default permission tree for operators.DefaultPermissions.MEMBER
: default permission tree for new players.DefaultPermissions.VISITOR
: default permission tree for visitors.
Handle the commands¶
Now, the next step is to handle the commands we just created. Let's say we want to send a "Hello, World!" greeting message to whoever execute this command. It's just a few more lines away from that.
Now, install your plugin and restart your server. Join the game and type /help hello
. You should see the usage
information, which indicates that our command has been successfully added to the game!
Now, type /hello
, and you should receive the message "Hello World!".
Add parameters to commands¶
Now, let's say we want to send a custom message to greet the sender instead of "Hello World!". We can add a parameter to the command that accepts a message.
Let's change our code to add an optional parameter message
with msg
type:
- Here, we checked if the optional parameter is provided.
And it's done! Now when the users use /hello This is my message!
, "This is my message!" will
be shown to them instead of "Hello World!".
Tip
To make the parameter mandatory, remove the .optional()
method call.
Built-in parameter types¶
In the section above, we added a parameter with type msg
which is a built-in type supported by Allay.
You can check the full list of all built-in types currently supported here.