Repository navigation
Plugin Basics
Creating Python plugins is a lot like creating Java plugins, extending a base class and writing a plugin.yml file. To get started, create a directory named ExamplePlugin.py in a test server's plugin directory. This is where your plugin code will go (for more information, see Packaging Plugins).
Your plugin can include as many Python modules as you want, but one of them must contain your main class. For this example, we will create a module example in example.py. Like Java plugins, a Python plugin's entry point is a class extending PythonPlugin:
# example.py
class ExamplePlugin(PythonPlugin):
def onEnable(self):
print "Hello, world!"onEnable is a method called when your plugin is enabled. The normal Python print statement has been made to print to your plugin's Bukkit logger. This is enough Python code to get started, but we still need a plugin.yml. This is just like Java plugins, except for the main field. The mandatory fields are name, version, and main. Here's a starting point:
# plugin.yml
name: ExamplePlugin
version: 1.0
main: example:ExamplePluginThe main field's value of example:ExamplePlugin indicates that our plugin's main class is the class ExamplePlugin in the module example. With a main class and a plugin.yml, Bukkit should now be able to load your plugin, and the "Hello world" message should display on startup. But what about commands, events, and other parts of Bukkit?
Just like Java plugins, commands are implemented by defining the command in plugin.yml:
# in plugin.yml
commands:
example:
usage: /example
description: An example command.And either defining onCommand on your plugin:
# in ExamplePlugin
def onCommand(self, sender, command, label, args):
sender.sendMessage("Hello, world!")
return TrueOr on a separate executor class:
from org.bukkit.command import CommandExecutor
class ExCommand(CommandExecutor)
def onCommand(self, sender, command, label, args):
sender.sendMessage("Hello, world!")
return True
# in onEnable
self.getCommand("example").executor = ExCommand()(For a simpler way to define commands, try out the Quick Module).
Much like Java plugins, events are defined by implementing the Listener interface and adding @EventHandler annotations. However, because Python is dynamically typed, the desired event must be specified manually.
class MyListener(Listener):
@EventHandler('player.PlayerJoinEvent')
def onJoin(self, event):
event.player.sendMessage("Hello, world!")
# in onEnable
self.server.pluginManager.registerEvents(MyListener(), self)Priorty can be specified by passing one of the priority names LOWEST, LOW, NORMAL, HIGH, HIGHEST, or MONITOR as the priority parameter, and ignore_cancelled is also available:
@EventHandler('player.PlayerMoveEvent', priority='HIGH', ignore_cancelled=True)All the rest of the Bukkit API can be accessed much like expected. Methods such as getServer() are available on your plugin methods' self parameter. For information on Java/Python interop, see Jython Features.
Many Bukkit classes will need to be imported, but these are included in the Python builtins:
-
PythonPlugin- base class for your plugin class. -
Listener- base class for listeners. -
EventHandler- the@EventHandlerannotation described above. -
Bukkit- the classorg.bukkit.Bukkit, for static access to the server.