Chapter 5: Commands

You've learnt how to create a simple commands that send text to a player, or broadcast something to a server. But what about arguments, permissions, and aliases? Let's find out how to do them!

In This Chapter

We're going to be learning how to adding more functionality to our commands. We're going to be adding aliases, permissions, and most importantly, arguments.

Part 1: Command Entries

Before we get into command arguments, let's talk about command entries, since these are fairly easy to understand. A command entry is basically a piece of code that modifies the way your command works. It can either be adding a permission to your command, or just describing what it does. to use command entries, you will put it right after creating your command, for example, if I wanted a command to only be executed if the player has permission tenfont.use:

command /tenfont:
    permission: tenfont.use

There are a bunch of command entries you can use to modify your command. To add a permission, use the permission: tenfont.use entry, and to send a message if the player doesn't have permission, use the permission message: You don't have permission! entry. To add an alias to your command, use the aliases: /tf entry, you can also insert multiple values in the alias like this: aliases: /tf, /10font, /ten. To add a description your command, use the description: This is a command entry. To add a usage to your command, use the usage: /tenfont entry. To make your command only executable by a certain command sender, use the executable by: player entry, you can also insert multiple by using executable by: player and console. Example code can be found below.

command /tenfont:
    usage: /tenfont
    description: Just a useless command.
    permission: tenfont.use
    permission message: &cYou don't have permission!
    executable by: player and console
    aliases: /tf
    trigger:
        send "&7Quack"

Part 2: Command Arguments

Alright! So now you know how to add a permission, description, usage, alias, executable types, and a no-permission message to your command! Now let's add some arguments to give the user more control over the command.

What's an argument?

An argument is what comes after your command name. For example, a text argument in a broadcast command, or a player argument in a kill event. Arguments are extremely useful when you want the command sender to have control over a specific part of your command. Before adding arguments however, we must learn about Skript types.

What's a type?

A type in Skript is the type of an expression. Each expression is of some type. For example, the world "world_name" expression is of type world. The event-player expression in the on teleport event, and the victim in the on death event are both of type player. If you didn't get it already, types are basically what object the expression returns. The victim expression returns the player that died, so we say that it is returning a player type. Arguments in Skript work the same way. Each argument has it's own type, and the command sender may enter anything that matches that type. If the argument is of type text, then the user must enter a text. So now, let's go back to Skript Arguments.

Creating Arguments

If you've read the Chapter 1 of this tutorial, you're in luck! Command arguments are somewhat similar to Skript syntaxes. The main difference is that instead of % percent signs for placeholders, we use the < less than sign, and the > greater than sign. So a text argument would look something like <text>. Just like Skript syntaxes, to create an optional argument, we would enclose it in square brackets. So an optional player argument would look like [<player>]. Now that we know how arguments work, let's create some!

In this tutorial, I'll be creating a give command, which gives an item to a specific player, or the entire server. Get ready, because this might get a little long.

First of all, to add arguments to our command, we'll just insert the arguments after the command name. So, if we were to create a broadcast command, our first line would look something like command /broadcast <text>:. Since we're creating a give command, we'll need 2 arguments. The first will be the item we want to give, and the second will be a player. For this tutorial, I'll make the player argument optional, and if it's not provided, we'll give the item to only the command sender. Pro Tip! Similar to Skript syntax, you can also use text in command arguments! For example, in the broadcast command, you could do something like command /broadcast <text> [to the server]:, which makes it so the player can also type to the server at the end of the command if they would like to. -- Back to our command now, for this tutorial, I'll name the command give. And the arguments will be <item> [to <player>]. Our first line will look like this.

command /give <item> [to <player>]:

Next, let's add a permission to this command, since we don't want players giving themselves stacks and stacks of diamond blocks, would we? We're also going to add a permission message which tells the player they don't have permission. We would also want to make the command not executable by the console, since we can't give items to the console. Our code will now look like this.

command /give <item> [to <player>]:
    permission: tenfont.give
    permission message: You don't have permission!
    executable by: player
    trigger:

Now let's check if the argument is set or not. For this we'll use a condition. You can read about conditions in the previous chapter of this tutorial. Our condition will check if the player argument is set. If it is, then we're going to get the player argument and give the item to them. Else, we're going to give the item to the sender of the command. The condition we'll use is %object% is set, and in our case, object will be our player argument. There are multiple ways of calling arguments, but my favorite is to call them by their order. In our command, <item> is the first argument, and <player> is the second argument, so we'll use arg-2 to call our argument. Now, if the second argument is set, we'll give the item argument to the player. The item argument is the first argument in our command, so we'll call it by using arg-1. Then, we use the give effect to give the item to the second argument, which is a player (which is the perfect type for the effect!) Our code will look something like this:

command /give <item> [to <player>]:
    permission: tenfont.give
    permission message: You don't have permission!
    executable by: player
    trigger:
        if arg-2 is set:
            give arg-1 to arg-2

Finally, let's create an else statement and give the item argument to the command sender.

command /give <item> [to <player>]:
    permission: tenfont.give
    permission message: You don't have permission!
    executable by: player
    trigger:
        if arg-2 is set:
            give arg-1 to arg-2
        else:
            give arg-1 to sender

And that's it. You've just created a command with arguments!

Conclusion

There you go! You now know how to create an advanced command, including a permission, permission message, executable type, alias, description, and usage.

Also, did you realize this is the only chapter with only 2 parts?

Last updated