Create a simple command - HevelMc/CraftWithPy GitHub Wiki

Hello User !

Introduction

Hello world is nice, but how about greeting your users when they execute a command ? In this short tutorial, you will learn how to create a command with CraftWithPy. There are two steps for a command to work: defining it at the start of the script (usually) and defining its purpose. Let's assume that your new script is called "greet_me".

The code

# -*- coding: iso-8859-1 -*-
from core import register_command

class greet_me:
    def on_enable(self):
        register_command("hello", self.hello_cmd, usage="/hello", description="Greet the user!", permission="greet.me", aliases=["greet", "hi"])
    
    def hello_cmd(self, sender, cmd, label, args):
        sender.sendMessage("Hello " + sender.getName() + "!")

Explanations

Let's explain this code now.

  • Line 1, always the same encoding line that you must have at the top of each of your scripts.
  • Line 2, the import of the register_command function, which allows us to create a command and add it to the server.

  • Line 4, the class that contains all your code.

  • Line 5, the function called when the script is started. This is where you will most of the time call the declaration of your commands.

  • Line 6, you define your command. Here are the parameters:

    • The first one is required, it is the name of your function (without the / at the beginning)
    • The second one is the function to call when the user executes the command, also required. Here we call the hello_cmd function, belonging to the same class so to self. DO NOT put parentheses at the end. We refer to the function, don't call it!

    All other arguments are optional:

    • usage: the text that will be displayed when the person makes a mistake with the syntax.
    • description: the text that will be displayed in the /help or the /?
    • permission: the permission needed to execute the command
    • aliases: a list of different aliases of the function, which will work exactly the same way as the main name.

  • Line 8 is the function called when you execute the command. You must always specify the following arguments (you can change the names of the variables but not their order):
    • self: to have a reference to the class it belongs to
    • sender: the user who executed the command (see CommandSender on Spigot for the list of functions)
    • cmd: the command used (see PluginCommand on Spigot for the list of functions)
    • label: the name or alias used by the user (the word just after the "/"). 
    • args: The list of arguments (words) specified after the label.
  • Line 9, the code of your command. Here we use sender.sendMessage() to send a message to the player (or console) who typed the command.

Other examples

Basic gamemode switcher

# -*- coding: iso-8859-1 -*-
from core import register_command

class greet_me:
    def on_enable(self):
        register_command("gamemode", self.gamemode_cmd, usage="/gm <gamemode id>", description="Put the player in the specified gamemode.", permission="gamemode", aliases=["gm"])
    
    def gamemode_cmd(self, sender, cmd, label, args):
        # Test if the player has specified correct arguments
        if len(args) < 1: return False
        # Test if the sender is a player (not console)
        if isinstance(sender, Player):
            if args[0] == "0": sender.setGamemode(Gamemode.SURVIVAL)
            elif args[0] == "1": sender.setGamemode(Gamemode.CREATIVE)
            elif args[0] == "2": sender.setGamemode(Gamemode.ADVENTURE)
            elif args[0] == "3": sender.setGamemode(Gamemode.SPECTATOR)
            # Cancel if the player didn't specified a correct gamemode id.
            else return False
        

Continue reading

In the next page we will see how to perform an action when a certain event takes place.