Create a script - HevelMc/CraftWithPy GitHub Wiki

Hello World!

Introduction

Once you have created your file (let's say you called it "example"), you need to initialize it as follows:

The script below allows you to broadcast "Hello World" to everyone on the server each time the script is reloaded (and "Bye!" when it is disabled)

The code

# -*- coding: iso-8859-1 -*-
from org.bukkit import Bukkit

class example:
    def on_enable(self):
        Bukkit.broadcastMessage("Hello World")

    def on_disable(self):
        Bukkit.broadcastMessage("Bye!")

Explanations

Let's explain line by line this code to understand it better:

  • Line 1 is not always necessary, but it is required for now for the proper functioning of color messages. (Please note that, as soon as we can change the encoding of the file to UTF-8, this line will have to be removed from your scripts when updating).
  • Line 2 is used to import the function that allows us to broadcast global messages.

  • Line 4 must contain the name of your script, the same name as your file (case-sensitive).
  • Line 5 defines the "on_enable" function. It is called when the script starts. (this can be executed when /py reload example or a /py enable example is typed, or when the server is started).
  • Line 6 is actually the line that sends the message. Here we call the spigot API just like we would on a real java project. For the list of functions, you can visit their documentation

  • Line 8 works like line 5 but runs when the script is disabled (this can be executed when /py reload example or a /py disable example is typed, or when the server is stopped).
  • Line 9 is exactly the same as line 6, with a different message

Next chapters

In the next pages you will see how to create a command and how to register to an event in the plugin.